use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.
@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatal() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE - 1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(403)));
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);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify callbacks invoked (1 + DefaultChannel.CLEAR_BATCH_SIZE + DefaultChannel.CLEAR_BATCH_SIZE - 1) times. */
verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onBeforeSending(any(Log.class));
verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onFailure(any(Log.class), any(Exception.class));
/* Verify logs were deleted. */
verify(mockPersistence).deleteLogs(TEST_GROUP);
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.
@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendRecoverable() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(503)));
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);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify callbacks invoked only for the first log. */
verify(mockListener).onBeforeSending(any(Log.class));
/* Verify no failure forwarded. */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify no log was deleted. */
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* But that we cleared batch state. */
verify(mockPersistence).clearPendingLogState();
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method analyticsSuccess.
@Test
@SuppressWarnings("unchecked")
public void analyticsSuccess() throws Persistence.PersistenceException, InterruptedException {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(2));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer());
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 49 events. */
for (int i = 1; i <= 49; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP);
assertEquals(i, channel.getCounter(TEST_GROUP));
}
verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* Enqueue another event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
verify(mHandler).removeCallbacks(any(Runnable.class));
/* The counter should be 0 as we reset the counter after reaching the limit of 50. */
assertEquals(0, channel.getCounter(TEST_GROUP));
/* Verify that 5 items have been persisted. */
verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
/* Verify that we have called sendAsync on the ingestion. */
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence. */
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. */
verify(mockListener, times(50)).onBeforeSending(any(Log.class));
/* Verify that we have called onSuccess in the listener. */
verify(mockListener, times(50)).onSuccess(any(Log.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getCounter(TEST_GROUP));
/* Prepare to mock timer. */
AtomicReference<Runnable> runnable = catchPostRunnable();
/* Schedule only 1 log after that. */
channel.enqueue(mock(Log.class), TEST_GROUP);
assertEquals(1, channel.getCounter(TEST_GROUP));
verify(mockPersistence, times(51)).putLog(eq(TEST_GROUP), any(Log.class));
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
verify(mockListener, times(50)).onSuccess(any(Log.class));
/* Simulate the timer. */
assertNotNull(runnable.get());
runnable.get().run();
runnable.set(null);
assertEquals(0, channel.getCounter(TEST_GROUP));
verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
verify(mockListener, times(51)).onSuccess(any(Log.class));
/* 2 more timed logs. */
channel.enqueue(mock(Log.class), TEST_GROUP);
channel.enqueue(mock(Log.class), TEST_GROUP);
assertEquals(2, channel.getCounter(TEST_GROUP));
verify(mockPersistence, times(53)).putLog(eq(TEST_GROUP), any(Log.class));
verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
verify(mockListener, times(51)).onSuccess(any(Log.class));
/* Simulate the timer. */
assertNotNull(runnable.get());
runnable.get().run();
assertEquals(0, channel.getCounter(TEST_GROUP));
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, times(3)).deleteLogs(any(String.class), any(String.class));
verify(mockListener, times(53)).onSuccess(any(Log.class));
/* Check total timers. */
verify(mHandler, times(3)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler).removeCallbacks(any(Runnable.class));
/* Check channel clear clear */
channel.clear(TEST_GROUP);
verify(mockPersistence).deleteLogs(eq(TEST_GROUP));
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionSuccess.
@Test
public void disabledWhileHandlingIngestionSuccess() 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);
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]).onCallSucceeded("");
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 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;
}
}));
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-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));
}
Aggregations