|
20 | 20 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
|
21 | 21 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
|
22 | 22 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testDocument;
|
| 23 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirebaseApp; |
23 | 24 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
|
24 | 25 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
|
25 | 26 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitForException;
|
|
30 | 31 | import static org.junit.Assert.assertFalse;
|
31 | 32 | import static org.junit.Assert.assertNotEquals;
|
32 | 33 | import static org.junit.Assert.assertNotNull;
|
| 34 | +import static org.junit.Assert.assertNotSame; |
33 | 35 | import static org.junit.Assert.assertNull;
|
34 | 36 | import static org.junit.Assert.assertSame;
|
35 | 37 | import static org.junit.Assert.assertTrue;
|
|
38 | 40 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
39 | 41 | import com.google.android.gms.tasks.Task;
|
40 | 42 | import com.google.android.gms.tasks.TaskCompletionSource;
|
| 43 | +import com.google.firebase.FirebaseApp; |
41 | 44 | import com.google.firebase.Timestamp;
|
42 | 45 | import com.google.firebase.firestore.FirebaseFirestoreException.Code;
|
43 | 46 | import com.google.firebase.firestore.Query.Direction;
|
@@ -1023,4 +1026,74 @@ public void testClearPersistenceWhileRunningFails() {
|
1023 | 1026 | FirebaseFirestoreException firestoreException = (FirebaseFirestoreException) e;
|
1024 | 1027 | assertEquals(Code.FAILED_PRECONDITION, firestoreException.getCode());
|
1025 | 1028 | }
|
| 1029 | + |
| 1030 | + @Test |
| 1031 | + public void testRestartFirestoreLeadsToNewInstance() { |
| 1032 | + FirebaseApp app = testFirebaseApp(); |
| 1033 | + FirebaseFirestore instance = FirebaseFirestore.getInstance(app); |
| 1034 | + FirebaseFirestore sameInstance = FirebaseFirestore.getInstance(app); |
| 1035 | + |
| 1036 | + assertSame(instance, sameInstance); |
| 1037 | + waitFor(instance.document("abc/123").set(Collections.singletonMap("field", 100L))); |
| 1038 | + |
| 1039 | + instance.shutdown(); |
| 1040 | + FirebaseFirestore newInstance = FirebaseFirestore.getInstance(app); |
| 1041 | + |
| 1042 | + // Verify new instance works. |
| 1043 | + DocumentSnapshot doc = waitFor(newInstance.document("abc/123").get()); |
| 1044 | + assertEquals(doc.get("field"), 100L); |
| 1045 | + waitFor(newInstance.document("abc/123").delete()); |
| 1046 | + |
| 1047 | + // Verify it is different instance. |
| 1048 | + assertNotSame(instance, newInstance); |
| 1049 | + } |
| 1050 | + |
| 1051 | + @Test |
| 1052 | + public void testAppDeleteLeadsToFirestoreShutdown() { |
| 1053 | + FirebaseApp app = testFirebaseApp(); |
| 1054 | + FirebaseFirestore instance = FirebaseFirestore.getInstance(app); |
| 1055 | + waitFor(instance.document("abc/123").set(Collections.singletonMap("Field", 100))); |
| 1056 | + |
| 1057 | + app.delete(); |
| 1058 | + |
| 1059 | + assertTrue(instance.getClient().isShutdown()); |
| 1060 | + } |
| 1061 | + |
| 1062 | + @Test |
| 1063 | + public void testNewOperationThrowsAfterFirestoreShutdown() { |
| 1064 | + FirebaseFirestore instance = testFirestore(); |
| 1065 | + DocumentReference reference = instance.document("abc/123"); |
| 1066 | + waitFor(reference.set(Collections.singletonMap("Field", 100))); |
| 1067 | + |
| 1068 | + instance.shutdown(); |
| 1069 | + |
| 1070 | + final String expectedMessage = "The client has already been shutdown"; |
| 1071 | + expectError(() -> waitFor(reference.get()), expectedMessage); |
| 1072 | + expectError(() -> waitFor(reference.update("Field", 1)), expectedMessage); |
| 1073 | + expectError( |
| 1074 | + () -> waitFor(reference.set(Collections.singletonMap("Field", 1))), expectedMessage); |
| 1075 | + expectError( |
| 1076 | + () -> waitFor(instance.runBatch((batch) -> batch.update(reference, "Field", 1))), |
| 1077 | + expectedMessage); |
| 1078 | + expectError( |
| 1079 | + () -> waitFor(instance.runTransaction(transaction -> transaction.get(reference))), |
| 1080 | + expectedMessage); |
| 1081 | + } |
| 1082 | + |
| 1083 | + @Test |
| 1084 | + public void testShutdownCalledMultipleTimes() { |
| 1085 | + FirebaseFirestore instance = testFirestore(); |
| 1086 | + DocumentReference reference = instance.document("abc/123"); |
| 1087 | + waitFor(reference.set(Collections.singletonMap("Field", 100))); |
| 1088 | + |
| 1089 | + instance.shutdown(); |
| 1090 | + |
| 1091 | + final String expectedMessage = "The client has already been shutdown"; |
| 1092 | + expectError(() -> waitFor(reference.get()), expectedMessage); |
| 1093 | + |
| 1094 | + // Calling a second time should go through and change nothing. |
| 1095 | + instance.shutdown(); |
| 1096 | + |
| 1097 | + expectError(() -> waitFor(reference.get()), expectedMessage); |
| 1098 | + } |
1026 | 1099 | }
|
0 commit comments