Search in sources :

Example 11 with CancellationException

use of com.microsoft.appcenter.CancellationException in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method shutdown.

@Override
public void shutdown() {
    mEnabled = false;
    suspend(false, new CancellationException());
}
Also used : CancellationException(com.microsoft.appcenter.CancellationException)

Example 12 with CancellationException

use of com.microsoft.appcenter.CancellationException in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method deleteLogsOnSuspended.

private void deleteLogsOnSuspended(final GroupState groupState) {
    final List<Log> logs = new ArrayList<>();
    mPersistence.getLogs(groupState.mName, Collections.<String>emptyList(), CLEAR_BATCH_SIZE, logs);
    if (logs.size() > 0 && groupState.mListener != null) {
        for (Log log : logs) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
    }
    if (logs.size() >= CLEAR_BATCH_SIZE && groupState.mListener != null) {
        deleteLogsOnSuspended(groupState);
    } else {
        mPersistence.deleteLogs(groupState.mName);
    }
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) CancellationException(com.microsoft.appcenter.CancellationException) ArrayList(java.util.ArrayList)

Example 13 with CancellationException

use of com.microsoft.appcenter.CancellationException in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.

@Test(timeout = 5000)
public void disabledWhileHandlingIngestionFailure() {
    /* Set up mocking. */
    final Semaphore beforeCallSemaphore = new Semaphore(0);
    final Semaphore afterCallSemaphore = new Semaphore(0);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    final Exception mockException = new IOException();
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) {
            new Thread() {

                @Override
                public void run() {
                    beforeCallSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[3]).onCallFailed(mockException);
                    afterCallSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    /* Simulate enable module then disable. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, listener);
    channel.setEnabled(false);
    channel.setEnabled(true);
    /* Release call to mock ingestion. */
    beforeCallSemaphore.release();
    /* Wait for callback ingestion. */
    afterCallSemaphore.acquireUninterruptibly();
    /* Verify handling error was ignored. */
    verify(listener, never()).onFailure(any(Log.class), eq(mockException));
    verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof CancellationException;
        }
    }));
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) Log(com.microsoft.appcenter.ingestion.models.Log) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CancellationException(com.microsoft.appcenter.CancellationException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatcher(org.mockito.ArgumentMatcher) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

CancellationException (com.microsoft.appcenter.CancellationException)13 Persistence (com.microsoft.appcenter.persistence.Persistence)7 Log (com.microsoft.appcenter.ingestion.models.Log)6 Context (android.content.Context)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4 UUID (java.util.UUID)4 Semaphore (java.util.concurrent.Semaphore)4 Test (org.junit.Test)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)3 DatabasePersistence (com.microsoft.appcenter.persistence.DatabasePersistence)3 DeviceInfoHelper (com.microsoft.appcenter.utils.DeviceInfoHelper)3 Date (java.util.Date)3 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2