use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelOtherOperationsTest method shutdown.
@Test
public void shutdown() {
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(any(String.class), anyListOf(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
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 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
verify(mockListener).onBeforeSending(notNull(Log.class));
channel.shutdown();
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
verify(mockPersistence).clearPendingLogState();
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.
@Test(timeout = 5000)
public void disabledWhileHandlingIngestionFailure() {
/* Set up mocking. */
final Semaphore beforeCallSemaphore = new Semaphore(0);
final Semaphore afterCallSemaphore = new Semaphore(0);
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.countLogs(anyString())).thenReturn(1);
when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
final Exception mockException = new IOException();
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
@Override
public Object answer(final InvocationOnMock invocation) {
new Thread() {
@Override
public void run() {
beforeCallSemaphore.acquireUninterruptibly();
((ServiceCallback) invocation.getArguments()[3]).onCallFailed(mockException);
afterCallSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
/* Simulate enable module then disable. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
Channel.GroupListener listener = mock(Channel.GroupListener.class);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, listener);
channel.setEnabled(false);
channel.setEnabled(true);
/* Release call to mock ingestion. */
beforeCallSemaphore.release();
/* Wait for callback ingestion. */
afterCallSemaphore.acquireUninterruptibly();
/* Verify handling error was ignored. */
verify(listener, never()).onFailure(any(Log.class), eq(mockException));
verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {
@Override
public boolean matches(Object argument) {
return argument instanceof CancellationException;
}
}));
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method analyticsRecoverable.
@Test
public void analyticsRecoverable() throws Persistence.PersistenceException {
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(), 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 SocketException())).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);
/* 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 we have not called deleteLogs on the Persistence. */
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Verify that the Channel is disabled. */
assertFalse(channel.isEnabled());
verify(mockPersistence).clearPendingLogState();
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* Enqueuing 20 more events. */
for (int i = 0; i < 20; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
/* The counter keeps being increased. */
assertEquals(70, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Enable channel. */
channel.setEnabled(true);
/* Upon enabling, 1st batch of 50 is sent immediately, 20 logs are remaining. */
assertEquals(20, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Wait for timer. */
delayedRunnable.getValue().run();
/* The counter should be 0 after the second batch. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Verify that we have called sendAsync on the ingestion 3 times total. */
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence (2 successful batches, the first call was a recoverable failure). */
verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. getLogs will return 50, 50 and 20. */
verify(mockListener, times(120)).onBeforeSending(any(Log.class));
/* Intermediate failures never forwarded to listener, only final success */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
verify(mockListener, times(70)).onSuccess(any(Log.class));
/* Verify timer. */
verify(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
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 checkPendingLogsReplacesInvalidStartTime.
@Test
public void checkPendingLogsReplacesInvalidStartTime() {
/* Mock current time. */
long now = 1000;
when(System.currentTimeMillis()).thenReturn(now);
/* Mock stored start time. */
long startTime = 10000;
when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
/* 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));
/* Verify that start time is replaced. */
verifyStatic();
SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
/* Start timer for whole interval. */
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method enqueuePersistenceFailure.
@Test
public void enqueuePersistenceFailure() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
/* Simulate Persistence failing. */
doThrow(new Persistence.PersistenceException("mock", new IOException("mock"))).when(mockPersistence).putLog(any(Log.class), anyString(), anyInt());
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, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
channel.addGroup(TEST_GROUP + "2", 10, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Verify no request is sent if Persistence fails. */
for (int i = 0; i < 10; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
channel.enqueue(mock(Log.class), TEST_GROUP + "2", Flags.DEFAULTS);
}
verify(mockPersistence, times(10)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
verify(mockPersistence, times(10)).putLog(any(Log.class), eq(TEST_GROUP + "2"), eq(NORMAL));
verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
verify(mockListener, times(10)).onBeforeSending(any(Log.class));
verify(mockListener, times(10)).onFailure(any(Log.class), any(Persistence.PersistenceException.class));
}
Aggregations