Search in sources :

Example 1 with Persistence

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

the class DefaultChannelTest method filter.

@Test
public void filter() throws Persistence.PersistenceException {
    /* Given a mock channel. */
    Persistence persistence = mock(Persistence.class);
    @SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class), mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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);
        /* Then except the following. behaviors. */
        verify(listener1).onEnqueuingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onEnqueuingLog(log, TEST_GROUP);
        verify(listener2).shouldFilter(log);
        verify(persistence, never()).putLog(TEST_GROUP, log);
    }
    /* 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);
        /* Then except the following. behaviors. */
        verify(listener1).onEnqueuingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onEnqueuingLog(log, TEST_GROUP);
        /* Second listener skipped since first listener filtered out. */
        verify(listener2, never()).shouldFilter(log);
        verify(persistence, never()).putLog(TEST_GROUP, log);
    }
    /* 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);
        /* Then except the following. behaviors. */
        verify(listener1).onEnqueuingLog(log, TEST_GROUP);
        verify(listener1).shouldFilter(log);
        verify(listener2).onEnqueuingLog(log, TEST_GROUP);
        verify(listener2).shouldFilter(log);
        verify(persistence).putLog(TEST_GROUP, log);
    }
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) Log(com.microsoft.appcenter.ingestion.models.Log) Test(org.junit.Test)

Example 2 with Persistence

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

the class DefaultChannelTest method initialLogsMoreThan1Batch.

@Test
@SuppressWarnings("unchecked")
public void initialLogsMoreThan1Batch() 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(103);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(3));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    verify(ingestion, times(2)).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, times(3)).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.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 3 with Persistence

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

the class DefaultChannelTest method initialLogsThenDisable.

@Test
@SuppressWarnings("unchecked")
public void initialLogsThenDisable() 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, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    assertEquals(3, channel.getCounter(TEST_GROUP));
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    channel.setEnabled(false);
    verify(mHandler).removeCallbacks(any(Runnable.class));
    verify(ingestion).close();
    verify(persistence).deleteLogs(TEST_GROUP);
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 4 with Persistence

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

the class DefaultChannelTest method lessLogsThanExpected.

@Test
public void lessLogsThanExpected() {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<ArrayList<Log>>any())).then(getGetLogsAnswer(40));
    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, mCoreHandler);
    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));
    /* Database returned less logs than we expected (40 vs 50), yet counter must be reset. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
}
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) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with Persistence

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

the class DefaultChannelTest method enqueuePersistenceFailure.

@Test
public void enqueuePersistenceFailure() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    /* Simulate Persistence failing. */
    doThrow(new Persistence.PersistenceException("mock", new IOException("mock"))).when(mockPersistence).putLog(anyString(), any(Log.class));
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Verify no request is sent if Persistence fails. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(0, channel.getCounter(TEST_GROUP));
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) IOException(java.io.IOException) 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