use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method maxRequests.
@Test
public void maxRequests() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
/* We make second request return less logs than expected to make sure counter is reset properly. */
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(49)).then(getGetLogsAnswer()).then(getGetLogsAnswer()).then(getGetLogsAnswer(0));
final List<ServiceCallback> callbacks = new ArrayList<>();
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
if (args[3] instanceof ServiceCallback) {
callbacks.add((ServiceCallback) invocation.getArguments()[3]);
}
return null;
}
});
/* Init channel with mocks. */
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, null);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Enqueue enough logs to be split in N + 1 maximum requests. */
for (int i = 0; i < 200; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
verify(mAppCenterHandler, times(4)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, times(4)).removeCallbacks(any(Runnable.class));
/* Verify all logs stored, N requests sent, not log deleted yet. */
verify(mockPersistence, times(200)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Make 1 of the call succeed. Verify log deleted. */
callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* The request N+1 is now unlocked. */
verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Unlock all requests and check logs deleted. */
for (int i = 1; i < 4; i++) {
callbacks.get(i).onCallSucceeded(new HttpResponse(200, ""));
}
verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
/* Wait for timer. */
delayedRunnable.getValue().run();
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method maxRequestsInitial.
@Test
public void maxRequestsInitial() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockPersistence.countLogs(any(String.class))).thenReturn(100);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer());
final List<ServiceCallback> callbacks = new ArrayList<>();
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
if (args[3] instanceof ServiceCallback) {
callbacks.add((ServiceCallback) invocation.getArguments()[3]);
}
return null;
}
});
when(mockIngestion.isEnabled()).thenReturn(true);
/* Init channel with mocks. */
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, null);
/* Enqueue enough logs to be split in N + 1 maximum requests. */
for (int i = 0; i < 100; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
/* Verify all logs stored, N requests sent, not log deleted yet. */
verify(mockPersistence, times(100)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Make 1 of the call succeed. Verify log deleted. */
callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* The request N+1 is now unlocked. */
verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Unlock all requests and check logs deleted. */
for (int i = 1; i < 4; i++) callbacks.get(i).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Only 2 batches after channel start (non initial logs), verify timer interactions. */
verify(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, times(2)).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method analyticsFatal.
@Test
public void analyticsFatal() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(20));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(new HttpResponse(403)))).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, null);
/* Enqueuing 50 events. */
for (int i = 0; i < 50; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
/* Verify that 50 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 the Channel is disabled. */
assertFalse(channel.isEnabled());
/* Verify that we have cleared the logs. */
verify(mockPersistence).deleteLogs(TEST_GROUP);
/* Verify counter. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Enqueuing 20 more events. */
for (int i = 0; i < 20; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
/* The counter should still be 0 as logs are discarded by channel now. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* No more timer yet at this point. */
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Enable channel to see if it can work again after that error state. */
channel.setEnabled(true);
/* Enqueuing 20 more events. */
for (int i = 0; i < 20; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
assertEquals(20, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Wait for timer. */
delayedRunnable.getValue().run();
/* The counter should back to 0 now. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Verify that we have called sendAsync on the ingestion 2 times total: 1 earlier failure then 1 success. */
verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence for the successful batch after re-enabling. */
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* Verify 1 more timer call. */
verify(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* Verify no more cancel timer. */
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method checkPendingLogsResumesStartTime.
@Test
public void checkPendingLogsResumesStartTime() {
/* Mock stored start time. */
long startTime = 1000;
when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
/* Mock current time - before end of interval. */
long now = 5000;
when(System.currentTimeMillis()).thenReturn(now);
/* Create channel and group. */
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
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, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
/* Do not replace start timer value. */
verifyStatic(never());
SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
/* Start timer for remaining time. */
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL - (now - startTime)));
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method lessLogsThanExpected.
@Test
public void lessLogsThanExpected() {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40)).then(getGetLogsAnswer(0));
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);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* 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));
/* Wait for timer. */
delayedRunnable.getValue().run();
/* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Aggregations