Search in sources :

Example 11 with AppCenterIngestion

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

the class DefaultChannelTest method checkPendingLogsSendsAllBatchesIfTimerIsOver.

@Test
public void checkPendingLogsSendsAllBatchesIfTimerIsOver() {
    /* Mock current time. */
    long now = 5000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Mock stored start time. */
    long startTimer = 1000;
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTimer);
    /* Mock persistence. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(0));
    /* Mock sending logs. */
    final List<ServiceCallback> callbacks = new ArrayList<>();
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    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;
        }
    });
    /* Create channel and group. */
    DefaultChannel channel = spy(new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler));
    channel.addGroup(TEST_GROUP, 50, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, null, mock(Channel.GroupListener.class));
    /* Prepare to mock timer. */
    long timeDelay = CUSTOM_INTERVAL - (now - startTimer);
    /* Enqueuing 200 events. */
    for (int i = 0; i < 200; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    /* Check invoke the timer with custom timestamp. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    verify(mAppCenterHandler).postDelayed(delayedRunnable.capture(), eq(timeDelay));
    /* Wait for timer. */
    now = 12000;
    when(System.currentTimeMillis()).thenReturn(now);
    delayedRunnable.getValue().run();
    /* Check sending the logs batches. */
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Successful finish one of sending the log. */
    callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Check rest logs sending. */
    verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : 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 12 with AppCenterIngestion

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

the class DefaultChannelPauseResumeTest method pauseResumeGroup.

@Test
public void pauseResumeGroup() throws Persistence.PersistenceException {
    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(), anyListOf(Log.class))).then(getGetLogsAnswer(50));
    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);
    assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
    /* Pause group. */
    channel.pauseGroup(TEST_GROUP, null);
    assertTrue(channel.getGroupState(TEST_GROUP).mPaused);
    /* Enqueue a log. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* 50 logs are persisted but never being sent to Ingestion. */
    assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(Flags.NORMAL));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, never()).onBeforeSending(any(Log.class));
    verify(mockListener, never()).onSuccess(any(Log.class));
    /* The counter should still be 50 now as we did NOT send data. */
    assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Resume group. */
    channel.resumeGroup(TEST_GROUP, null);
    assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
    /* Verify channel starts sending logs. */
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(50)).onBeforeSending(any(Log.class));
    verify(mockListener, times(50)).onSuccess(any(Log.class));
    /* 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) Matchers.anyString(org.mockito.Matchers.anyString) 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)

Example 13 with AppCenterIngestion

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

the class DefaultChannelPauseResumeTest method pauseGroupPauseTargetResumeGroupResumeTarget.

@Test
public void pauseGroupPauseTargetResumeGroupResumeTarget() throws Persistence.PersistenceException {
    /* Mock database and ingestion. */
    Persistence persistence = mock(Persistence.class);
    OneCollectorIngestion ingestion = mock(OneCollectorIngestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    AppCenterIngestion appCenterIngestion = mock(AppCenterIngestion.class);
    when(appCenterIngestion.isEnabled()).thenReturn(true);
    /* Create a channel with a log group that send logs 1 by 1. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, appCenterIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, ingestion, null);
    /* Pause group first. */
    channel.pauseGroup(TEST_GROUP, null);
    /* Pause token. */
    String targetToken = "iKey-apiKey";
    channel.pauseGroup(TEST_GROUP, targetToken);
    /* Mock the database to return logs now. */
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    /* Enqueue a log. */
    Log log = mock(Log.class);
    when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton(targetToken));
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    /* Verify persisted but not incrementing and checking logs. */
    verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Resume group should not send the log. */
    channel.resumeGroup(TEST_GROUP, null);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Resume token, send the log now. */
    channel.resumeGroup(TEST_GROUP, targetToken);
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) OneCollectorIngestion(com.microsoft.appcenter.ingestion.OneCollectorIngestion) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with AppCenterIngestion

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

the class DefaultChannelPauseResumeTest method pauseWithCustomIntervalAndResumeBeforeIntervalDue.

@Test
public void pauseWithCustomIntervalAndResumeBeforeIntervalDue() {
    /* Mock current time. */
    long now = 1000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(TEST_GROUP)).thenReturn(0);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mockIngestion, mAppCenterHandler);
    int batchTimeInterval = 10000;
    channel.addGroup(TEST_GROUP, 10, batchTimeInterval, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), anyLong());
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we enqueue a log while being paused. */
    channel.pauseGroup(TEST_GROUP, null);
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that timer does not start but that the current time is saved for future reference. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we resume later (before interval is due) */
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(now);
    now = 3000;
    long expectedTimeToWait = 8000;
    when(System.currentTimeMillis()).thenReturn(now);
    channel.resumeGroup(TEST_GROUP, null);
    /* Check timer is started with remaining time. */
    verify(mAppCenterHandler).postDelayed(notNull(Runnable.class), eq(expectedTimeToWait));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) 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)

Example 15 with AppCenterIngestion

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

the class DefaultChannelPauseResumeTest method pauseWithCustomIntervalAndResumeAfterIntervalDue.

@Test
public void pauseWithCustomIntervalAndResumeAfterIntervalDue() {
    /* Mock current time. */
    long now = 1000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(TEST_GROUP)).thenReturn(0);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mockIngestion, mAppCenterHandler);
    int batchTimeInterval = 10000;
    channel.addGroup(TEST_GROUP, 10, batchTimeInterval, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), anyLong());
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we enqueue a log while being paused. */
    channel.pauseGroup(TEST_GROUP, null);
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that timer does not start but that the current time is saved for future reference. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we resume later (after interval is due) */
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(now);
    when(System.currentTimeMillis()).thenReturn(now + batchTimeInterval + 1);
    channel.resumeGroup(TEST_GROUP, null);
    /* Check timer is not started and logs send immediately. */
    verify(mAppCenterHandler, never()).postDelayed(notNull(Runnable.class), anyLong());
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) 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