use of com.microsoft.appcenter.persistence.Persistence 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.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannel method buildDefaultPersistence.
/**
* Init Persistence for default constructor.
*/
private static Persistence buildDefaultPersistence(@NonNull LogSerializer logSerializer) {
Persistence persistence = new DatabasePersistence();
persistence.setLogSerializer(logSerializer);
return persistence;
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileSendingLogs.
@Test
public void disabledWhileSendingLogs() 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);
doAnswer(new Answer<Void>() {
@Override
public Void answer(final InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
beforeCallSemaphore.acquireUninterruptibly();
((Runnable) invocation.getArguments()[0]).run();
afterCallSemaphore.release();
}
}.start();
return null;
}
}).when(HandlerUtils.class);
HandlerUtils.runOnUiThread(any(Runnable.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 ingestion not sent. */
verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method shutdown.
@Test
public void shutdown() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
verify(mockListener).onBeforeSending(notNull(Log.class));
channel.shutdown();
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
verify(mockPersistence).clearPendingLogState();
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method errorLogRecoverable.
@Test
@SuppressWarnings("unchecked")
public void errorLogRecoverable() throws Persistence.PersistenceException, InterruptedException {
Persistence mockPersistence = mock(Persistence.class);
Ingestion mockIngestion = mock(Ingestion.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new SocketException())).then(getSendAsyncAnswer());
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing n errors. */
int logNumber = 5;
for (int i = 0; i < logNumber; i++) channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify that n items have been persisted. */
verify(mockPersistence, times(logNumber)).putLog(eq(TEST_GROUP), any(Log.class));
/* Verify that we have called sendAsync on the ingestion once for the first item, but not more than that. */
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have not called deleteLogs on the Persistence. */
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. */
verify(mockListener).onBeforeSending(any(Log.class));
/* Verify that we have not called the failure listener. It's a transient exception that will be retried later when the channel is re-enabled. */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify that the Channel is disabled. */
assertFalse(channel.isEnabled());
verify(mockPersistence).clearPendingLogState();
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* Verify timer. */
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
channel.setEnabled(true);
/* Verify that we have called sendAsync on the ingestion n+1 times total: 1 failure before re-enabling, n success after. */
verify(mockIngestion, times(logNumber + 1)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence n times. */
verify(mockPersistence, times(logNumber)).deleteLogs(any(String.class), any(String.class));
/* Verify timer. */
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
Aggregations