Search in sources :

Example 16 with AsyncQueue

use of com.google.firebase.firestore.util.AsyncQueue in project firebase-android-sdk by firebase.

the class RemoteStoreTest method testRemoteStoreStreamStopsWhenNetworkUnreachable.

@Test
public void testRemoteStoreStreamStopsWhenNetworkUnreachable() {
    AsyncQueue testQueue = new AsyncQueue();
    Datastore datastore = new Datastore(IntegrationTestUtil.testEnvDatabaseInfo(), testQueue, null, null, ApplicationProvider.getApplicationContext(), null);
    Semaphore networkChangeSemaphore = new Semaphore(0);
    RemoteStore.RemoteStoreCallback callback = new RemoteStore.RemoteStoreCallback() {

        @Override
        public void handleRemoteEvent(RemoteEvent remoteEvent) {
        }

        @Override
        public void handleRejectedListen(int targetId, Status error) {
        }

        @Override
        public void handleSuccessfulWrite(MutationBatchResult successfulWrite) {
        }

        @Override
        public void handleRejectedWrite(int batchId, Status error) {
        }

        @Override
        public void handleOnlineStateChange(OnlineState onlineState) {
            networkChangeSemaphore.release();
        }

        @Override
        public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
            return null;
        }
    };
    FakeConnectivityMonitor connectivityMonitor = new FakeConnectivityMonitor();
    QueryEngine queryEngine = new QueryEngine();
    Persistence persistence = MemoryPersistence.createEagerGcMemoryPersistence();
    persistence.start();
    IndexBackfiller indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    LocalStore localStore = new LocalStore(persistence, indexBackfiller, queryEngine, User.UNAUTHENTICATED);
    RemoteStore remoteStore = new RemoteStore(callback, localStore, datastore, testQueue, connectivityMonitor);
    waitFor(testQueue.enqueue(remoteStore::forceEnableNetwork));
    drain(testQueue);
    networkChangeSemaphore.drainPermits();
    connectivityMonitor.goOffline();
    waitFor(networkChangeSemaphore);
    drain(testQueue);
    networkChangeSemaphore.drainPermits();
    waitFor(testQueue.enqueue(remoteStore::forceEnableNetwork));
    connectivityMonitor.goOnline();
    waitFor(networkChangeSemaphore);
}
Also used : Status(io.grpc.Status) IndexBackfiller(com.google.firebase.firestore.local.IndexBackfiller) MutationBatchResult(com.google.firebase.firestore.model.mutation.MutationBatchResult) Semaphore(java.util.concurrent.Semaphore) QueryEngine(com.google.firebase.firestore.local.QueryEngine) LocalStore(com.google.firebase.firestore.local.LocalStore) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) Persistence(com.google.firebase.firestore.local.Persistence) MemoryPersistence(com.google.firebase.firestore.local.MemoryPersistence) OnlineState(com.google.firebase.firestore.core.OnlineState) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Test(org.junit.Test)

Example 17 with AsyncQueue

use of com.google.firebase.firestore.util.AsyncQueue in project firebase-android-sdk by firebase.

the class SQLiteOverlayMigrationManagerTest method testSkipsIfAlreadyMigrated.

@Test
public void testSkipsIfAlreadyMigrated() {
    writeRemoteDocument(doc("foo/bar", 2, map("it", "original")));
    writeMutation(setMutation("foo/bar", map("foo", "bar")));
    // Switch to new persistence and run migrations
    this.persistence.shutdown();
    persistence = PersistenceTestHelpers.createSQLitePersistence("test-data-migration");
    IndexBackfiller indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    localStore = new LocalStore(persistence, indexBackfiller, new QueryEngine(), User.UNAUTHENTICATED);
    localStore.start();
    assertEquals(setMutation("foo/bar", map("foo", "bar")), persistence.getDocumentOverlayCache(User.UNAUTHENTICATED).getOverlay(key("foo/bar")).getMutation());
    // Delete the overlay to verify migration is skipped the second time.
    persistence.getDocumentOverlayCache(User.UNAUTHENTICATED).removeOverlaysForBatchId(1);
    // Switch to new persistence and run migrations which should be a no-op.
    this.persistence.shutdown();
    persistence = PersistenceTestHelpers.createSQLitePersistence("test-data-migration");
    indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    localStore = new LocalStore(persistence, indexBackfiller, new QueryEngine(), User.UNAUTHENTICATED);
    localStore.start();
    SQLiteOverlayMigrationManager migrationManager = (SQLiteOverlayMigrationManager) persistence.getOverlayMigrationManager();
    assertFalse(migrationManager.hasPendingOverlayMigration());
    // We deleted the overlay earlier and the migration is not run again, so we get a null.
    assertNull(persistence.getDocumentOverlayCache(User.UNAUTHENTICATED).getOverlay(key("foo/bar")));
}
Also used : AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) Test(org.junit.Test)

Example 18 with AsyncQueue

use of com.google.firebase.firestore.util.AsyncQueue in project firebase-android-sdk by firebase.

the class SQLiteOverlayMigrationManagerTest method testCreateOverlayForDifferentUsers.

@Test
public void testCreateOverlayForDifferentUsers() {
    writeRemoteDocument(doc("foo/bar", 2, map("it", "original")));
    writeMutation(setMutation("foo/bar", map("foo", "set-by-unauthenticated")));
    // Switch to a different user
    IndexBackfiller indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    localStore = new LocalStore(persistence, indexBackfiller, new QueryEngine(), new User("another_user"));
    localStore.start();
    writeMutation(setMutation("foo/bar", map("foo", "set-by-another_user")));
    // Switch to new persistence and run migrations
    this.persistence.shutdown();
    persistence = PersistenceTestHelpers.createSQLitePersistence("test-data-migration");
    indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    localStore = new LocalStore(persistence, indexBackfiller, new QueryEngine(), User.UNAUTHENTICATED);
    localStore.start();
    assertEquals(setMutation("foo/bar", map("foo", "set-by-unauthenticated")), persistence.getDocumentOverlayCache(User.UNAUTHENTICATED).getOverlay(key("foo/bar")).getMutation());
    assertEquals(setMutation("foo/bar", map("foo", "set-by-another_user")), persistence.getDocumentOverlayCache(new User("another_user")).getOverlay(key("foo/bar")).getMutation());
    SQLiteOverlayMigrationManager migrationManager = (SQLiteOverlayMigrationManager) persistence.getOverlayMigrationManager();
    assertFalse(migrationManager.hasPendingOverlayMigration());
}
Also used : User(com.google.firebase.firestore.auth.User) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) Test(org.junit.Test)

Example 19 with AsyncQueue

use of com.google.firebase.firestore.util.AsyncQueue in project firebase-android-sdk by firebase.

the class SQLiteOverlayMigrationManagerTest method testCreateOverlayFromDelete.

@Test
public void testCreateOverlayFromDelete() {
    writeRemoteDocument(doc("foo/bar", 2, map("it", "original")));
    writeMutation(deleteMutation("foo/bar"));
    // Switch to new persistence and run migrations
    this.persistence.shutdown();
    persistence = PersistenceTestHelpers.createSQLitePersistence("test-data-migration");
    IndexBackfiller indexBackfiller = new IndexBackfiller(persistence, new AsyncQueue());
    localStore = new LocalStore(persistence, indexBackfiller, new QueryEngine(), User.UNAUTHENTICATED);
    localStore.start();
    assertEquals(deleteMutation("foo/bar"), persistence.getDocumentOverlayCache(User.UNAUTHENTICATED).getOverlay(key("foo/bar")).getMutation());
    // Version is 0 because of remote document elision.
    assertContains(deletedDoc("foo/bar", 2).setHasLocalMutations());
    SQLiteOverlayMigrationManager migrationManager = (SQLiteOverlayMigrationManager) persistence.getOverlayMigrationManager();
    assertFalse(migrationManager.hasPendingOverlayMigration());
}
Also used : AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) Test(org.junit.Test)

Aggregations

AsyncQueue (com.google.firebase.firestore.util.AsyncQueue)19 Test (org.junit.Test)15 EmptyAppCheckTokenProvider (com.google.firebase.firestore.testutil.EmptyAppCheckTokenProvider)5 EmptyCredentialsProvider (com.google.firebase.firestore.testutil.EmptyCredentialsProvider)3 User (com.google.firebase.firestore.auth.User)2 DatabaseId (com.google.firebase.firestore.model.DatabaseId)2 Context (android.content.Context)1 NonNull (androidx.annotation.NonNull)1 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)1 FirebaseAppCheckTokenProvider (com.google.firebase.firestore.auth.FirebaseAppCheckTokenProvider)1 FirebaseAuthCredentialsProvider (com.google.firebase.firestore.auth.FirebaseAuthCredentialsProvider)1 ComponentProvider (com.google.firebase.firestore.core.ComponentProvider)1 OnlineState (com.google.firebase.firestore.core.OnlineState)1 IndexBackfiller (com.google.firebase.firestore.local.IndexBackfiller)1 LocalStore (com.google.firebase.firestore.local.LocalStore)1 MemoryPersistence (com.google.firebase.firestore.local.MemoryPersistence)1 Persistence (com.google.firebase.firestore.local.Persistence)1 QueryEngine (com.google.firebase.firestore.local.QueryEngine)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1