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);
}
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")));
}
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());
}
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());
}
Aggregations