Search in sources :

Example 1 with AppCenterIngestion

use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method maxRequests.

@Test
public void maxRequests() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    /* We make second request return less logs than expected to make sure counter is reset properly. */
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(49)).then(getGetLogsAnswer()).then(getGetLogsAnswer()).then(getGetLogsAnswer(0));
    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) {
            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), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* 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, Flags.DEFAULTS);
    }
    verify(mAppCenterHandler, times(4)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, times(4)).removeCallbacks(any(Runnable.class));
    /* Verify all logs stored, N requests sent, not log deleted yet. */
    verify(mockPersistence, times(200)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    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(new HttpResponse(200, ""));
    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(new HttpResponse(200, ""));
    }
    verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
    /* Wait for timer. */
    delayedRunnable.getValue().run();
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with AppCenterIngestion

use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method maxRequestsInitial.

@Test
public void maxRequestsInitial() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockPersistence.countLogs(any(String.class))).thenReturn(100);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).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) {
            Object[] args = invocation.getArguments();
            if (args[3] instanceof ServiceCallback) {
                callbacks.add((ServiceCallback) invocation.getArguments()[3]);
            }
            return null;
        }
    });
    when(mockIngestion.isEnabled()).thenReturn(true);
    /* Init channel with mocks. */
    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, null);
    /* Enqueue enough logs to be split in N + 1 maximum requests. */
    for (int i = 0; i < 100; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    /* Verify all logs stored, N requests sent, not log deleted yet. */
    verify(mockPersistence, times(100)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    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(new HttpResponse(200, ""));
    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(new HttpResponse(200, ""));
    verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Only 2 batches after channel start (non initial logs), verify timer interactions. */
    verify(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, times(2)).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with AppCenterIngestion

use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method analyticsFatal.

@Test
public void analyticsFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(20));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(new HttpResponse(403)))).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, null);
    /* Enqueuing 50 events. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Verify that 50 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    /* Verify that we have called sendAsync on the ingestion. */
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that the Channel is disabled. */
    assertFalse(channel.isEnabled());
    /* Verify that we have cleared the logs. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
    /* Verify counter. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    /* The counter should still be 0 as logs are discarded by channel now. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* No more timer yet at this point. */
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* Enable channel to see if it can work again after that error state. */
    channel.setEnabled(true);
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    assertEquals(20, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Wait for timer. */
    delayedRunnable.getValue().run();
    /* The counter should back to 0 now. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Verify that we have called sendAsync on the ingestion 2 times total: 1 earlier failure then 1 success. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence for the successful batch after re-enabling. */
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Verify 1 more timer call. */
    verify(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Verify no more cancel timer. */
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpException(com.microsoft.appcenter.http.HttpException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with AppCenterIngestion

use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method checkPendingLogsResumesStartTime.

@Test
public void checkPendingLogsResumesStartTime() {
    /* Mock stored start time. */
    long startTime = 1000;
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
    /* Mock current time - before end of interval. */
    long now = 5000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Do not replace start timer value. */
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
    /* Start timer for remaining time. */
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL - (now - startTime)));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 5 with AppCenterIngestion

use of com.microsoft.appcenter.ingestion.AppCenterIngestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method lessLogsThanExpected.

@Test
public void lessLogsThanExpected() {
    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(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40)).then(getGetLogsAnswer(0));
    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);
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* Enqueuing 49 events. */
    for (int i = 1; i <= 49; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
        assertEquals(i, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    }
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Enqueue another event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Wait for timer. */
    delayedRunnable.getValue().run();
    /* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)25 Persistence (com.microsoft.appcenter.persistence.Persistence)25 Test (org.junit.Test)25 Context (android.content.Context)24 Log (com.microsoft.appcenter.ingestion.models.Log)21 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)19 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)19 UUID (java.util.UUID)19 Matchers.anyString (org.mockito.Matchers.anyString)18 HttpResponse (com.microsoft.appcenter.http.HttpResponse)8 CancellationException (com.microsoft.appcenter.CancellationException)6 IOException (java.io.IOException)6 HttpException (com.microsoft.appcenter.http.HttpException)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 SocketException (java.net.SocketException)4 ArrayList (java.util.ArrayList)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)2 OneCollectorIngestion (com.microsoft.appcenter.ingestion.OneCollectorIngestion)2 Semaphore (java.util.concurrent.Semaphore)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2