Search in sources :

Example 6 with CancellationException

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

the class DefaultChannel method setNetworkRequests.

@Override
public void setNetworkRequests(boolean isAllowed) {
    if (isAllowed) {
        mCurrentState++;
        for (GroupState groupState : mGroupStates.values()) {
            checkPendingLogs(groupState);
        }
    } else {
        mEnabled = true;
        suspend(false, new CancellationException());
    }
}
Also used : CancellationException(com.microsoft.appcenter.CancellationException)

Example 7 with CancellationException

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

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionSuccess.

@Test(timeout = 5000)
public void disabledWhileHandlingIngestionSuccess() {
    /* 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);
    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]).onCallSucceeded(new HttpResponse(200, ""));
                    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 success was ignored. */
    verify(listener, never()).onSuccess(any(Log.class));
    verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof CancellationException;
        }
    }));
    verify(mockPersistence, never()).deleteLogs(anyString(), anyString());
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) 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)

Example 8 with CancellationException

use of com.microsoft.appcenter.CancellationException in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannel method setEnabled.

/**
 * Set the enabled flag. If false, the channel will continue to persist data but not forward any item to ingestion.
 * The most common use-case would be to set it to false and enable sending again after the channel has disabled itself after receiving
 * a recoverable error (most likely related to a server issue).
 *
 * @param enabled flag to enable or disable the channel.
 */
@Override
public synchronized void setEnabled(boolean enabled) {
    if (mEnabled == enabled) {
        return;
    }
    if (enabled) {
        mEnabled = true;
        mDiscardLogs = false;
        mCurrentState++;
        mIngestion.reopen();
        for (String groupName : mGroupStates.keySet()) {
            checkPendingLogs(groupName);
        }
    } else {
        suspend(true, new CancellationException());
    }
}
Also used : CancellationException(com.microsoft.appcenter.CancellationException)

Example 9 with CancellationException

use of com.microsoft.appcenter.CancellationException in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.

@Test
public void disabledWhileHandlingIngestionFailure() throws Exception {
    /* 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(), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockPersistence.getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    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) throws Throwable {
            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), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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) IOException(java.io.IOException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) 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)

Example 10 with CancellationException

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

the class DefaultChannel method setEnabled.

/**
 * Set the enabled flag. If false, the channel will continue to persist data but not forward any item to ingestion.
 * The most common use-case would be to set it to false and enable sending again after the channel has disabled itself after receiving
 * a recoverable error (most likely related to a server issue).
 *
 * @param enabled flag to enable or disable the channel.
 */
@Override
public void setEnabled(boolean enabled) {
    if (mEnabled == enabled) {
        return;
    }
    if (enabled) {
        mEnabled = true;
        mDiscardLogs = false;
        mCurrentState++;
        for (Ingestion ingestion : mIngestions) {
            ingestion.reopen();
        }
        for (GroupState groupState : mGroupStates.values()) {
            checkPendingLogs(groupState);
        }
    } else {
        mEnabled = false;
        suspend(true, new CancellationException());
    }
    /* Notify listeners that channel state has changed. */
    for (Listener listener : mListeners) {
        listener.onGloballyEnabled(enabled);
    }
}
Also used : CancellationException(com.microsoft.appcenter.CancellationException) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion)

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