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