use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method checkPendingLogsResumesStartTime.
@Test
public void checkPendingLogsResumesStartTime() {
/* Mock stored start time. */
long startTime = 1000;
when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
/* Mock current time - before end of interval. */
long now = 5000;
when(System.currentTimeMillis()).thenReturn(now);
/* 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));
/* Do not replace start timer value. */
verifyStatic(never());
SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
/* Start timer for remaining time. */
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL - (now - startTime)));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method lessLogsThanExpected.
@Test
public void lessLogsThanExpected() {
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(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40)).then(getGetLogsAnswer(0));
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, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Enqueuing 49 events. */
for (int i = 1; i <= 49; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
assertEquals(i, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* Enqueue another event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
/* Wait for timer. */
delayedRunnable.getValue().run();
/* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-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);
DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, 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, Flags.DEFAULTS);
verify(listener).onPreparingLog(log, TEST_GROUP);
verify(listener, never()).shouldFilter(log);
verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method checkPendingLogsTriggersIngestionIfTimerIsOver.
@Test
public void checkPendingLogsTriggersIngestionIfTimerIsOver() {
/* Mock current time. */
long now = 50000;
when(System.currentTimeMillis()).thenReturn(now);
/* Mock stored start time. */
long startTime = 1000;
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);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(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));
/* Do not start the timer. */
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
/* Check sending the logs batches. */
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.
@Test
public void invokeCallbacksAfterSuspendFatal() {
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(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.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(new HttpResponse(403))));
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);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* 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