Search in sources :

Example 21 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method useAlternateIngestion.

@Test
public void useAlternateIngestion() throws IOException {
    /* Set up channel with an alternate ingestion. */
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that we have called sendAsync on the ingestion. */
    verify(alternateIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Disabling the channel should close all channels */
    channel.setEnabled(false);
    verify(alternateIngestion).close();
    verify(defaultIngestion).close();
    /* Enabling the channel should reopen all channels */
    channel.setEnabled(true);
    verify(alternateIngestion).reopen();
    verify(defaultIngestion).reopen();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) 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) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 22 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method sendPendingLogsAfterSettingAppSecret.

@Test
public void sendPendingLogsAfterSettingAppSecret() {
    /* Set up channel without app secret. */
    String appCenterGroup = "test_group1";
    String oneCollectorGroup = "test_group2";
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    /* Simulate we have 1 pending log in storage. */
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    /* Create channel with the two groups. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(appCenterGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Verify that we can now send logs to app center after we have set app secret. */
    channel.setAppSecret("testAppSecret");
    verify(defaultIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* If we add a one collector group it also resumes. */
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    verify(alternateIngestion).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) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 23 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method nullAppSecretProvided.

@Test
public void nullAppSecretProvided() throws Persistence.PersistenceException {
    /*
         * We don't test empty app secret in channel (and thus in tests) as it's already checked by AppCenter.
         * If app secret is set in channel, the assumption is that it's a well formed string.
         * Channel is not meant to be used publicly directly.
         */
    /* Given a mock channel. */
    Persistence persistence = mock(Persistence.class);
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, ingestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Check log url. */
    String logUrl = "http://mockUrl";
    channel.setLogUrl(logUrl);
    verify(ingestion).setLogUrl(logUrl);
    /* Check enqueue. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
    channel.enqueue(mock(Log.class), "other", Flags.DEFAULTS);
    verify(persistence, never()).putLog(any(Log.class), anyString(), anyInt());
    /* Check clear. Even without app secret it works as it could be logs from previous process. */
    channel.clear(TEST_GROUP);
    verify(persistence).deleteLogs(eq(TEST_GROUP));
    /* Check shutdown. */
    channel.shutdown();
    verify(persistence).clearPendingLogState();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 24 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion 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 25 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion 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

Ingestion (com.microsoft.appcenter.ingestion.Ingestion)31 Context (android.content.Context)29 Persistence (com.microsoft.appcenter.persistence.Persistence)29 Test (org.junit.Test)29 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)24 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)24 UUID (java.util.UUID)24 Log (com.microsoft.appcenter.ingestion.models.Log)20 Matchers.anyString (org.mockito.Matchers.anyString)19 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)14 IOException (java.io.IOException)13 SocketException (java.net.SocketException)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 HttpException (com.microsoft.appcenter.http.HttpException)4 Answer (org.mockito.stubbing.Answer)4 CancellationException (com.microsoft.appcenter.CancellationException)3 ArrayList (java.util.ArrayList)3 HttpResponse (com.microsoft.appcenter.http.HttpResponse)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 HashMap (java.util.HashMap)1