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