Search in sources :

Example 1 with OutboxEvent

use of com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method updateEventPostedWhenExistingOutboxItemUpdate.

/**
 * When an already-pending mutation is updated, then the {@link Observable} returned by
 * {@link MutationOutbox#events()} should emit an {@link OutboxEvent#CONTENT_AVAILABLE} event.
 */
@Test
public void updateEventPostedWhenExistingOutboxItemUpdate() {
    // Watch for events.
    TestObserver<OutboxEvent> eventsObserver = mutationOutbox.events().test();
    // Create tony.
    BlogOwner tonyWrongName = BlogOwner.builder().name("Tony Jon Swanssssssssson yee-haw!").build();
    PendingMutation<BlogOwner> originalCreation = PendingMutation.creation(tonyWrongName, schema);
    mutationOutbox.enqueue(originalCreation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Update tony - we spelled his name wrong originally
    BlogOwner tonySpelledRight = tonyWrongName.copyOfBuilder().name("Tony Jon (\"TJ\") Swanson").build();
    mutationOutbox.enqueue(PendingMutation.update(tonySpelledRight, schema)).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Assert: an event for the original creation, then another for the update
    eventsObserver.awaitCount(2).assertNoErrors().assertNotComplete().assertValues(OutboxEvent.CONTENT_AVAILABLE, OutboxEvent.CONTENT_AVAILABLE);
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) Test(org.junit.Test)

Example 2 with OutboxEvent

use of com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method enqueueDoesNothingBeforeSubscription.

/**
 * The enqueue() returns a Completable, but that Completable doesn't actually invoke
 * any behavior until it is subscribed.
 * @throws DataStoreException On failure to query results, for assertions
 */
@Test
public void enqueueDoesNothingBeforeSubscription() throws DataStoreException {
    // Watch for notifications on the observe() API.
    TestObserver<OutboxEvent> testObserver = mutationOutbox.events().test();
    // Enqueue something, but don't subscribe to the observable just yet.
    BlogOwner tony = BlogOwner.builder().name("Tony Daniels").build();
    mutationOutbox.enqueue(PendingMutation.creation(tony, schema));
    // .subscribe() is NOT called on the enqueue() above!! This is the point!!!
    // Note that nothing has actually happened yet --
    // Nothing was put out on the observable ...
    testObserver.assertNoValues();
    testObserver.assertNotComplete();
    testObserver.dispose();
    // And nothing is in storage.
    assertTrue(storage.query(PersistentRecord.class).isEmpty());
    // And nothing is peek()ed.
    assertNull(mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) Test(org.junit.Test)

Example 3 with OutboxEvent

use of com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method notifiesWhenContentAvailableAfterDelete.

/**
 * When there are multiple pending mutations in the outbox, and one is removed,
 * we will see a {@link OutboxEvent#CONTENT_AVAILABLE} after the removal. This
 * notifies the system that there is more work to be done, even though we've successfully
 * processed an event. The system will continue processing items from the outbox until
 * all have been processed.
 * @throws DataStoreException On failure to arrange data into storage
 * @throws InterruptedException If thread interrupted while waiting for events
 */
@Test
public void notifiesWhenContentAvailableAfterDelete() throws DataStoreException, InterruptedException {
    // Start watching the events stream. We'll expect a notification here once,
    // after the first deletion.
    TestObserver<OutboxEvent> firstEventObserver = mutationOutbox.events().test();
    // Arrange a few mutations into the queue.
    BlogOwner senatorBernie = BlogOwner.builder().name("Senator Bernard Sanders").build();
    PendingMutation<BlogOwner> createSenatorBernie = PendingMutation.creation(senatorBernie, schema);
    storage.save(converter.toRecord(createSenatorBernie));
    BlogOwner candidateBernie = senatorBernie.copyOfBuilder().name("Democratic Presidential Candidate, Bernard Sanders").build();
    PendingMutation<BlogOwner> updateCandidateBernie = PendingMutation.update(candidateBernie, schema);
    storage.save(converter.toRecord(updateCandidateBernie));
    mutationOutbox.load().blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // Remove first item.
    TestObserver<Void> firstRemoval = mutationOutbox.remove(createSenatorBernie.getMutationId()).test();
    firstRemoval.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    firstRemoval.assertNoErrors().assertComplete().dispose();
    // One event is observed on events(), since there are still some pending mutations
    // that need to be processed.
    firstEventObserver.awaitCount(1).assertValues(OutboxEvent.CONTENT_AVAILABLE).assertNoErrors();
    // Get ready to watch the events() again.
    TestObserver<OutboxEvent> secondEventObserver = mutationOutbox.events().test();
    // Remove the next item.
    TestObserver<Void> secondRemoval = mutationOutbox.remove(updateCandidateBernie.getMutationId()).test();
    secondRemoval.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    secondRemoval.assertNoErrors().assertComplete().dispose();
    // This time, we don't see any event on events(), since the outbox has become empty.
    secondEventObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    secondEventObserver.assertNoValues().assertNoErrors();
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) Test(org.junit.Test)

Example 4 with OutboxEvent

use of com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent in project amplify-android by aws-amplify.

the class PersistentMutationOutboxTest method enqueuePersistsMutationAndNotifiesObserver.

/**
 * Enqueueing a mutation should have the result of persisting
 * the mutation to storage, and notifying any observers that
 * a new mutation has been enqueued.
 * @throws DataStoreException On failure to query results, for assertions
 * @throws InterruptedException If interrupted while awaiting terminal result in test observer
 */
@Test
public void enqueuePersistsMutationAndNotifiesObserver() throws DataStoreException, InterruptedException {
    // Observe the queue
    TestObserver<OutboxEvent> queueObserver = mutationOutbox.events().test();
    BlogOwner jameson = BlogOwner.builder().name("Jameson Williams").build();
    PendingMutation<BlogOwner> createJameson = PendingMutation.creation(jameson, schema);
    HubAccumulator savedMutationsAccumulator = HubAccumulator.create(HubChannel.DATASTORE, isEnqueued(jameson), 1).start();
    // Enqueue an save for a Jameson BlogOwner object,
    // and make sure that it calls back onComplete().
    TestObserver<Void> saveObserver = mutationOutbox.enqueue(createJameson).test();
    saveObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    saveObserver.assertNoErrors().assertComplete();
    saveObserver.dispose();
    // Wait for a Hub event telling us that the model got successfully enqueued.
    savedMutationsAccumulator.await();
    // Expected to observe the mutation on the subject
    queueObserver.awaitCount(1);
    queueObserver.assertValue(OutboxEvent.CONTENT_AVAILABLE);
    queueObserver.dispose();
    // Assert that the storage contains the mutation
    assertEquals(Collections.singletonList(converter.toRecord(createJameson)), storage.query(PersistentRecord.class));
    assertTrue(mutationOutbox.hasPendingMutation(jameson.getId()));
    assertEquals(createJameson, mutationOutbox.peek());
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) OutboxEvent(com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) PersistentRecord(com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord) Test(org.junit.Test)

Aggregations

OutboxEvent (com.amplifyframework.datastore.syncengine.MutationOutbox.OutboxEvent)4 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)4 Test (org.junit.Test)4 PersistentRecord (com.amplifyframework.datastore.syncengine.PendingMutation.PersistentRecord)1 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)1