use of com.microsoft.azure.mobile.ingestion.IngestionHttp in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method shutdownInterrupted.
@Test
public void shutdownInterrupted() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
doThrow(new InterruptedException()).when(mockPersistenceAsync).waitForCurrentTasksToComplete(anyLong());
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
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();
verify(mockPersistenceAsync).waitForCurrentTasksToComplete(DefaultChannel.SHUTDOWN_TIMEOUT);
verifyStatic();
MobileCenterLog.warn(eq(MobileCenterLog.LOG_TAG), anyString(), argThat(new ArgumentMatcher<Throwable>() {
@Override
public boolean matches(Object argument) {
return argument instanceof InterruptedException;
}
}));
}
use of com.microsoft.azure.mobile.ingestion.IngestionHttp in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.
@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatalNoListener() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
/* Simulate a lot of logs already in database. */
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.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(403)));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 2 events. */
channel.enqueue(mock(Log.class), TEST_GROUP);
channel.enqueue(mock(Log.class), TEST_GROUP);
/* 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);
}
use of com.microsoft.azure.mobile.ingestion.IngestionHttp in project mobile-center-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);
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
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);
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.azure.mobile.ingestion.IngestionHttp in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method maxRequestsInitial.
@Test
@SuppressWarnings("unchecked")
public void maxRequestsInitial() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
when(mockPersistence.countLogs(any(String.class))).thenReturn(100);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.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) throws Throwable {
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), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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);
}
/* Verify all logs stored, N requests sent, not log deleted yet. */
verify(mockPersistence, times(100)).putLog(eq(TEST_GROUP), any(Log.class));
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("");
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("");
verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getCounter(TEST_GROUP));
/* Only 2 batches after channel start (non initial logs), verify timer interactions. */
verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, times(2)).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.azure.mobile.ingestion.IngestionHttp in project mobile-center-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);
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
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);
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);
}
Aggregations