EventManager { @WorkerThread public void addEvent(@NonNull Event event, @NonNull String sessionId) { synchronized (eventLock) { eventResolver.insertEvent(event, sessionId); eventResolver.trimDatabase(preferenceDataStore.getInt(MAX_TOTAL_DB_SIZE_KEY, EventResponse.MAX_TOTAL_DB_SIZE_BYTES)); } switch (event.getPriority()) { case Event.HIGH_PRIORITY: scheduleEventUpload(HIGH_PRIORITY_BATCH_DELAY, TimeUnit.MILLISECONDS); break; case Event.NORMAL_PRIORITY: scheduleEventUpload(Math.max(getNextSendDelay(), NORMAL_PRIORITY_BATCH_DELAY), TimeUnit.MILLISECONDS); break; case Event.LOW_PRIORITY: default: if (activityMonitor.isAppForegrounded()) { scheduleEventUpload(Math.max(getNextSendDelay(), LOW_PRIORITY_BATCH_DELAY), TimeUnit.MILLISECONDS); } else { long currentTime = System.currentTimeMillis(); long lastSendTime = preferenceDataStore.getLong(LAST_SEND_KEY, 0); long sendDelta = currentTime - lastSendTime; long minimumWait = Math.max(runtimeConfig.getConfigOptions().backgroundReportingIntervalMS - sendDelta, getNextSendDelay()); scheduleEventUpload(Math.max(minimumWait, LOW_PRIORITY_BATCH_DELAY), TimeUnit.MILLISECONDS); } break; } } 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 testAddEventAfterNextSendTime() { CustomEvent customEvent = CustomEvent.newBuilder("event name").build(); eventManager.addEvent(customEvent, "session"); verify(mockEventResolver, new Times(1)).insertEvent(customEvent, "session"); verify(mockDispatcher).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(EventManager.ACTION_SEND) && jobInfo.getInitialDelay() == 10000L; } })); }
@Test public void testAddEventBeforeNextSendTime() { dataStore.put(EventManager.LAST_SEND_KEY, System.currentTimeMillis()); dataStore.put(MIN_BATCH_INTERVAL_KEY, 20000); CustomEvent customEvent = CustomEvent.newBuilder("event name").build(); eventManager.addEvent(customEvent, "session"); verify(mockDispatcher).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(EventManager.ACTION_SEND) && jobInfo.getInitialDelay() > 10000; } })); }
@Test public void testAddingHighPriorityEvents() { dataStore.put(EventManager.LAST_SEND_KEY, 32661446400000L); RegionEvent regionEvent = RegionEvent.newBuilder() .setRegionId("id") .setSource("source") .setBoundaryEvent(RegionEvent.BOUNDARY_EVENT_ENTER) .build(); eventManager.addEvent(regionEvent, "session"); verify(mockDispatcher).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(EventManager.ACTION_SEND) && jobInfo.getInitialDelay() == 0; } })); }