use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invalidGroup.
@Test
public void invalidGroup() throws Persistence.PersistenceException {
Persistence persistence = mock(Persistence.class);
Channel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, mock(Ingestion.class));
/* Enqueue a log before group is registered = failure. */
Log log = mock(Log.class);
channel.enqueue(log, TEST_GROUP);
verify(log, never()).setDevice(any(Device.class));
verify(log, never()).setToffset(anyLong());
verify(persistence, never()).putLog(TEST_GROUP, log);
/* Trying remove group that not registered. */
channel.removeGroup(TEST_GROUP);
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method errorLogSuccess.
@Test
@SuppressWarnings("unchecked")
public void errorLogSuccess() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
Ingestion mockIngestion = mock(Ingestion.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer());
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer());
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 2 error logs. */
channel.enqueue(mock(Log.class), TEST_GROUP);
channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify that 2 items have been persisted. */
verify(mockPersistence, times(2)).putLog(eq(TEST_GROUP), any(Log.class));
/* Verify that we have called sendAsync on the ingestion twice as batch size is 1. */
verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Verify that we have called deleteLogs on the Persistence. */
verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
/* Verify that we have called onBeforeSending in the listener. */
verify(mockListener, times(2)).onBeforeSending(any(Log.class));
/* Verify that we have called onSuccess in the listener. */
verify(mockListener, times(2)).onSuccess(any(Log.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getCounter(TEST_GROUP));
/* Verify timer. */
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method enableWhileDisabling.
@Test
public void enableWhileDisabling() 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(), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
doAnswer(new Answer<Object>() {
@Override
public Object answer(final InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
beforeCallSemaphore.acquireUninterruptibly();
try {
invocation.callRealMethod();
} catch (Throwable e) {
throw new RuntimeException(e);
}
afterCallSemaphore.release();
}
}.start();
return null;
}
}).when(mockPersistenceAsync).getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
/* Simulate enable module then disable. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mock(IngestionHttp.class));
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);
/* Unblock call to getLogs. */
beforeCallSemaphore.release();
/* Wait call to getLogs. */
afterCallSemaphore.acquireUninterruptibly();
/* Check canceled before we could delete logs. */
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method disableBeforeCheckingPendingLogs.
@Test
@SuppressWarnings("unchecked")
public void disableBeforeCheckingPendingLogs() throws IOException {
Ingestion ingestion = mock(Ingestion.class);
Persistence persistence = mock(Persistence.class);
final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
when(ingestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
/* Simulate a service disabled in the middle of network transaction. */
ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
channel.removeGroup(TEST_GROUP);
callback.onCallSucceeded("");
return null;
}
});
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
channel.enqueue(mock(Log.class), TEST_GROUP);
verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* checkPendingLogs is getting called twice from triggerIngestion and a callback for ingestion.
It can be failed because of timing issue so checking at least once instead. */
verifyStatic(atLeastOnce());
MobileCenterLog.info(eq(MobileCenter.LOG_TAG), anyString());
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-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);
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));
}
Aggregations