Search in sources :

Example 11 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence 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 12 with Persistence

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

the class DefaultChannelTest method maxRequestsInitial.

@Test
@SuppressWarnings("unchecked")
public void maxRequestsInitial() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    when(mockPersistence.countLogs(any(String.class))).thenReturn(100);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer());
    final List<ServiceCallback> callbacks = new ArrayList<>();
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            if (args[3] instanceof ServiceCallback) {
                callbacks.add((ServiceCallback) invocation.getArguments()[3]);
            }
            return null;
        }
    });
    /* Init channel with mocks. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueue enough logs to be split in N + 1 maximum requests. */
    for (int i = 0; i < 100; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    /* Verify all logs stored, N requests sent, not log deleted yet. */
    verify(mockPersistence, times(100)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
    /* Make 1 of the call succeed. Verify log deleted. */
    callbacks.get(0).onCallSucceeded("");
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* The request N+1 is now unlocked. */
    verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Unlock all requests and check logs deleted. */
    for (int i = 1; i < 4; i++) callbacks.get(i).onCallSucceeded("");
    verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Only 2 batches after channel start (non initial logs), verify timer interactions. */
    verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, times(2)).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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with Persistence

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE - 1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(403)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify callbacks invoked (1 + DefaultChannel.CLEAR_BATCH_SIZE + DefaultChannel.CLEAR_BATCH_SIZE - 1) times. */
    verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onBeforeSending(any(Log.class));
    verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onFailure(any(Log.class), any(Exception.class));
    /* Verify logs were deleted. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) 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) 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 14 with Persistence

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

Example 15 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence 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)

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