Search in sources :

Example 6 with AsyncQueue

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

the class StreamTest method testWriteStreamStopAfterHandshake.

@Test
public void testWriteStreamStopAfterHandshake() throws Exception {
    AsyncQueue testQueue = new AsyncQueue();
    Datastore datastore = new Datastore(IntegrationTestUtil.testEnvDatabaseInfo(), testQueue, new EmptyCredentialsProvider(), new EmptyAppCheckTokenProvider(), ApplicationProvider.getApplicationContext(), null);
    final WriteStream[] writeStreamWrapper = new WriteStream[1];
    StreamStatusCallback streamCallback = new StreamStatusCallback() {

        @Override
        public void onHandshakeComplete() {
            assertThat(writeStreamWrapper[0].getLastStreamToken()).isNotEmpty();
            super.onHandshakeComplete();
        }

        @Override
        public void onWriteResponse(SnapshotVersion commitVersion, List<MutationResult> mutationResults) {
            assertThat(mutationResults).hasSize(1);
            assertThat(writeStreamWrapper[0].getLastStreamToken()).isNotEmpty();
            super.onWriteResponse(commitVersion, mutationResults);
        }
    };
    WriteStream writeStream = writeStreamWrapper[0] = datastore.createWriteStream(streamCallback);
    testQueue.enqueueAndForget(writeStream::start);
    waitFor(streamCallback.openSemaphore);
    // Writing before the handshake should throw
    testQueue.enqueueAndForget(() -> assertThrows(Throwable.class, () -> writeStream.writeMutations(mutations)));
    // Handshake should always be called
    testQueue.enqueueAndForget(writeStream::writeHandshake);
    waitFor(streamCallback.handshakeSemaphore);
    // Now writes should succeed
    testQueue.enqueueAndForget(() -> writeStream.writeMutations(mutations));
    waitFor(streamCallback.responseReceivedSemaphore);
    testQueue.runSync(writeStream::stop);
}
Also used : EmptyAppCheckTokenProvider(com.google.firebase.firestore.testutil.EmptyAppCheckTokenProvider) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ArrayList(java.util.ArrayList) List(java.util.List) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) EmptyCredentialsProvider(com.google.firebase.firestore.testutil.EmptyCredentialsProvider) Test(org.junit.Test)

Example 7 with AsyncQueue

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

the class StreamTest method testWatchStreamStopBeforeHandshake.

@Test
public void testWatchStreamStopBeforeHandshake() throws Exception {
    AsyncQueue testQueue = new AsyncQueue();
    GrpcMetadataProvider mockGrpcProvider = mock(GrpcMetadataProvider.class);
    Datastore datastore = new Datastore(IntegrationTestUtil.testEnvDatabaseInfo(), testQueue, new EmptyCredentialsProvider(), new EmptyAppCheckTokenProvider(), ApplicationProvider.getApplicationContext(), mockGrpcProvider);
    StreamStatusCallback streamCallback = new StreamStatusCallback() {
    };
    final WatchStream watchStream = datastore.createWatchStream(streamCallback);
    testQueue.enqueueAndForget(watchStream::start);
    waitFor(streamCallback.openSemaphore);
    // Stop should call watchStreamStreamDidClose.
    testQueue.runSync(watchStream::stop);
    assertThat(streamCallback.closeSemaphore.availablePermits()).isEqualTo(1);
    verify(mockGrpcProvider, times(1)).updateMetadata(any());
}
Also used : EmptyAppCheckTokenProvider(com.google.firebase.firestore.testutil.EmptyAppCheckTokenProvider) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue) EmptyCredentialsProvider(com.google.firebase.firestore.testutil.EmptyCredentialsProvider) Test(org.junit.Test)

Example 8 with AsyncQueue

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

the class IntegrationTestUtil method testFirestore.

public static FirebaseFirestore testFirestore(String projectId, Logger.Level logLevel, FirebaseFirestoreSettings settings, String persistenceKey) {
    // This unfortunately is a global setting that affects existing Firestore clients.
    Logger.setLogLevel(logLevel);
    Context context = ApplicationProvider.getApplicationContext();
    DatabaseId databaseId = DatabaseId.forDatabase(projectId, DatabaseId.DEFAULT_DATABASE_ID);
    ensureStrictMode();
    AsyncQueue asyncQueue = new AsyncQueue();
    FirebaseFirestore firestore = AccessHelper.newFirebaseFirestore(context, databaseId, persistenceKey, MockCredentialsProvider.instance(), new EmptyAppCheckTokenProvider(), asyncQueue, /*firebaseApp=*/
    null, /*instanceRegistry=*/
    (dbId) -> {
    });
    waitFor(firestore.clearPersistence());
    firestore.setFirestoreSettings(settings);
    firestoreStatus.put(firestore, true);
    return firestore;
}
Also used : Context(android.content.Context) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) DatabaseId(com.google.firebase.firestore.model.DatabaseId) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue)

Example 9 with AsyncQueue

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

the class SpecTestCase method initClient.

/**
 * Sets up a new client. Is used to initially setup the client initially and after every restart.
 */
private void initClient() {
    queue = new AsyncQueue();
    datastore = new MockDatastore(databaseInfo, queue, ApplicationProvider.getApplicationContext());
    ComponentProvider.Configuration configuration = new ComponentProvider.Configuration(ApplicationProvider.getApplicationContext(), queue, databaseInfo, datastore, currentUser, maxConcurrentLimboResolutions, new FirebaseFirestoreSettings.Builder().build());
    ComponentProvider provider = initializeComponentProvider(configuration, garbageCollectionEnabled);
    localPersistence = provider.getPersistence();
    remoteStore = provider.getRemoteStore();
    syncEngine = provider.getSyncEngine();
    eventManager = provider.getEventManager();
}
Also used : ComponentProvider(com.google.firebase.firestore.core.ComponentProvider) MockDatastore(com.google.firebase.firestore.remote.MockDatastore) AsyncQueue(com.google.firebase.firestore.util.AsyncQueue)

Example 10 with AsyncQueue

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

the class SQLiteOverlayMigrationManagerTest method testCreateOverlayFromPatches.

@Test
public void testCreateOverlayFromPatches() {
    writeRemoteDocument(doc("foo/bar", 2, map("it", "original")));
    writeMutation(patchMutation("foo/bar", map("it", FieldValue.increment(1))));
    writeMutations(asList(patchMutation("foo/bar", map("it", FieldValue.increment(1))), mergeMutation("foo/newBar", map("it", FieldValue.arrayUnion(1)), Collections.emptyList())));
    // 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();
    DocumentOverlayCache overlay = persistence.getDocumentOverlayCache(User.UNAUTHENTICATED);
    assertEquals(mergeMutation("foo/bar", map("it", 2), Collections.emptyList()), overlay.getOverlay(key("foo/bar")).getMutation());
    assertEquals(mergeMutation("foo/newBar", map("it", asList(1)), Collections.emptyList()), overlay.getOverlay(key("foo/newBar")).getMutation());
    assertContains(doc("foo/bar", 2, map("it", 2)).setHasLocalMutations());
    assertContains(doc("foo/newBar", 0, map("it", asList(1))).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