Search in sources :

Example 36 with Cancelable

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

the class RxAdaptersTest method underlyingCancelIsCalledWhenObservableSubscriptionIsDisposed.

/**
 * The {@link Observable} returned by
 * {@link CancelableBehaviors#toObservable(CancelableBehaviors.StreamEmitter)}
 * is cancelable.
 */
@Test
public void underlyingCancelIsCalledWhenObservableSubscriptionIsDisposed() {
    SimpleCancelable cancelable = new SimpleCancelable();
    TestObserver<?> observer = CancelableBehaviors.toObservable((onStart, onItem, onError, onComplete) -> cancelable).test();
    observer.dispose();
    assertTrue(observer.isDisposed());
    assertTrue(cancelable.isCanceled());
}
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) SimpleCancelable(com.amplifyframework.testutils.SimpleCancelable) Test(org.junit.Test)

Example 37 with Cancelable

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

the class RxDataStoreBindingTest method observeFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observe behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = clazz, 1 = onStart, 2 = onNext, 3 = onFailure, 4 = onComplete
        final int positionOfOnStart = 1;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnFailure = 3;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe the DataStore via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) 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 38 with Cancelable

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

the class RxDataStoreBindingTest method observeQueryFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observeQuery behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeQueryFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    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 positionOfOnFailure = 4;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "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: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) 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)

Example 39 with Cancelable

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

the class RxDataStoreBindingTest method observeReturnsCategoryBehaviorChanges.

/**
 * The Rx binding for observing 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 observeReturnsCategoryBehaviorChanges() {
    // Arrange: observe(Class<?>) will spit out some values from category behavior.
    Model model = RandomModel.model();
    DataStoreItemChange<Model> changeEvent = DataStoreItemChange.builder().uuid(model.getId()).itemClass(Model.class).item(model).type(Type.CREATE).initiator(Initiator.LOCAL).build();
    doAnswer(invocation -> {
        // 0 = clazz, 1 = start consumer, 2 = item consumer, 3 = failure consumer, 4 = onComplete
        final int positionOfStartConsumer = 1;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfStartConsumer);
        onStart.accept(new NoOpCancelable());
        final int positionOfValueConsumer = 2;
        Consumer<DataStoreItemChange<Model>> onNext = invocation.getArgument(positionOfValueConsumer);
        onNext.accept(changeEvent);
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: Observe the DataStore via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    // Assert: event is observed
    observer.awaitCount(1).assertValue(changeEvent);
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : 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 40 with Cancelable

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

the class SynchronousApi method onCreate.

/**
 * Subscribe to model creations.
 *
 * @param apiName One of the configured API endpoints
 * @param clazz   Class of model for which you want notifications when they're created
 * @param <T>     The type of model for which creation notifications will be dispatched
 * @return An Observable that can be used to observe the subscription data
 */
@SuppressWarnings("CodeBlock2Expr")
@NonNull
public <T extends Model> Observable<GraphQLResponse<T>> onCreate(@NonNull String apiName, @NonNull Class<T> clazz) {
    return Observable.create(emitter -> {
        Await.<String, ApiException>result(OPERATION_TIMEOUT_MS, (onSubscriptionStarted, onError) -> {
            Cancelable cancelable = asyncDelegate.subscribe(apiName, ModelSubscription.onCreate(clazz), onSubscriptionStarted, emitter::onNext, onError, emitter::onComplete);
            emitter.setDisposable(AmplifyDisposables.fromCancelable(cancelable));
        });
    });
}
Also used : Cancelable(com.amplifyframework.core.async.Cancelable) ApiException(com.amplifyframework.api.ApiException) NonNull(androidx.annotation.NonNull)

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