Search in sources :

Example 61 with Persistence

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

the class DefaultChannelOtherOperationsTest method checkSetStorageSizeForwarding.

@Test
public void checkSetStorageSizeForwarding() {
    /* The real Android test for checking size is in DatabaseManagerAndroidTest. */
    Persistence persistence = mock(Persistence.class);
    when(persistence.setMaxStorageSize(anyLong())).thenReturn(true).thenReturn(false);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mock(Ingestion.class), mAppCenterHandler);
    /* Just checks calls are forwarded to the low level database layer. */
    assertTrue(channel.setMaxStorageSize(20480));
    assertFalse(channel.setMaxStorageSize(2));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 62 with Persistence

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

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.

@Test(timeout = 5000)
public void disabledWhileHandlingIngestionFailure() {
    /* Set up mocking. */
    final Semaphore beforeCallSemaphore = new Semaphore(0);
    final Semaphore afterCallSemaphore = new Semaphore(0);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    final Exception mockException = new IOException();
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) {
            new Thread() {

                @Override
                public void run() {
                    beforeCallSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[3]).onCallFailed(mockException);
                    afterCallSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    /* Simulate enable module then disable. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, listener);
    channel.setEnabled(false);
    channel.setEnabled(true);
    /* Release call to mock ingestion. */
    beforeCallSemaphore.release();
    /* Wait for callback ingestion. */
    afterCallSemaphore.acquireUninterruptibly();
    /* Verify handling error was ignored. */
    verify(listener, never()).onFailure(any(Log.class), eq(mockException));
    verify(listener).onFailure(any(Log.class), argThat(new ArgumentMatcher<Exception>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof CancellationException;
        }
    }));
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) Log(com.microsoft.appcenter.ingestion.models.Log) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CancellationException(com.microsoft.appcenter.CancellationException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatcher(org.mockito.ArgumentMatcher) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 63 with Persistence

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

the class DefaultChannelTest method analyticsRecoverable.

@Test
public void analyticsRecoverable() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.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());
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
    /* Enqueuing 50 events. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Verify that 50 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    /* 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, Flags.DEFAULTS);
    }
    /* The counter keeps being increased. */
    assertEquals(70, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* Enable channel. */
    channel.setEnabled(true);
    /* Upon enabling, 1st batch of 50 is sent immediately, 20 logs are remaining. */
    assertEquals(20, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Wait for timer. */
    delayedRunnable.getValue().run();
    /* The counter should be 0 after the second batch. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* 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(mAppCenterHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) 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) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 64 with Persistence

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

the class DefaultChannelTest method checkPendingLogsReplacesInvalidStartTime.

@Test
public void checkPendingLogsReplacesInvalidStartTime() {
    /* Mock current time. */
    long now = 1000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Mock stored start time. */
    long startTime = 10000;
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that start time is replaced. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
    /* Start timer for whole interval. */
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 65 with Persistence

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

the class DefaultChannelTest method errorLogRecoverable.

@Test
public void errorLogRecoverable() 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), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new SocketException())).then(getSendAsyncAnswer());
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
    /* Enqueuing n errors. */
    int logNumber = 5;
    for (int i = 0; i < logNumber; i++) channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that n items have been persisted. */
    verify(mockPersistence, times(logNumber)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    /* 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(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, 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(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) 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) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) 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)

Aggregations

Persistence (com.microsoft.appcenter.persistence.Persistence)80 Test (org.junit.Test)77 Context (android.content.Context)76 Log (com.microsoft.appcenter.ingestion.models.Log)64 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)56 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)56 UUID (java.util.UUID)56 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)42 Matchers.anyString (org.mockito.Matchers.anyString)42 Ingestion (com.microsoft.appcenter.ingestion.Ingestion)29 IOException (java.io.IOException)26 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)19 InvocationOnMock (org.mockito.invocation.InvocationOnMock)17 CancellationException (com.microsoft.appcenter.CancellationException)16 HttpException (com.microsoft.appcenter.http.HttpException)16 SocketException (java.net.SocketException)16 ArrayList (java.util.ArrayList)13 HttpResponse (com.microsoft.appcenter.http.HttpResponse)10 Semaphore (java.util.concurrent.Semaphore)6 Answer (org.mockito.stubbing.Answer)6