Search in sources :

Example 26 with Ingestion

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

the class DefaultChannelTest method initialLogsMoreThan1Batch.

@Test
public void initialLogsMoreThan1Batch() throws IOException {
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(anyString())).thenReturn(103);
    when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(50)).thenAnswer(getGetLogsAnswer(3));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    verify(ingestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(3, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    delayedRunnable.getValue().run();
    verify(ingestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.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) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 27 with Ingestion

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

the class DefaultChannelTest method disableBeforeCheckingPendingLogs.

@Test
public void disableBeforeCheckingPendingLogs() {
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    Persistence persistence = mock(Persistence.class);
    final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
    when(ingestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            /* Simulate a service disabled in the middle of network transaction. */
            ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
            channel.removeGroup(TEST_GROUP);
            callback.onCallSucceeded(new HttpResponse(200, ""));
            return null;
        }
    });
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 28 with Ingestion

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

the class DefaultChannelTest method suspendWithoutFailureCallback.

@Test
public void suspendWithoutFailureCallback() {
    Ingestion mockIngestion = mock(Ingestion.class);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(3);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(getSendAsyncAnswer(new SocketException()));
    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, null);
    assertFalse(channel.isEnabled());
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 29 with Ingestion

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

the class DefaultChannelTest method errorLogSuccess.

@Test
public void errorLogSuccess() 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());
    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, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
    /* Enqueuing 2 error logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that 2 items have been persisted. */
    verify(mockPersistence, times(2)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    /* Verify that we have called sendAsync on the ingestion twice as batch size is 1. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence. */
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. */
    verify(mockListener, times(2)).onBeforeSending(any(Log.class));
    /* Verify that we have called onSuccess in the listener. */
    verify(mockListener, times(2)).onSuccess(any(Log.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Verify timer. */
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.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) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 30 with Ingestion

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

the class DefaultChannel method setEnabled.

/**
 * Set the enabled flag. If false, the channel will continue to persist data but not forward any item to ingestion.
 * The most common use-case would be to set it to false and enable sending again after the channel has disabled itself after receiving
 * a recoverable error (most likely related to a server issue).
 *
 * @param enabled flag to enable or disable the channel.
 */
@Override
public void setEnabled(boolean enabled) {
    if (mEnabled == enabled) {
        return;
    }
    if (enabled) {
        mEnabled = true;
        mDiscardLogs = false;
        mCurrentState++;
        for (Ingestion ingestion : mIngestions) {
            ingestion.reopen();
        }
        for (GroupState groupState : mGroupStates.values()) {
            checkPendingLogs(groupState);
        }
    } else {
        mEnabled = false;
        suspend(true, new CancellationException());
    }
    /* Notify listeners that channel state has changed. */
    for (Listener listener : mListeners) {
        listener.onGloballyEnabled(enabled);
    }
}
Also used : CancellationException(com.microsoft.appcenter.CancellationException) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion)

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