Search in sources :

Example 6 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class SyncProcessor method syncPage.

/**
 * Fetches one page for a sync.
 * @param request GraphQLRequest object for the sync, obtained from {@link AppSync#buildSyncRequest}, or from
 *                response.getData().getRequestForNextResult() for subsequent requests.
 * @param <T> The type of model to sync.
 */
private <T extends Model> Single<PaginatedResult<ModelWithMetadata<T>>> syncPage(GraphQLRequest<PaginatedResult<ModelWithMetadata<T>>> request) {
    return Single.create(emitter -> {
        Cancelable cancelable = appSync.sync(request, result -> {
            if (result.hasErrors()) {
                emitter.onError(new DataStoreException(String.format("A model sync failed: %s", result.getErrors()), "Check your schema."));
            } else if (!result.hasData()) {
                emitter.onError(new DataStoreException.IrRecoverableException("Empty response from AppSync.", "Report to AWS team."));
            } else {
                emitter.onSuccess(result.getData());
            }
        }, emitter::onError);
        emitter.setDisposable(AmplifyDisposables.fromCancelable(cancelable));
    });
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 7 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class RxAdaptersTest method completableIsDisposable.

/**
 * The {@link Completable} returned by
 * {@link VoidBehaviors#toCompletable(VoidBehaviors.ActionEmitter)}
 * is cancelable.
 */
// No-op VoidResultEmitter body
@SuppressWarnings("checkstyle:WhitespaceAround")
@Test
public void completableIsDisposable() {
    Completable completable = VoidBehaviors.toCompletable(((onResult, onError) -> {
    }));
    TestObserver<?> observer = completable.test();
    observer.dispose();
    assertTrue(observer.isDisposed());
}
Also used : Single(io.reactivex.rxjava3.core.Single) SimpleCancelable(com.amplifyframework.testutils.SimpleCancelable) CancelableBehaviors(com.amplifyframework.rx.RxAdapters.CancelableBehaviors) VoidBehaviors(com.amplifyframework.rx.RxAdapters.VoidBehaviors) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Sleep(com.amplifyframework.testutils.Sleep) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Cancelable(com.amplifyframework.core.async.Cancelable) Observable(io.reactivex.rxjava3.core.Observable) RandomString(com.amplifyframework.testutils.random.RandomString) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Disposable(io.reactivex.rxjava3.disposables.Disposable) UndeliverableException(io.reactivex.rxjava3.exceptions.UndeliverableException) Completable(io.reactivex.rxjava3.core.Completable) Test(org.junit.Test)

Example 8 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeQueryReturnsCategoryBehaviorChanges.

/**
 * The Rx binding for observeQuerying the DataStore should be an Observable stream
 * of DataStore changes. It should emit events whenever they are observed
 * on the observe behavior.
 */
@Test
public void observeQueryReturnsCategoryBehaviorChanges() {
    // Arrange: observe(Class<?>) will spit out some values from category behavior.
    List<Model> modelList = new ArrayList<>();
    modelList.add(RandomModel.model());
    DataStoreQuerySnapshot<Model> changeEvent = new DataStoreQuerySnapshot<>(new ArrayList<>(), true);
    doAnswer(invocation -> {
        // 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
        final int positionOfStartConsumer = 2;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfStartConsumer);
        onStart.accept(new NoOpCancelable());
        final int positionOfValueConsumer = 3;
        Consumer<DataStoreQuerySnapshot<Model>> onNext = invocation.getArgument(positionOfValueConsumer);
        onNext.accept(changeEvent);
        // "void"
        return null;
    }).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: Observe the DataStore via Rx binding
    TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
    // Assert: event is observed
    observer.awaitCount(1).assertValue(changeEvent);
    verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : ArrayList(java.util.ArrayList) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Example 9 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeCompletesWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observe method is an Observable. It should
 * complete when the Rx binding's completion callback is triggered.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeCompletesWhenCategoryBehaviorDoes() throws InterruptedException {
    // Category behavior is arranged to complete
    doAnswer(invocation -> {
        // 0 = clazz, 1 = onStart, 2 = onNext, 3 = onFailure, 4 = onComplete
        final int positionOfOnStart = 2;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnComplete = 4;
        Action onComplete = invocation.getArgument(positionOfOnComplete);
        onComplete.call();
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertComplete();
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : Matchers.anyAction(com.amplifyframework.rx.Matchers.anyAction) Action(com.amplifyframework.core.Action) DataStoreItemChange(com.amplifyframework.datastore.DataStoreItemChange) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Example 10 with Cancelable

use of com.amplifyframework.core.async.Cancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeQueryCompletesWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observeQuery method is an Observable. It should
 * complete when the Rx binding's completion callback is triggered.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeQueryCompletesWhenCategoryBehaviorDoes() throws InterruptedException {
    // Category behavior is arranged to complete
    doAnswer(invocation -> {
        // 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
        final int positionOfOnStart = 3;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnComplete = 5;
        Action onComplete = invocation.getArgument(positionOfOnComplete);
        onComplete.call();
        // "void"
        return null;
    }).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe via Rx binding
    TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertComplete();
    verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : Matchers.anyAction(com.amplifyframework.rx.Matchers.anyAction) Action(com.amplifyframework.core.Action) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Aggregations

Cancelable (com.amplifyframework.core.async.Cancelable)40 Test (org.junit.Test)30 DataStoreException (com.amplifyframework.datastore.DataStoreException)29 Action (com.amplifyframework.core.Action)28 Consumer (com.amplifyframework.core.Consumer)26 CountDownLatch (java.util.concurrent.CountDownLatch)24 ArrayList (java.util.ArrayList)23 List (java.util.List)23 TimeUnit (java.util.concurrent.TimeUnit)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)22 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)22 Collections (java.util.Collections)22 Model (com.amplifyframework.core.model.Model)20 Arrays (java.util.Arrays)20 NoOpAction (com.amplifyframework.core.NoOpAction)19 NoOpConsumer (com.amplifyframework.core.NoOpConsumer)19 QuerySortBy (com.amplifyframework.core.model.query.QuerySortBy)19 Post (com.amplifyframework.testmodels.commentsblog.Post)19 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)19