Search in sources :

Example 1 with Ingestion

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

the class DefaultChannelTest method initialLogsThenDisable.

@Test
@SuppressWarnings("unchecked")
public void initialLogsThenDisable() 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);
    assertEquals(3, channel.getCounter(TEST_GROUP));
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    channel.setEnabled(false);
    verify(mHandler).removeCallbacks(any(Runnable.class));
    verify(ingestion).close();
    verify(persistence).deleteLogs(TEST_GROUP);
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.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 2 with Ingestion

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

the class DefaultChannelTest method errorLogSuccess.

@Test
@SuppressWarnings("unchecked")
public void errorLogSuccess() throws Persistence.PersistenceException {
    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());
    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);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 2 error logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify that 2 items have been persisted. */
    verify(mockPersistence, times(2)).putLog(eq(TEST_GROUP), any(Log.class));
    /* Verify that we have called sendAsync on the ingestion twice as batch size is 1. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence. */
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. */
    verify(mockListener, times(2)).onBeforeSending(any(Log.class));
    /* Verify that we have called onSuccess in the listener. */
    verify(mockListener, times(2)).onSuccess(any(Log.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* 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) 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) 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)

Example 3 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-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);
    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));
    /* checkPendingLogs is getting called twice from triggerIngestion and a callback for ingestion.
           It can be failed because of timing issue so checking at least once instead. */
    verifyStatic(atLeastOnce());
    MobileCenterLog.info(eq(MobileCenter.LOG_TAG), anyString());
}
Also used : Context(android.content.Context) 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) 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 4 with Ingestion

use of com.microsoft.azure.mobile.ingestion.Ingestion in project mobile-center-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);
    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.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 5 with Ingestion

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

the class DefaultChannelTest method setLogUrl.

@Test
@SuppressWarnings("unchecked")
public void setLogUrl() {
    Ingestion ingestion = mock(Ingestion.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mock(Persistence.class), ingestion);
    String logUrl = "http://mockUrl";
    channel.setLogUrl(logUrl);
    verify(ingestion).setLogUrl(logUrl);
}
Also used : Context(android.content.Context) Persistence(com.microsoft.azure.mobile.persistence.Persistence) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) 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