use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method suspendWithFailureCallback.
@Test
@SuppressWarnings("unchecked")
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(), anyInt(), anyList())).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) throws Throwable {
return null;
}
}).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).then(getSendAsyncAnswer(new HttpException(404)));
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);
/* 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 AppCenter-SDK-Android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.
@Test
public void disabledWhileHandlingIngestionFailure() throws Exception {
/* 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(), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(mockPersistence.getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
IngestionHttp mockIngestion = mock(IngestionHttp.class);
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) throws Throwable {
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), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
Channel.GroupListener listener = mock(Channel.GroupListener.class);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannel method buildDefaultPersistence.
/**
* Init Persistence for default constructor.
*/
private static Persistence buildDefaultPersistence(@NonNull Context context, @NonNull LogSerializer logSerializer) {
Persistence persistence = new DatabasePersistence(context);
persistence.setLogSerializer(logSerializer);
return persistence;
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelPauseResumeTest method pauseResumeGroup.
@Test
public void pauseResumeGroup() 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));
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);
assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
/* Pause group. */
channel.pauseGroup(TEST_GROUP, null);
assertTrue(channel.getGroupState(TEST_GROUP).mPaused);
/* Enqueue a log. */
for (int i = 0; i < 50; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* 50 logs are persisted but never being sent to Ingestion. */
assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(Flags.NORMAL));
verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
verify(mockListener, never()).onBeforeSending(any(Log.class));
verify(mockListener, never()).onSuccess(any(Log.class));
/* The counter should still be 50 now as we did NOT send data. */
assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
/* Resume group. */
channel.resumeGroup(TEST_GROUP, null);
assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
/* Verify channel starts sending logs. */
verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
verify(mockListener, times(50)).onBeforeSending(any(Log.class));
verify(mockListener, times(50)).onSuccess(any(Log.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelPauseResumeTest method pauseGroupPauseTargetResumeGroupResumeTarget.
@Test
public void pauseGroupPauseTargetResumeGroupResumeTarget() throws Persistence.PersistenceException {
/* Mock database and ingestion. */
Persistence persistence = mock(Persistence.class);
OneCollectorIngestion ingestion = mock(OneCollectorIngestion.class);
when(ingestion.isEnabled()).thenReturn(true);
AppCenterIngestion appCenterIngestion = mock(AppCenterIngestion.class);
when(appCenterIngestion.isEnabled()).thenReturn(true);
/* Create a channel with a log group that send logs 1 by 1. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, appCenterIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, ingestion, null);
/* Pause group first. */
channel.pauseGroup(TEST_GROUP, null);
/* Pause token. */
String targetToken = "iKey-apiKey";
channel.pauseGroup(TEST_GROUP, targetToken);
/* Mock the database to return logs now. */
when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
/* Enqueue a log. */
Log log = mock(Log.class);
when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton(targetToken));
channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
/* Verify persisted but not incrementing and checking logs. */
verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Resume group should not send the log. */
channel.resumeGroup(TEST_GROUP, null);
verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Resume token, send the log now. */
channel.resumeGroup(TEST_GROUP, targetToken);
verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Aggregations