Search in sources :

Example 56 with Persistence

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

the class DefaultChannelPauseResumeTest method pauseWithCustomIntervalAndResumeBeforeIntervalDue.

@Test
public void pauseWithCustomIntervalAndResumeBeforeIntervalDue() {
    /* Mock current time. */
    long now = 1000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(TEST_GROUP)).thenReturn(0);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mockIngestion, mAppCenterHandler);
    int batchTimeInterval = 10000;
    channel.addGroup(TEST_GROUP, 10, batchTimeInterval, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), anyLong());
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we enqueue a log while being paused. */
    channel.pauseGroup(TEST_GROUP, null);
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that timer does not start but that the current time is saved for future reference. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we resume later (before interval is due) */
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(now);
    now = 3000;
    long expectedTimeToWait = 8000;
    when(System.currentTimeMillis()).thenReturn(now);
    channel.resumeGroup(TEST_GROUP, null);
    /* Check timer is started with remaining time. */
    verify(mAppCenterHandler).postDelayed(notNull(Runnable.class), eq(expectedTimeToWait));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
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 57 with Persistence

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

the class DefaultChannelPauseResumeTest method pauseWithCustomIntervalAndResumeAfterIntervalDue.

@Test
public void pauseWithCustomIntervalAndResumeAfterIntervalDue() {
    /* Mock current time. */
    long now = 1000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(TEST_GROUP)).thenReturn(0);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mockIngestion, mAppCenterHandler);
    int batchTimeInterval = 10000;
    channel.addGroup(TEST_GROUP, 10, batchTimeInterval, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), anyLong());
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we enqueue a log while being paused. */
    channel.pauseGroup(TEST_GROUP, null);
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that timer does not start but that the current time is saved for future reference. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* When we resume later (after interval is due) */
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(now);
    when(System.currentTimeMillis()).thenReturn(now + batchTimeInterval + 1);
    channel.resumeGroup(TEST_GROUP, null);
    /* Check timer is not started and logs send immediately. */
    verify(mAppCenterHandler, never()).postDelayed(notNull(Runnable.class), anyLong());
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
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 58 with Persistence

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

the class DefaultChannelOtherOperationsTest method shutdown.

@Test
public void shutdown() {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
    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 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mockListener).onBeforeSending(notNull(Log.class));
    channel.shutdown();
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    verify(mockPersistence).clearPendingLogState();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 59 with Persistence

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

the class DefaultChannelOtherOperationsTest method groupCallbacks.

@Test
public void groupCallbacks() {
    Persistence persistence = mock(Persistence.class);
    Ingestion ingestion = mock(Ingestion.class);
    Channel.Listener listener = spy(new AbstractChannelListener());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    channel.addListener(listener);
    Channel.GroupListener groupListener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, groupListener);
    verify(listener).onGroupAdded(TEST_GROUP, groupListener, BATCH_TIME_INTERVAL);
    channel.pauseGroup(TEST_GROUP, null);
    verify(listener).onPaused(TEST_GROUP, null);
    channel.pauseGroup(TEST_GROUP, "token");
    verify(listener).onPaused(TEST_GROUP, "token");
    channel.resumeGroup(TEST_GROUP, null);
    verify(listener).onResumed(TEST_GROUP, null);
    channel.resumeGroup(TEST_GROUP, "token");
    verify(listener).onResumed(TEST_GROUP, "token");
    channel.removeGroup(TEST_GROUP);
    verify(listener).onGroupRemoved(TEST_GROUP);
}
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 60 with Persistence

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

the class DefaultChannelOtherOperationsTest method filter.

@Test
public void filter() throws Persistence.PersistenceException {
    /* Given a mock channel. */
    Persistence persistence = mock(Persistence.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Given we add mock listeners. */
    Channel.Listener listener1 = mock(Channel.Listener.class);
    channel.addListener(listener1);
    Channel.Listener listener2 = mock(Channel.Listener.class);
    channel.addListener(listener2);
    /* Given 1 log. */
    {
        /* Given the second listener filtering out logs. */
        Log log = mock(Log.class);
        when(listener2.shouldFilter(log)).thenReturn(true);
        /* When we enqueue that log. */
        channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
        /* Then except the following. behaviors. */
        verify(listener1).onPreparingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onPreparingLog(log, TEST_GROUP);
        verify(listener2).shouldFilter(log);
        verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
    }
    /* Given 1 log. */
    {
        /* Given the first listener filtering out logs. */
        Log log = mock(Log.class);
        when(listener1.shouldFilter(log)).thenReturn(true);
        when(listener2.shouldFilter(log)).thenReturn(false);
        /* When we enqueue that log. */
        channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
        /* Then except the following. behaviors. */
        verify(listener1).onPreparingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onPreparingLog(log, TEST_GROUP);
        /* Second listener skipped since first listener filtered out. */
        verify(listener2, never()).shouldFilter(log);
        verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
    }
    /* Given 1 log. */
    {
        /* Given no listener filtering out logs. */
        Log log = mock(Log.class);
        when(listener1.shouldFilter(log)).thenReturn(false);
        when(listener2.shouldFilter(log)).thenReturn(false);
        /* When we enqueue that log. */
        channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
        /* Then except the following. behaviors. */
        verify(listener1).onPreparingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onPreparingLog(log, TEST_GROUP);
        verify(listener2).shouldFilter(log);
        verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
    }
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) 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