DefaultChannel implements Channel { @Override public void addGroup(final String groupName, int maxLogsPerBatch, long batchTimeInterval, int maxParallelBatches, Ingestion ingestion, GroupListener groupListener) { AppCenterLog.debug(LOG_TAG, "addGroup(" + groupName + ")"); ingestion = ingestion == null ? mIngestion : ingestion; mIngestions.add(ingestion); final GroupState groupState = new GroupState(groupName, maxLogsPerBatch, batchTimeInterval, maxParallelBatches, ingestion, groupListener); mGroupStates.put(groupName, groupState); groupState.mPendingLogCount = mPersistence.countLogs(groupName); if (mAppSecret != null || mIngestion != ingestion) { checkPendingLogs(groupState); } for (Listener listener : mListeners) { listener.onGroupAdded(groupName, groupListener, batchTimeInterval); } } DefaultChannel(@NonNull Context context, String appSecret, @NonNull LogSerializer logSerializer, @NonNull HttpClient httpClient, @NonNull Handler appCenterHandler); @VisibleForTesting  DefaultChannel(@NonNull Context context, String appSecret, @NonNull Persistence persistence, @NonNull Ingestion ingestion, @NonNull Handler appCenterHandler); @WorkerThread @Override boolean setMaxStorageSize(long maxStorageSizeInBytes); @WorkerThread @Override void setAppSecret(@NonNull String appSecret); @Override void addGroup(final String groupName, int maxLogsPerBatch, long batchTimeInterval, int maxParallelBatches, Ingestion ingestion, GroupListener groupListener); @Override void removeGroup(String groupName); @Override void pauseGroup(String groupName, String targetToken); @Override void resumeGroup(String groupName, String targetToken); @Override boolean isEnabled(); @Override void setEnabled(boolean enabled); @Override void setLogUrl(String logUrl); @Override void clear(String groupName); @Override void invalidateDeviceCache(); @Override void enqueue(@NonNull Log log, @NonNull final String groupName, int flags); @Override void addListener(Listener listener); @Override void removeListener(Listener listener); @Override void shutdown();  }
@Test public void checkPendingLogsStoresStartTime() { long now = 1; when(System.currentTimeMillis()).thenReturn(now); Persistence mockPersistence = mock(Persistence.class); when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5); AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler); channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class)); verifyStatic(); SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now)); verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL)); }
@Test public void checkPendingLogsDoesNotStartTimerWithoutLogs() { long now = 1; when(System.currentTimeMillis()).thenReturn(now); Persistence mockPersistence = mock(Persistence.class); when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(0); AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler); channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class)); verifyStatic(never()); SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now)); verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL)); }
@Test public void checkPendingLogsResumesStartTime() { long startTime = 1000; when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime); long now = 5000; when(System.currentTimeMillis()).thenReturn(now); Persistence mockPersistence = mock(Persistence.class); when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5); AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler); channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class)); verifyStatic(never()); SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class)); verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL - (now - startTime))); }
@Test public void checkPendingLogsReplacesInvalidStartTime() { long now = 1000; when(System.currentTimeMillis()).thenReturn(now); long startTime = 10000; when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime); Persistence mockPersistence = mock(Persistence.class); when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5); AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler); channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class)); verifyStatic(); SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), any(long.class)); verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL)); }
@Test public void checkPendingLogsTriggersIngestionIfTimerIsOver() { long now = 50000; when(System.currentTimeMillis()).thenReturn(now); long startTime = 1000; when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTime); Persistence mockPersistence = mock(Persistence.class); when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5); when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(5)); AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler); channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class)); verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), anyLong()); verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class)); }