EventManager { @WorkerThread public boolean uploadEvents(@NonNull Map<String, String> headers) { isScheduled = false; preferenceDataStore.put(LAST_SEND_KEY, System.currentTimeMillis()); int eventCount; Map<String, String> events; synchronized (eventLock) { eventCount = eventResolver.getEventCount(); if (eventCount <= 0) { Logger.debug("EventManager - No events to send."); return true; } final int avgSize = Math.max(1, eventResolver.getDatabaseSize() / eventCount); int batchEventCount = Math.min(MAX_BATCH_EVENT_COUNT, preferenceDataStore.getInt(MAX_BATCH_SIZE_KEY, EventResponse.MAX_BATCH_SIZE_BYTES) / avgSize); events = eventResolver.getEvents(batchEventCount); } if (events.isEmpty()) { return true; } EventResponse response = apiClient.sendEvents(events.values(), headers); if (response == null || response.getStatus() != 200) { Logger.debug("EventManager - Analytic upload failed."); return false; } Logger.debug("EventManager - Analytic events uploaded."); synchronized (eventLock) { eventResolver.deleteEvents(events.keySet()); } preferenceDataStore.put(MAX_TOTAL_DB_SIZE_KEY, response.getMaxTotalSize()); preferenceDataStore.put(MAX_BATCH_SIZE_KEY, response.getMaxBatchSize()); preferenceDataStore.put(MIN_BATCH_INTERVAL_KEY, response.getMinBatchInterval()); if (eventCount - events.size() > 0) { scheduleEventUpload(MULTIPLE_BATCH_DELAY, TimeUnit.MILLISECONDS); } return true; } EventManager(@NonNull Context context,
                 @NonNull PreferenceDataStore preferenceDataStore,
                 @NonNull AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  EventManager(@NonNull PreferenceDataStore preferenceDataStore,
                        @NonNull AirshipRuntimeConfig runtimeConfig,
                        @NonNull JobDispatcher jobDispatcher,
                        @NonNull ActivityMonitor activityMonitor,
                        @NonNull EventResolver eventResolver,
                        @NonNull EventApiClient apiClient); void scheduleEventUpload(final long delay, @NonNull TimeUnit timeUnit); @WorkerThread void addEvent(@NonNull Event event, @NonNull String sessionId); @WorkerThread void deleteEvents(); @WorkerThread boolean uploadEvents(@NonNull Map<String, String> headers); @NonNull
    static final String ACTION_SEND; }
@Test public void testSendEventMaxCount() { dataStore.put(EventManager.MAX_BATCH_SIZE_KEY, 100000); when(mockEventResolver.getDatabaseSize()).thenReturn(100000); when(mockEventResolver.getEventCount()).thenReturn(1000); eventManager.uploadEvents(Collections.<String, String>emptyMap()); verify(mockEventResolver).getEvents(500); }