Search in sources :

Example 46 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RetryHandlerTest method testRetryOnRecoverableError.

/**
 * Test retry on recoverable error.
 */
@Test
public void testRetryOnRecoverableError() {
    // arrange
    RetryHandler subject = new RetryHandler(8, 0, 1, 1);
    DataStoreException expectedException = new DataStoreException("PaginatedResult<ModelWithMetadata<BlogOwner>>", "");
    AtomicInteger count = new AtomicInteger(0);
    Single<Object> mockSingle = Single.error(expectedException).doOnError(e -> count.incrementAndGet());
    // act and assert
    subject.retry(mockSingle, new ArrayList<>()).test().awaitDone(10, TimeUnit.SECONDS).assertError(expectedException).isDisposed();
    assertEquals(2, count.get());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 47 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RetryHandlerTest method testNoRetryOnIrrecoverableError.

/**
 * Test no retry on Irrecoverable error.
 */
@Test
public void testNoRetryOnIrrecoverableError() {
    // arrange
    RetryHandler subject = new RetryHandler();
    DataStoreException expectedException = new DataStoreException.GraphQLResponseException("PaginatedResult<ModelWithMetadata<BlogOwner>>", new ArrayList<>());
    Single<String> mockSingle = Single.error(expectedException);
    ArrayList<Class<? extends Throwable>> skipExceptionList = new ArrayList<>();
    skipExceptionList.add(DataStoreException.GraphQLResponseException.class);
    // act and assert
    subject.retry(mockSingle, skipExceptionList).test().awaitDone(1, TimeUnit.SECONDS).assertError(expectedException).isDisposed();
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 48 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class SubscriptionProcessorTest method arrangeDataEmittingSubscription.

@SuppressWarnings("SameParameterValue")
private static <T extends Model> void arrangeDataEmittingSubscription(AppSync appSync, ModelSchema modelSchema, SubscriptionType subscriptionType, GraphQLResponse<ModelWithMetadata<T>> response) throws DataStoreException {
    Answer<Cancelable> answer = invocation -> {
        final int startConsumerIndex = 1;
        Consumer<String> onStart = invocation.getArgument(startConsumerIndex);
        onStart.accept(RandomString.string());
        final int dataConsumerIndex = 2;
        Consumer<GraphQLResponse<ModelWithMetadata<T>>> onData = invocation.getArgument(dataConsumerIndex);
        onData.accept(response);
        return new NoOpCancelable();
    };
    arrangeSubscription(appSync, answer, modelSchema, subscriptionType);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ModelProvider(com.amplifyframework.core.model.ModelProvider) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) AppSync(com.amplifyframework.datastore.appsync.AppSync) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Answer(org.mockito.stubbing.Answer) Consumer(com.amplifyframework.core.Consumer) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Observable(io.reactivex.rxjava3.core.Observable) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) Before(org.junit.Before) DataStoreConfiguration(com.amplifyframework.datastore.DataStoreConfiguration) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) RandomString(com.amplifyframework.testutils.random.RandomString) Temporal(com.amplifyframework.core.model.temporal.Temporal) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Consumer(com.amplifyframework.core.Consumer) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable)

Example 49 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class SyncProcessorTest method retryHandlesHydrateSubscriptionDispose.

/**
 * Verify that retry is called on appsync failure and when dispose in called midway no exception is thrown.
 *
 * @throws AmplifyException On failure to build GraphQLRequest for sync query.
 */
@Test
public void retryHandlesHydrateSubscriptionDispose() throws AmplifyException {
    // Arrange: mock failure when invoking hydrate
    requestRetry = spy(RetryHandler.class);
    initSyncProcessor(10_000);
    AppSyncMocking.sync(appSync).mockFailure(new DataStoreException("Something timed out during sync.", ""));
    // Act: call hydrate.
    TestObserver<Void> testObserver = syncProcessor.hydrate().test(false);
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            testObserver.dispose();
        }
    }, 10000);
    verify(requestRetry, times(1)).retry(any(), any());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Test(org.junit.Test)

Example 50 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class SyncProcessorTest method shouldNotRetryOnAppSyncFailureWhenSynRetryIsSetToFalse.

/**
 * Verify that retry is called on appsync failure when syncRetry is set to true.
 *
 * @throws AmplifyException On failure to build GraphQLRequest for sync query.
 */
@Test
public void shouldNotRetryOnAppSyncFailureWhenSynRetryIsSetToFalse() throws AmplifyException {
    // Arrange: mock failure when invoking hydrate on the mock object.
    requestRetry = mock(RetryHandler.class);
    isSyncRetryEnabled = false;
    when(requestRetry.retry(any(), any())).thenReturn(Single.error(new DataStoreException("PaginatedResult<ModelWithMetadata<BlogOwner>>", "")));
    initSyncProcessor(10_000);
    AppSyncMocking.sync(appSync).mockFailure(new DataStoreException("Something timed out during sync.", ""));
    // Act: call hydrate.
    syncProcessor.hydrate().test(false).assertNotComplete();
    verify(requestRetry, times(0)).retry(any(), any());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Test(org.junit.Test)

Aggregations

DataStoreException (com.amplifyframework.datastore.DataStoreException)89 Test (org.junit.Test)52 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)36 Consumer (com.amplifyframework.core.Consumer)32 List (java.util.List)32 Cancelable (com.amplifyframework.core.async.Cancelable)31 Model (com.amplifyframework.core.model.Model)31 ArrayList (java.util.ArrayList)31 AmplifyException (com.amplifyframework.AmplifyException)29 ModelSchema (com.amplifyframework.core.model.ModelSchema)28 Collections (java.util.Collections)28 Action (com.amplifyframework.core.Action)27 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)27 TimeUnit (java.util.concurrent.TimeUnit)25 Post (com.amplifyframework.testmodels.commentsblog.Post)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)23 Arrays (java.util.Arrays)23 Assert.assertEquals (org.junit.Assert.assertEquals)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)21