Search in sources :

Example 51 with Persistence

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

Example 52 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project AppCenter-SDK-Android by Microsoft.

the class DefaultChannelRaceConditionTest method disabledWhileHandlingIngestionFailure.

@Test
public void disabledWhileHandlingIngestionFailure() throws Exception {
    /* 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(), eq(1), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockPersistence.getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class))).then(getGetLogsAnswer(0));
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    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) throws Throwable {
            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), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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) IOException(java.io.IOException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) 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 53 with Persistence

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

the class DefaultChannel method buildDefaultPersistence.

/**
 * Init Persistence for default constructor.
 */
private static Persistence buildDefaultPersistence(@NonNull Context context, @NonNull LogSerializer logSerializer) {
    Persistence persistence = new DatabasePersistence(context);
    persistence.setLogSerializer(logSerializer);
    return persistence;
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) DatabasePersistence(com.microsoft.appcenter.persistence.DatabasePersistence) DatabasePersistence(com.microsoft.appcenter.persistence.DatabasePersistence)

Example 54 with Persistence

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

the class DefaultChannelPauseResumeTest method pauseResumeGroup.

@Test
public void pauseResumeGroup() 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));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).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);
    assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
    /* Pause group. */
    channel.pauseGroup(TEST_GROUP, null);
    assertTrue(channel.getGroupState(TEST_GROUP).mPaused);
    /* Enqueue a log. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* 50 logs are persisted but never being sent to Ingestion. */
    assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(mockPersistence, times(50)).putLog(any(Log.class), eq(TEST_GROUP), eq(Flags.NORMAL));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, never()).onBeforeSending(any(Log.class));
    verify(mockListener, never()).onSuccess(any(Log.class));
    /* The counter should still be 50 now as we did NOT send data. */
    assertEquals(50, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Resume group. */
    channel.resumeGroup(TEST_GROUP, null);
    assertFalse(channel.getGroupState(TEST_GROUP).mPaused);
    /* Verify channel starts sending logs. */
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    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)).onBeforeSending(any(Log.class));
    verify(mockListener, times(50)).onSuccess(any(Log.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) 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 55 with Persistence

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

the class DefaultChannelPauseResumeTest method pauseGroupPauseTargetResumeGroupResumeTarget.

@Test
public void pauseGroupPauseTargetResumeGroupResumeTarget() throws Persistence.PersistenceException {
    /* Mock database and ingestion. */
    Persistence persistence = mock(Persistence.class);
    OneCollectorIngestion ingestion = mock(OneCollectorIngestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    AppCenterIngestion appCenterIngestion = mock(AppCenterIngestion.class);
    when(appCenterIngestion.isEnabled()).thenReturn(true);
    /* Create a channel with a log group that send logs 1 by 1. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, appCenterIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, ingestion, null);
    /* Pause group first. */
    channel.pauseGroup(TEST_GROUP, null);
    /* Pause token. */
    String targetToken = "iKey-apiKey";
    channel.pauseGroup(TEST_GROUP, targetToken);
    /* Mock the database to return logs now. */
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    /* Enqueue a log. */
    Log log = mock(Log.class);
    when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton(targetToken));
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    /* Verify persisted but not incrementing and checking logs. */
    verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Resume group should not send the log. */
    channel.resumeGroup(TEST_GROUP, null);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Resume token, send the log now. */
    channel.resumeGroup(TEST_GROUP, targetToken);
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) OneCollectorIngestion(com.microsoft.appcenter.ingestion.OneCollectorIngestion) Matchers.anyString(org.mockito.Matchers.anyString) 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