use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method initialLogsMoreThan1Batch.
@Test
public void initialLogsMoreThan1Batch() throws IOException {
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
Ingestion ingestion = mock(Ingestion.class);
when(ingestion.isEnabled()).thenReturn(true);
doThrow(new IOException()).when(ingestion).close();
Persistence persistence = mock(Persistence.class);
when(persistence.countLogs(anyString())).thenReturn(103);
when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(3));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
verify(ingestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
assertEquals(3, channel.getGroupState(TEST_GROUP).mPendingLogCount);
delayedRunnable.getValue().run();
verify(ingestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method disableBeforeCheckingPendingLogs.
@Test
public void disableBeforeCheckingPendingLogs() {
Ingestion ingestion = mock(Ingestion.class);
when(ingestion.isEnabled()).thenReturn(true);
Persistence persistence = mock(Persistence.class);
final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
when(ingestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
/* Simulate a service disabled in the middle of network transaction. */
ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
channel.removeGroup(TEST_GROUP);
callback.onCallSucceeded(new HttpResponse(200, ""));
return null;
}
});
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method enqueuePersistenceFailure.
@Test
public void enqueuePersistenceFailure() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
/* Simulate Persistence failing. */
doThrow(new Persistence.PersistenceException("mock", new IOException("mock"))).when(mockPersistence).putLog(any(Log.class), anyString(), anyInt());
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 10, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
channel.addGroup(TEST_GROUP + "2", 10, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Verify no request is sent if Persistence fails. */
for (int i = 0; i < 10; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
channel.enqueue(mock(Log.class), TEST_GROUP + "2", Flags.DEFAULTS);
}
verify(mockPersistence, times(10)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
verify(mockPersistence, times(10)).putLog(any(Log.class), eq(TEST_GROUP + "2"), eq(NORMAL));
verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
verify(mockListener, times(10)).onBeforeSending(any(Log.class));
verify(mockListener, times(10)).onFailure(any(Log.class), any(Persistence.PersistenceException.class));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method analyticsSuccess.
@Test
public void analyticsSuccess() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.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());
when(mockIngestion.isEnabled()).thenReturn(true);
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Enqueuing 49 events. */
for (int i = 1; i <= 49; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
assertEquals(i, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* Enqueue another event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
/* The counter should be 0 as we reset the counter after reaching the limit of 50. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Verify that 5 items have been persisted. */
verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
/* 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.getGroupState(TEST_GROUP).mPendingLogCount);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Schedule only 1 log after that. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
assertEquals(1, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mockPersistence, times(51)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
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. */
delayedRunnable.getValue().run();
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
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, Flags.DEFAULTS);
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
assertEquals(2, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mockPersistence, times(53)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
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. */
delayedRunnable.getValue().run();
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
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(mAppCenterHandler, times(3)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
/* Check channel clear. */
channel.clear(TEST_GROUP);
verify(mockPersistence).deleteLogs(eq(TEST_GROUP));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.
@Test
public void invokeCallbacksAfterSuspendFatalNoListener() {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
/* Simulate a lot of logs already in database. */
when(mockPersistence.getLogs(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
/* Make first call hang, and the second call return a fatal error. */
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenReturn(null).then(getSendAsyncAnswer(new HttpException(new HttpResponse(403))));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null, null);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Enqueuing 2 events. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* Verify callbacks not invoked. */
verify(mockListener, never()).onBeforeSending(any(Log.class));
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify logs were deleted. */
verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Aggregations