use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultLogSerializer method readLog.
@NonNull
private Log readLog(JSONObject object) throws JSONException {
String type = object.getString(TYPE);
LogFactory logFactory = mLogFactories.get(type);
if (logFactory == null) {
throw new JSONException("Unknown log type: " + type);
}
Log log = logFactory.create();
log.read(object);
return log;
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method filter.
@Test
public void filter() throws Persistence.PersistenceException {
/* Given a mock channel. */
Persistence persistence = mock(Persistence.class);
@SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class), mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* Given we add mock listeners. */
Channel.Listener listener1 = mock(Channel.Listener.class);
channel.addListener(listener1);
Channel.Listener listener2 = mock(Channel.Listener.class);
channel.addListener(listener2);
/* Given 1 log. */
{
/* Given the second listener filtering out logs. */
Log log = mock(Log.class);
when(listener2.shouldFilter(log)).thenReturn(true);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
verify(listener2).shouldFilter(log);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
/* Given 1 log. */
{
/* Given the first listener filtering out logs. */
Log log = mock(Log.class);
when(listener1.shouldFilter(log)).thenReturn(true);
when(listener2.shouldFilter(log)).thenReturn(false);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
/* Second listener skipped since first listener filtered out. */
verify(listener2, never()).shouldFilter(log);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
/* Given 1 log. */
{
/* Given no listener filtering out logs. */
Log log = mock(Log.class);
when(listener1.shouldFilter(log)).thenReturn(false);
when(listener2.shouldFilter(log)).thenReturn(false);
/* When we enqueue that log. */
channel.enqueue(log, TEST_GROUP);
/* Then except the following. behaviors. */
verify(listener1).onEnqueuingLog(log, TEST_GROUP);
verify(listener1).shouldFilter(log);
verify(listener2).onEnqueuingLog(log, TEST_GROUP);
verify(listener2).shouldFilter(log);
verify(persistence).putLog(TEST_GROUP, log);
}
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method maxRequests.
@Test
@SuppressWarnings("unchecked")
public void maxRequests() throws Persistence.PersistenceException {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
/* We make second request return less logs than expected to make sure counter is reset properly. */
when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(49)).then(getGetLogsAnswer());
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) throws Throwable {
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), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* 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);
}
verify(mHandler, times(4)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
verify(mHandler, times(4)).removeCallbacks(any(Runnable.class));
/* Verify all logs stored, N requests sent, not log deleted yet. */
verify(mockPersistence, times(200)).putLog(eq(TEST_GROUP), any(Log.class));
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("");
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("");
verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
/* The counter should be 0 now as we sent data. */
assertEquals(0, channel.getCounter(TEST_GROUP));
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method lessLogsThanExpected.
@Test
public void lessLogsThanExpected() {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40));
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, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 49 events. */
for (int i = 1; i <= 49; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP);
assertEquals(i, channel.getCounter(TEST_GROUP));
}
verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
/* Enqueue another event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
verify(mHandler).removeCallbacks(any(Runnable.class));
/* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
assertEquals(0, channel.getCounter(TEST_GROUP));
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.
@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendRecoverable() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(503)));
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);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
/* Verify callbacks invoked only for the first log. */
verify(mockListener).onBeforeSending(any(Log.class));
/* Verify no failure forwarded. */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify no log was deleted. */
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* But that we cleared batch state. */
verify(mockPersistence).clearPendingLogState();
}
Aggregations