use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-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);
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
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);
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();
verify(mockPersistenceAsync).waitForCurrentTasksToComplete(DefaultChannel.SHUTDOWN_TIMEOUT);
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileCounting.
@Test
public void disabledWhileCounting() throws Exception {
/* Set up mocking. */
final Semaphore beforeCallSemaphore = new Semaphore(0);
final Semaphore afterCallSemaphore = new Semaphore(0);
Persistence mockPersistence = mock(Persistence.class);
DatabasePersistenceAsync mockPersistenceAsync = mock(DatabasePersistenceAsync.class);
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() {
/* Wait for disable call to be done. */
beforeCallSemaphore.acquireUninterruptibly();
/* Return the count. */
DatabasePersistenceAsync.DatabasePersistenceAsyncCallback callback = (DatabasePersistenceAsync.DatabasePersistenceAsyncCallback) invocation.getArguments()[1];
callback.onSuccess(1);
/* Mark call as done. */
afterCallSemaphore.release();
}
}.start();
return null;
}
}).when(mockPersistenceAsync).countLogs(anyString(), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
/* Simulate enable module then disable before persistence can return results. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mock(Ingestion.class));
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mock(Channel.GroupListener.class));
channel.setEnabled(false);
channel.setEnabled(true);
/* We wait on this object to make persistence return results after the groups add/remove calls. */
beforeCallSemaphore.release();
/* We wait on database call to return to verify behavior. */
afterCallSemaphore.acquireUninterruptibly();
/* Verify we didn't get logs after counting since group was removed. */
verify(mockPersistenceAsync, never()).getLogs(eq(TEST_GROUP), eq(1), anyListOf(Log.class), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileEnqueuing.
@Test
public void disabledWhileEnqueuing() 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(0).thenReturn(1).thenReturn(0);
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));
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).putLog(anyString(), any(Log.class), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
IngestionHttp mockIngestion = mock(IngestionHttp.class);
/* Simulate enable module then disable. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
Channel.GroupListener listener = mock(Channel.GroupListener.class);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, listener);
channel.enqueue(mock(Log.class), TEST_GROUP);
channel.setEnabled(false);
channel.setEnabled(true);
/* Release call to mock ingestion. */
beforeCallSemaphore.release();
/* Wait for callback ingestion. */
afterCallSemaphore.acquireUninterruptibly();
/* Verify ingestion not sent. */
verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method removeGroupWhileCounting.
@Test
public void removeGroupWhileCounting() throws Exception {
/* Set up mocking. */
final Semaphore beforeCallSemaphore = new Semaphore(0);
final Semaphore afterCallSemaphore = new Semaphore(0);
Persistence mockPersistence = mock(Persistence.class);
DatabasePersistenceAsync mockPersistenceAsync = mock(DatabasePersistenceAsync.class);
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() {
/* Wait for add/remove calls to be done. */
beforeCallSemaphore.acquireUninterruptibly();
/* Return the count. */
DatabasePersistenceAsync.DatabasePersistenceAsyncCallback callback = (DatabasePersistenceAsync.DatabasePersistenceAsyncCallback) invocation.getArguments()[1];
callback.onSuccess(1);
/* Mark call as done. */
afterCallSemaphore.release();
}
}.start();
return null;
}
}).when(mockPersistenceAsync).countLogs(anyString(), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
/* Simulate enable module then disable before persistence can return results. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mock(Ingestion.class));
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mock(Channel.GroupListener.class));
channel.removeGroup(TEST_GROUP);
/* We wait on this object to make persistence return results after the groups add/remove calls. */
beforeCallSemaphore.release();
/* We wait on database call to return to verify behavior. */
afterCallSemaphore.acquireUninterruptibly();
/* Verify we didn't get logs after counting since group was removed. */
verify(mockPersistenceAsync, never()).getLogs(eq(TEST_GROUP), eq(1), anyListOf(Log.class), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
}
use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionSuccess.
@Test
public void disabledWhileHandlingIngestionSuccess() 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));
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
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]).onCallSucceeded("");
afterCallSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
/* Simulate enable module then disable. */
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
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 success was ignored. */
verify(listener, never()).onSuccess(any(Log.class));
verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {
@Override
public boolean matches(Object argument) {
return argument instanceof CancellationException;
}
}));
}
Aggregations