use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannel method buildDefaultPersistence.
/**
* Init Persistence for default constructor.
*/
private static Persistence buildDefaultPersistence(@NonNull LogSerializer logSerializer) {
Persistence persistence = new DatabasePersistence();
persistence.setLogSerializer(logSerializer);
return persistence;
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method checkPendingLogsSendsAllBatchesIfTimerIsOver.
@Test
public void checkPendingLogsSendsAllBatchesIfTimerIsOver() {
/* Mock current time. */
long now = 5000;
when(System.currentTimeMillis()).thenReturn(now);
/* Mock stored start time. */
long startTimer = 1000;
when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTimer);
/* Mock persistence. */
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(0));
/* Mock sending logs. */
final List<ServiceCallback> callbacks = new ArrayList<>();
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
if (args[3] instanceof ServiceCallback) {
callbacks.add((ServiceCallback) invocation.getArguments()[3]);
}
return null;
}
});
/* Create channel and group. */
DefaultChannel channel = spy(new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler));
channel.addGroup(TEST_GROUP, 50, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, null, mock(Channel.GroupListener.class));
/* Prepare to mock timer. */
long timeDelay = CUSTOM_INTERVAL - (now - startTimer);
/* Enqueuing 200 events. */
for (int i = 0; i < 200; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
/* Check invoke the timer with custom timestamp. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
verify(mAppCenterHandler).postDelayed(delayedRunnable.capture(), eq(timeDelay));
/* Wait for timer. */
now = 12000;
when(System.currentTimeMillis()).thenReturn(now);
delayedRunnable.getValue().run();
/* Check sending the logs batches. */
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Successful finish one of sending the log. */
callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* Check rest logs sending. */
verify(mockIngestion, times(4)).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 suspendWithFailureCallback.
@Test
public void suspendWithFailureCallback() {
Ingestion mockIngestion = mock(Ingestion.class);
Persistence mockPersistence = mock(Persistence.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.countLogs(anyString())).thenReturn(30);
when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(10));
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 HttpException(new HttpResponse(404))));
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);
/* 30 from countLogs and 10 new logs from getLogs. */
verify(mockListener, times(40)).onBeforeSending(any(Log.class));
verify(mockListener, times(40)).onFailure(any(Log.class), any(SocketException.class));
assertFalse(channel.isEnabled());
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method initialLogsThenDisable.
@Test
public void initialLogsThenDisable() 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(3);
when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).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);
assertEquals(3, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
channel.setEnabled(false);
verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
verify(ingestion).close();
verify(persistence).deleteLogs(TEST_GROUP);
delayedRunnable.getValue().run();
verify(ingestion, never()).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 maxRequests.
@Test
public void maxRequests() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
/* We make second request return less logs than expected to make sure counter is reset properly. */
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(49)).then(getGetLogsAnswer()).then(getGetLogsAnswer()).then(getGetLogsAnswer(0));
final List<ServiceCallback> callbacks = new ArrayList<>();
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
if (args[3] instanceof ServiceCallback) {
callbacks.add((ServiceCallback) invocation.getArguments()[3]);
}
return null;
}
});
/* Init channel with mocks. */
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, null);
/* Prepare to mock timer. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
/* Enqueue enough logs to be split in N + 1 maximum requests. */
for (int i = 0; i < 200; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
verify(mAppCenterHandler, times(4)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mAppCenterHandler, times(4)).removeCallbacks(any(Runnable.class));
/* Verify all logs stored, N requests sent, not log deleted yet. */
verify(mockPersistence, times(200)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
/* Make 1 of the call succeed. Verify log deleted. */
callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* The request N+1 is now unlocked. */
verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Unlock all requests and check logs deleted. */
for (int i = 1; i < 4; i++) {
callbacks.get(i).onCallSucceeded(new HttpResponse(200, ""));
}
verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
/* Wait for timer. */
delayedRunnable.getValue().run();
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Aggregations