use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method packageManagerIsBroken.
@Test
public void packageManagerIsBroken() throws Persistence.PersistenceException, DeviceInfoHelper.DeviceInfoException {
/* Setup mocking to make device properties generation fail. */
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
Persistence persistence = mock(Persistence.class);
@SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class), mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
Channel.Listener listener = mock(Channel.Listener.class);
channel.addListener(listener);
/* Enqueue a log: listener is called before but then attaching device properties fails before saving the log. */
Log log = mock(Log.class);
channel.enqueue(log, TEST_GROUP);
verify(listener).onEnqueuingLog(log, TEST_GROUP);
verify(listener, never()).shouldFilter(log);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method somehowDatabaseEmptiedAfterTimer.
@Test
@SuppressWarnings("unchecked")
public void somehowDatabaseEmptiedAfterTimer() throws IOException, InterruptedException {
/* Cover the if (batchId != null) test though it could happen only if the database content disappear after the timer... */
AtomicReference<Runnable> runnable = catchPostRunnable();
Ingestion ingestion = mock(Ingestion.class);
doThrow(new IOException()).when(ingestion).close();
Persistence persistence = mock(Persistence.class);
when(persistence.countLogs(anyString())).thenReturn(2);
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
assertEquals(2, channel.getCounter(TEST_GROUP));
assertNotNull(runnable.get());
runnable.get().run();
verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method filter.
@Test
public void filter() throws Persistence.PersistenceException {
/* Given a mock channel. */
Persistence persistence = mock(Persistence.class);
@SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class), mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* Given we add mock listeners. */
Channel.Listener listener1 = mock(Channel.Listener.class);
channel.addListener(listener1);
Channel.Listener listener2 = mock(Channel.Listener.class);
channel.addListener(listener2);
/* Given 1 log. */
{
/* Given the second listener filtering out logs. */
Log log = mock(Log.class);
when(listener2.shouldFilter(log)).thenReturn(true);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
verify(listener2).shouldFilter(log);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
/* Given 1 log. */
{
/* Given the first listener filtering out logs. */
Log log = mock(Log.class);
when(listener1.shouldFilter(log)).thenReturn(true);
when(listener2.shouldFilter(log)).thenReturn(false);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
/* Second listener skipped since first listener filtered out. */
verify(listener2, never()).shouldFilter(log);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
/* Given 1 log. */
{
/* Given no listener filtering out logs. */
Log log = mock(Log.class);
when(listener1.shouldFilter(log)).thenReturn(false);
when(listener2.shouldFilter(log)).thenReturn(false);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
verify(listener2).shouldFilter(log);
verify(persistence).putLog(TEST_GROUP, log);
}
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method shutdown.
@Test
public void shutdown() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
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();
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method analyticsRecoverable.
@Test
@SuppressWarnings("unchecked")
public void analyticsRecoverable() throws Persistence.PersistenceException, InterruptedException {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.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());
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 50 events. */
for (int i = 0; i < 50; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP);
}
verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler).removeCallbacks(any(Runnable.class));
/* Verify that 50 items have been persisted. */
verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
/* 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);
}
/* The counter keeps being increased. */
assertEquals(70, channel.getCounter(TEST_GROUP));
/* Prepare to mock timer. */
AtomicReference<Runnable> runnable = catchPostRunnable();
/* Enable channel. */
channel.setEnabled(true);
/* Upon enabling, 1st batch of 50 is sent immediately, 20 logs are remaining. */
assertEquals(20, channel.getCounter(TEST_GROUP));
/* Wait for timer. */
assertNotNull(runnable.get());
runnable.get().run();
/* The counter should be 0 after the second batch. */
assertEquals(0, channel.getCounter(TEST_GROUP));
/* 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(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler).removeCallbacks(any(Runnable.class));
}
Aggregations