Search in sources :

Example 31 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method analyticsFatal.

@Test
@SuppressWarnings("unchecked")
public void analyticsFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    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 HttpException(403))).then(getSendAsyncAnswer());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* 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 the Channel is disabled. */
    assertFalse(channel.isEnabled());
    /* Verify that we have cleared the logs. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
    /* Verify counter. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    /* The counter should still be 0 as logs are discarded by channel now. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* No more timer yet at this point. */
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Prepare to mock timer. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    /* Enable channel to see if it can work again after that error state. */
    channel.setEnabled(true);
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    assertEquals(20, channel.getCounter(TEST_GROUP));
    /* Wait for timer. */
    assertNotNull(runnable.get());
    runnable.get().run();
    /* The counter should back to 0 now. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Verify that we have called sendAsync on the ingestion 2 times total: 1 earlier failure then 1 success. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence for the successful batch after re-enabling. */
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Verify 1 more timer call. */
    verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Verify no more cancel timer. */
    verify(mHandler).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) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 32 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method enqueuePersistenceFailure.

@Test
public void enqueuePersistenceFailure() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    /* Simulate Persistence failing. */
    doThrow(new Persistence.PersistenceException("mock", new IOException("mock"))).when(mockPersistence).putLog(anyString(), any(Log.class));
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Verify no request is sent if Persistence fails. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(0, channel.getCounter(TEST_GROUP));
    verify(mHandler, never()).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) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) IOException(java.io.IOException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 33 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence 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);
    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) 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) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Persistence (com.microsoft.azure.mobile.persistence.Persistence)33 Context (android.content.Context)32 Test (org.junit.Test)32 Log (com.microsoft.azure.mobile.ingestion.models.Log)27 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)23 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)23 UUID (java.util.UUID)23 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)18 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)18 DatabasePersistenceAsync (com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync)15 Ingestion (com.microsoft.azure.mobile.ingestion.Ingestion)14 IOException (java.io.IOException)14 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 CancellationException (com.microsoft.azure.mobile.CancellationException)9 HttpException (com.microsoft.azure.mobile.http.HttpException)9 SocketException (java.net.SocketException)9 ArrayList (java.util.ArrayList)9 Semaphore (java.util.concurrent.Semaphore)9 Matchers.anyString (org.mockito.Matchers.anyString)9 ArgumentMatcher (org.mockito.ArgumentMatcher)3