use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-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), mCoreHandler);
/* 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()).setTimestamp(any(Date.class));
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.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method suspendWithoutFailureCallback.
@Test
@SuppressWarnings("unchecked")
public void suspendWithoutFailureCallback() {
Ingestion mockIngestion = mock(Ingestion.class);
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.countLogs(anyString())).thenReturn(3);
when(mockPersistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
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 SocketException()));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
assertFalse(channel.isEnabled());
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method initialLogs.
@Test
@SuppressWarnings("unchecked")
public void initialLogs() throws IOException, InterruptedException {
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(3);
when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(3));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
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(3, channel.getCounter(TEST_GROUP));
assertNotNull(runnable.get());
runnable.get().run();
verify(ingestion).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));
}
use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-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, mCoreHandler);
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.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.
@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatalNoListener() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
/* Simulate a lot of logs already in database. */
when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
/* Make first call hang, and the second call return a fatal error. */
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenReturn(null).then(getSendAsyncAnswer(new HttpException(403)));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 2 events. */
channel.enqueue(mock(Log.class), TEST_GROUP);
channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify callbacks not invoked. */
verify(mockListener, never()).onBeforeSending(any(Log.class));
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify logs were deleted. */
verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Aggregations