use of com.microsoft.appcenter.http.HttpResponse 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.http.HttpResponse 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.http.HttpResponse 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.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.
@Test
public void invokeCallbacksAfterSuspendFatal() {
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(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.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(new HttpResponse(403))));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* 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.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.
@Test
public void invokeCallbacksAfterSuspendRecoverable() {
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(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(new HttpResponse(503))));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* 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();
}
Aggregations