Search in sources :

Example 11 with IngestionHttp

use of com.microsoft.appcenter.ingestion.IngestionHttp in project mobile-center-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);
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpException(com.microsoft.appcenter.http.HttpException) HttpException(com.microsoft.appcenter.http.HttpException) SocketException(java.net.SocketException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Test(org.junit.Test)

Example 12 with IngestionHttp

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

the class DefaultChannelTest method shutdown.

@Test
public void shutdown() throws Exception {
    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.<List<Log>>any())).then(getGetLogsAnswer(1));
    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 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mockListener).onBeforeSending(notNull(Log.class));
    channel.shutdown();
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    verify(mockPersistence).clearPendingLogState();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) Log(com.microsoft.appcenter.ingestion.models.Log) 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 13 with IngestionHttp

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

the class DefaultChannelTest method analyticsSuccess.

@Test
@SuppressWarnings("unchecked")
public void analyticsSuccess() throws Persistence.PersistenceException, InterruptedException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.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());
    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));
    /* The counter should be 0 as we reset the counter after reaching the limit of 50. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Verify that 5 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    /* 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.getCounter(TEST_GROUP));
    /* Prepare to mock timer. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    /* Schedule only 1 log after that. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertEquals(1, channel.getCounter(TEST_GROUP));
    verify(mockPersistence, times(51)).putLog(eq(TEST_GROUP), any(Log.class));
    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. */
    assertNotNull(runnable.get());
    runnable.get().run();
    runnable.set(null);
    assertEquals(0, channel.getCounter(TEST_GROUP));
    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);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertEquals(2, channel.getCounter(TEST_GROUP));
    verify(mockPersistence, times(53)).putLog(eq(TEST_GROUP), any(Log.class));
    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. */
    assertNotNull(runnable.get());
    runnable.get().run();
    assertEquals(0, channel.getCounter(TEST_GROUP));
    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(mHandler, times(3)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Check channel clear clear */
    channel.clear(TEST_GROUP);
    verify(mockPersistence).deleteLogs(eq(TEST_GROUP));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with IngestionHttp

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

the class DefaultChannelTest method analyticsRecoverable.

@Test
@SuppressWarnings("unchecked")
public void analyticsRecoverable() throws Persistence.PersistenceException, InterruptedException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.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 SocketException())).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 50 events. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Verify that 50 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    /* 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 not called deleteLogs on the Persistence. */
    verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
    /* Verify that the Channel is disabled. */
    assertFalse(channel.isEnabled());
    verify(mockPersistence).clearPendingLogState();
    verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    /* The counter keeps being increased. */
    assertEquals(70, channel.getCounter(TEST_GROUP));
    /* Prepare to mock timer. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    /* Enable channel. */
    channel.setEnabled(true);
    /* Upon enabling, 1st batch of 50 is sent immediately, 20 logs are remaining. */
    assertEquals(20, channel.getCounter(TEST_GROUP));
    /* Wait for timer. */
    assertNotNull(runnable.get());
    runnable.get().run();
    /* The counter should be 0 after the second batch. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Verify that we have called sendAsync on the ingestion 3 times total. */
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence (2 successful batches, the first call was a recoverable failure). */
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. getLogs will return 50, 50 and 20. */
    verify(mockListener, times(120)).onBeforeSending(any(Log.class));
    /* Intermediate failures never forwarded to listener, only final success */
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    verify(mockListener, times(70)).onSuccess(any(Log.class));
    /* Verify timer. */
    verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) 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) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Context (android.content.Context)14 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)14 Log (com.microsoft.appcenter.ingestion.models.Log)14 Persistence (com.microsoft.appcenter.persistence.Persistence)14 Test (org.junit.Test)14 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)12 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)12 UUID (java.util.UUID)12 CancellationException (com.microsoft.appcenter.CancellationException)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Matchers.anyString (org.mockito.Matchers.anyString)7 HttpException (com.microsoft.appcenter.http.HttpException)6 SocketException (java.net.SocketException)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Semaphore (java.util.concurrent.Semaphore)3 ServiceCall (com.microsoft.appcenter.http.ServiceCall)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 Answer (org.mockito.stubbing.Answer)1 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)1