Search in sources :

Example 16 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelTest method errorLogRecoverable.

@Test
@SuppressWarnings("unchecked")
public void errorLogRecoverable() throws Persistence.PersistenceException, InterruptedException {
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion mockIngestion = mock(Ingestion.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
    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, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing n errors. */
    int logNumber = 5;
    for (int i = 0; i < logNumber; i++) channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify that n items have been persisted. */
    verify(mockPersistence, times(logNumber)).putLog(eq(TEST_GROUP), any(Log.class));
    /* Verify that we have called sendAsync on the ingestion once for the first item, but not more than that. */
    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 we have called onBeforeSending in the listener. */
    verify(mockListener).onBeforeSending(any(Log.class));
    /* Verify that we have not called the failure listener. It's a transient exception that will be retried later when the channel is re-enabled. */
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    /* Verify that the Channel is disabled. */
    assertFalse(channel.isEnabled());
    verify(mockPersistence).clearPendingLogState();
    verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
    /* Verify timer. */
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, never()).removeCallbacks(any(Runnable.class));
    channel.setEnabled(true);
    /* Verify that we have called sendAsync on the ingestion n+1 times total: 1 failure before re-enabling, n success after. */
    verify(mockIngestion, times(logNumber + 1)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence n times. */
    verify(mockPersistence, times(logNumber)).deleteLogs(any(String.class), any(String.class));
    /* Verify timer. */
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, never()).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) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Persistence(com.microsoft.appcenter.persistence.Persistence) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 17 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelTest method disableBeforeCheckingPendingLogs.

@Test
@SuppressWarnings("unchecked")
public void disableBeforeCheckingPendingLogs() throws IOException {
    Ingestion ingestion = mock(Ingestion.class);
    Persistence persistence = mock(Persistence.class);
    final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
    when(ingestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            /* Simulate a service disabled in the middle of network transaction. */
            ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
            channel.removeGroup(TEST_GROUP);
            callback.onCallSucceeded("");
            return null;
        }
    });
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Persistence(com.microsoft.appcenter.persistence.Persistence) 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 18 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelTest method setEnabled.

@Test
@SuppressWarnings("unchecked")
public void setEnabled() throws IOException, InterruptedException {
    /* Send a log. */
    Ingestion ingestion = mock(Ingestion.class);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Disable before timer is triggered. */
    channel.setEnabled(false);
    verify(mHandler).removeCallbacks(any(Runnable.class));
    verify(ingestion).close();
    verify(persistence).deleteLogs(TEST_GROUP);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enable and send a new log. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    channel.setEnabled(true);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion).reopen();
    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) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) IOException(java.io.IOException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 19 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelTest method somehowDatabaseEmptiedAfterTimer.

@Test
@SuppressWarnings("unchecked")
public void somehowDatabaseEmptiedAfterTimer() throws IOException, InterruptedException {
    /* Cover the if (batchId != null) test though it could happen only if the database content disappear after the timer... */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    Ingestion ingestion = mock(Ingestion.class);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(anyString())).thenReturn(2);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(2, channel.getCounter(TEST_GROUP));
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 20 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelTest method suspendWithFailureCallback.

@Test
@SuppressWarnings("unchecked")
public void suspendWithFailureCallback() {
    Ingestion mockIngestion = mock(Ingestion.class);
    Persistence mockPersistence = mock(Persistence.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(30);
    when(mockPersistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(10));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).then(getSendAsyncAnswer(new HttpException(404)));
    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);
    /* 30 from countLogs and 10 new logs from getLogs. */
    verify(mockListener, times(40)).onBeforeSending(any(Log.class));
    verify(mockListener, times(40)).onFailure(any(Log.class), any(SocketException.class));
    assertFalse(channel.isEnabled());
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Persistence(com.microsoft.appcenter.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.appcenter.http.HttpException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Ingestion (com.microsoft.appcenter.ingestion.Ingestion)31 Context (android.content.Context)29 Persistence (com.microsoft.appcenter.persistence.Persistence)29 Test (org.junit.Test)29 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)24 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)24 UUID (java.util.UUID)24 Log (com.microsoft.appcenter.ingestion.models.Log)20 Matchers.anyString (org.mockito.Matchers.anyString)19 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)14 IOException (java.io.IOException)13 SocketException (java.net.SocketException)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 HttpException (com.microsoft.appcenter.http.HttpException)4 Answer (org.mockito.stubbing.Answer)4 CancellationException (com.microsoft.appcenter.CancellationException)3 ArrayList (java.util.ArrayList)3 HttpResponse (com.microsoft.appcenter.http.HttpResponse)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 HashMap (java.util.HashMap)1