Search in sources :

Example 1 with DatabasePersistenceAsync

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method shutdownInterrupted.

@Test
public void shutdownInterrupted() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
    doThrow(new InterruptedException()).when(mockPersistenceAsync).waitForCurrentTasksToComplete(anyLong());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mockListener).onBeforeSending(notNull(Log.class));
    channel.shutdown();
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    verify(mockPersistence).clearPendingLogState();
    verify(mockPersistenceAsync).waitForCurrentTasksToComplete(DefaultChannel.SHUTDOWN_TIMEOUT);
    verifyStatic();
    MobileCenterLog.warn(eq(MobileCenterLog.LOG_TAG), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof InterruptedException;
        }
    }));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ArgumentMatcher(org.mockito.ArgumentMatcher) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 2 with DatabasePersistenceAsync

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatalNoListener() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    /* Simulate a lot of logs already in database. */
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
    /* Make first call hang, and the second call return a fatal error. */
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenReturn(null).then(getSendAsyncAnswer(new HttpException(403)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 2 events. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify callbacks not invoked. */
    verify(mockListener, never()).onBeforeSending(any(Log.class));
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    /* Verify logs were deleted. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Also used : Persistence(com.microsoft.azure.mobile.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpException(com.microsoft.azure.mobile.http.HttpException) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Test(org.junit.Test)

Example 3 with DatabasePersistenceAsync

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendRecoverable() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(503)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify callbacks invoked only for the first log. */
    verify(mockListener).onBeforeSending(any(Log.class));
    /* Verify no failure forwarded. */
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    /* Verify no log was deleted. */
    verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
    /* But that we cleared batch state. */
    verify(mockPersistence).clearPendingLogState();
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 4 with DatabasePersistenceAsync

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelRaceConditionTest method enableWhileDisabling.

@Test
public void enableWhileDisabling() throws Exception {
    /* Set up mocking. */
    final Semaphore beforeCallSemaphore = new Semaphore(0);
    final Semaphore afterCallSemaphore = new Semaphore(0);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(anyString(), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            new Thread() {

                @Override
                public void run() {
                    beforeCallSemaphore.acquireUninterruptibly();
                    try {
                        invocation.callRealMethod();
                    } catch (Throwable e) {
                        throw new RuntimeException(e);
                    }
                    afterCallSemaphore.release();
                }
            }.start();
            return null;
        }
    }).when(mockPersistenceAsync).getLogs(anyString(), eq(CLEAR_BATCH_SIZE), anyListOf(Log.class), any(DatabasePersistenceAsync.DatabasePersistenceAsyncCallback.class));
    /* Simulate enable module then disable. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mock(IngestionHttp.class));
    Channel.GroupListener listener = mock(Channel.GroupListener.class);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, listener);
    channel.setEnabled(false);
    channel.setEnabled(true);
    /* Unblock call to getLogs. */
    beforeCallSemaphore.release();
    /* Wait call to getLogs. */
    afterCallSemaphore.acquireUninterruptibly();
    /* Check canceled before we could delete logs. */
    verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
}
Also used : Context(android.content.Context) Log(com.microsoft.azure.mobile.ingestion.models.Log) Semaphore(java.util.concurrent.Semaphore) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 5 with DatabasePersistenceAsync

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.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(403)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* 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) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Aggregations

Context (android.content.Context)15 Log (com.microsoft.azure.mobile.ingestion.models.Log)15 DatabasePersistenceAsync (com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync)15 Persistence (com.microsoft.azure.mobile.persistence.Persistence)15 Test (org.junit.Test)15 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)12 Semaphore (java.util.concurrent.Semaphore)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)8 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)8 UUID (java.util.UUID)8 CancellationException (com.microsoft.azure.mobile.CancellationException)7 HttpException (com.microsoft.azure.mobile.http.HttpException)6 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)6 IOException (java.io.IOException)6 SocketException (java.net.SocketException)5 Ingestion (com.microsoft.azure.mobile.ingestion.Ingestion)3 ArrayList (java.util.ArrayList)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 Matchers.anyString (org.mockito.Matchers.anyString)3