Search in sources :

Example 21 with AppCenterIngestion

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

the class DefaultChannelTest method analyticsSuccess.

@Test
public void analyticsSuccess() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(2));
    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);
    /* 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));
    /* The counter should be 0 as we reset the counter after reaching the limit of 50. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Verify that 5 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 we have called deleteLogs on the Persistence. */
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. */
    verify(mockListener, times(50)).onBeforeSending(any(Log.class));
    /* Verify that we have called onSuccess in the listener. */
    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);
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* Schedule only 1 log after that. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    assertEquals(1, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockPersistence, times(51)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    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)).onSuccess(any(Log.class));
    /* Simulate the timer. */
    delayedRunnable.getValue().run();
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(51)).onSuccess(any(Log.class));
    /* 2 more timed logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    assertEquals(2, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockPersistence, times(53)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(51)).onSuccess(any(Log.class));
    /* Simulate the timer. */
    delayedRunnable.getValue().run();
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(3)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(53)).onSuccess(any(Log.class));
    /* Check total timers. */
    verify(mAppCenterHandler, times(3)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Check channel clear. */
    channel.clear(TEST_GROUP);
    verify(mockPersistence).deleteLogs(eq(TEST_GROUP));
}
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 22 with AppCenterIngestion

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.

@Test
public void invokeCallbacksAfterSuspendFatalNoListener() {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    /* Simulate a lot of logs already in database. */
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.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(new HttpResponse(403))));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null, null);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
    /* Enqueuing 2 events. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* 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);
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) HttpException(com.microsoft.appcenter.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString) HttpException(com.microsoft.appcenter.http.HttpException) SocketException(java.net.SocketException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Test(org.junit.Test)

Example 23 with AppCenterIngestion

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

the class DefaultChannelTest method checkPendingLogsStoresStartTime.

@Test
public void checkPendingLogsStoresStartTime() {
    /* Mock current time. */
    long now = 1;
    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));
    /* Verify that timer starts and current time is saved into preferences. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 24 with AppCenterIngestion

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

the class DefaultChannelTest method checkPendingLogsDoesNotStartTimerWithoutLogs.

@Test
public void checkPendingLogsDoesNotStartTimerWithoutLogs() {
    /* Mock current time. */
    long now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.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(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that timer isn't started. */
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 25 with AppCenterIngestion

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

the class DefaultChannelTest method setNetworkRequest.

@Test
public void setNetworkRequest() {
    /* Mock current time - before end of interval. */
    long now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    /* Disallow network requests. */
    when(mockIngestion.isEnabled()).thenReturn(false);
    channel.setNetworkRequests(false);
    channel.addGroup(TEST_GROUP, 3, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that sending logs wasn't called. */
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enable network requests and verify that sending logs was called. */
    when(mockIngestion.isEnabled()).thenReturn(true);
    channel.setNetworkRequests(true);
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Disallow network requests and verify that sending logs wasn't called. */
    when(mockIngestion.isEnabled()).thenReturn(false);
    channel.setNetworkRequests(false);
    channel.addGroup(TEST_GROUP, 3, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Allow network requests and verify that sending logs was called again. */
    when(mockIngestion.isEnabled()).thenReturn(true);
    channel.setNetworkRequests(true);
    verify(mockIngestion, times(2)).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) Matchers.anyString(org.mockito.Matchers.anyString) 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