use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method errorLogRecoverable.
@Test
@SuppressWarnings("unchecked")
public void errorLogRecoverable() throws Persistence.PersistenceException, InterruptedException {
Persistence mockPersistence = mock(Persistence.class);
Ingestion mockIngestion = mock(Ingestion.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new SocketException())).then(getSendAsyncAnswer());
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 n errors. */
int logNumber = 5;
for (int i = 0; i < logNumber; i++) channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify that n items have been persisted. */
verify(mockPersistence, times(logNumber)).putLog(eq(TEST_GROUP), any(Log.class));
/* Verify that we have called sendAsync on the ingestion once for the first item, but not more than that. */
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have not called deleteLogs on the Persistence. */
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. */
verify(mockListener).onBeforeSending(any(Log.class));
/* Verify that we have not called the failure listener. It's a transient exception that will be retried later when the channel is re-enabled. */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify that the Channel is disabled. */
assertFalse(channel.isEnabled());
verify(mockPersistence).clearPendingLogState();
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* Verify timer. */
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
channel.setEnabled(true);
/* Verify that we have called sendAsync on the ingestion n+1 times total: 1 failure before re-enabling, n success after. */
verify(mockIngestion, times(logNumber + 1)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence n times. */
verify(mockPersistence, times(logNumber)).deleteLogs(any(String.class), any(String.class));
/* Verify timer. */
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
Aggregations