use of com.microsoft.appcenter.ingestion.Ingestion 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.ingestion.Ingestion 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.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method suspendWithoutFailureCallback.
@Test
public void suspendWithoutFailureCallback() {
Ingestion mockIngestion = mock(Ingestion.class);
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.countLogs(anyString())).thenReturn(3);
when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) {
return null;
}
}).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) {
return null;
}
}).then(getSendAsyncAnswer(new SocketException()));
when(mockIngestion.isEnabled()).thenReturn(true);
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, null);
assertFalse(channel.isEnabled());
}
use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method errorLogSuccess.
@Test
public void errorLogSuccess() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
Ingestion mockIngestion = mock(Ingestion.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer());
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, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Enqueuing 2 error logs. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* Verify that 2 items have been persisted. */
verify(mockPersistence, times(2)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
/* Verify that we have called sendAsync on the ingestion twice as batch size is 1. */
verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence. */
verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. */
verify(mockListener, times(2)).onBeforeSending(any(Log.class));
/* Verify that we have called onSuccess in the listener. */
verify(mockListener, times(2)).onSuccess(any(Log.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Verify timer. */
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannel method setEnabled.
/**
* Set the enabled flag. If false, the channel will continue to persist data but not forward any item to ingestion.
* The most common use-case would be to set it to false and enable sending again after the channel has disabled itself after receiving
* a recoverable error (most likely related to a server issue).
*
* @param enabled flag to enable or disable the channel.
*/
@Override
public void setEnabled(boolean enabled) {
if (mEnabled == enabled) {
return;
}
if (enabled) {
mEnabled = true;
mDiscardLogs = false;
mCurrentState++;
for (Ingestion ingestion : mIngestions) {
ingestion.reopen();
}
for (GroupState groupState : mGroupStates.values()) {
checkPendingLogs(groupState);
}
} else {
mEnabled = false;
suspend(true, new CancellationException());
}
/* Notify listeners that channel state has changed. */
for (Listener listener : mListeners) {
listener.onGloballyEnabled(enabled);
}
}
Aggregations