Search in sources :

Example 6 with Persistence

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

the class DefaultChannelTest method invalidGroup.

@Test
public void invalidGroup() throws Persistence.PersistenceException {
    Persistence persistence = mock(Persistence.class);
    Channel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, mock(Ingestion.class));
    /* Enqueue a log before group is registered = failure. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP);
    verify(log, never()).setDevice(any(Device.class));
    verify(log, never()).setToffset(anyLong());
    verify(persistence, never()).putLog(TEST_GROUP, log);
    /* Trying remove group that not registered. */
    channel.removeGroup(TEST_GROUP);
    verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Persistence(com.microsoft.azure.mobile.persistence.Persistence) Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Device(com.microsoft.azure.mobile.ingestion.models.Device) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Test(org.junit.Test)

Example 7 with Persistence

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

the class DefaultChannelTest method errorLogSuccess.

@Test
@SuppressWarnings("unchecked")
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), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer());
    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);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 2 error logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify that 2 items have been persisted. */
    verify(mockPersistence, times(2)).putLog(eq(TEST_GROUP), any(Log.class));
    /* 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.getCounter(TEST_GROUP));
    /* Verify timer. */
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, never()).removeCallbacks(any(Runnable.class));
}
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) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with Persistence

use of com.microsoft.azure.mobile.persistence.Persistence 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 9 with Persistence

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

the class DefaultChannelTest method disableBeforeCheckingPendingLogs.

@Test
@SuppressWarnings("unchecked")
public void disableBeforeCheckingPendingLogs() throws IOException {
    Ingestion ingestion = mock(Ingestion.class);
    Persistence persistence = mock(Persistence.class);
    final DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).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) throws Throwable {
            /* Simulate a service disabled in the middle of network transaction. */
            ServiceCallback callback = (ServiceCallback) invocation.getArguments()[3];
            channel.removeGroup(TEST_GROUP);
            callback.onCallSucceeded("");
            return null;
        }
    });
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* checkPendingLogs is getting called twice from triggerIngestion and a callback for ingestion.
           It can be failed because of timing issue so checking at least once instead. */
    verifyStatic(atLeastOnce());
    MobileCenterLog.info(eq(MobileCenter.LOG_TAG), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 10 with Persistence

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

the class DefaultChannelTest method somehowDatabaseEmptiedAfterTimer.

@Test
@SuppressWarnings("unchecked")
public void somehowDatabaseEmptiedAfterTimer() throws IOException, InterruptedException {
    /* Cover the if (batchId != null) test though it could happen only if the database content disappear after the timer... */
    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(2);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(2, channel.getCounter(TEST_GROUP));
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion, never()).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.azure.mobile.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) IOException(java.io.IOException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Test(org.junit.Test)

Aggregations

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