Search in sources :

Example 26 with Persistence

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

the class DefaultChannelTest method checkPendingLogsResumesStartTime.

@Test
public void checkPendingLogsResumesStartTime() {
    /* Mock stored start time. */
    long startTime = 1000;
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime);
    /* Mock current time - before end of interval. */
    long now = 5000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* 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));
    /* Do not replace start timer value. */
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class));
    /* Start timer for remaining time. */
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL - (now - startTime)));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 27 with Persistence

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

the class DefaultChannelTest method lessLogsThanExpected.

@Test
public void lessLogsThanExpected() {
    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(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40)).then(getGetLogsAnswer(0));
    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);
    /* Prepare to mock timer. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    /* Enqueuing 49 events. */
    for (int i = 1; i <= 49; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
        assertEquals(i, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    }
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Enqueue another event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    /* Wait for timer. */
    delayedRunnable.getValue().run();
    /* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) 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 28 with Persistence

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

the class DefaultChannelTest method packageManagerIsBroken.

@Test
public void packageManagerIsBroken() throws Persistence.PersistenceException, DeviceInfoHelper.DeviceInfoException {
    /* Setup mocking to make device properties generation fail. */
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
    Persistence persistence = mock(Persistence.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    Channel.Listener listener = mock(Channel.Listener.class);
    channel.addListener(listener);
    /* Enqueue a log: listener is called before but then attaching device properties fails before saving the log. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(listener).onPreparingLog(log, TEST_GROUP);
    verify(listener, never()).shouldFilter(log);
    verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
}
Also used : Context(android.content.Context) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Test(org.junit.Test)

Example 29 with Persistence

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

the class DefaultChannelTest method checkPendingLogsTriggersIngestionIfTimerIsOver.

@Test
public void checkPendingLogsTriggersIngestionIfTimerIsOver() {
    /* Mock current time. */
    long now = 50000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Mock stored start time. */
    long startTime = 1000;
    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);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(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));
    /* Do not start the timer. */
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong());
    /* Check sending the logs batches. */
    verify(mockIngestion).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) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 30 with Persistence

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.

@Test
public void invokeCallbacksAfterSuspendFatal() {
    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(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.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(new HttpResponse(403))));
    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);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* 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) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) 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) HttpException(com.microsoft.appcenter.http.HttpException) 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