Search in sources :

Example 6 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method suspendWithoutFailureCallback.

@Test
@SuppressWarnings("unchecked")
public void suspendWithoutFailureCallback() {
    Ingestion mockIngestion = mock(Ingestion.class);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(3);
    when(mockPersistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
    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 SocketException()));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    assertFalse(channel.isEnabled());
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method initialLogs.

@Test
@SuppressWarnings("unchecked")
public void initialLogs() throws IOException, InterruptedException {
    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(3);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(3));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
    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(3, channel.getCounter(TEST_GROUP));
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion).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.azure.mobile.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Test(org.junit.Test)

Example 8 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-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);
    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) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 9 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method initialLogsMoreThan1Batch.

@Test
@SuppressWarnings("unchecked")
public void initialLogsMoreThan1Batch() throws IOException, InterruptedException {
    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(103);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(3));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    verify(ingestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(3, channel.getCounter(TEST_GROUP));
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion, times(3)).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.azure.mobile.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Test(org.junit.Test)

Example 10 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-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);
    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) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Context (android.content.Context)11 Ingestion (com.microsoft.azure.mobile.ingestion.Ingestion)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Test (org.junit.Test)11 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)10 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)10 UUID (java.util.UUID)10 IOException (java.io.IOException)6 Log (com.microsoft.azure.mobile.ingestion.models.Log)5 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)5 SocketException (java.net.SocketException)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 HttpException (com.microsoft.azure.mobile.http.HttpException)2 ArrayList (java.util.ArrayList)2 Answer (org.mockito.stubbing.Answer)2 CancellationException (com.microsoft.azure.mobile.CancellationException)1