AutomationEngine { @NonNull public PendingResult<Boolean> schedule(@NonNull final Schedule<? extends ScheduleData> schedule) { final PendingResult<Boolean> pendingResult = new PendingResult<>(); backgroundHandler.post(new Runnable() { @Override public void run() { cleanSchedules(); if (dao.getScheduleCount() >= SCHEDULE_LIMIT) { Logger.error("AutomationEngine - Unable to insert schedule due to schedule exceeded limit."); pendingResult.setResult(false); return; } FullSchedule entry = ScheduleConverters.convert(schedule); dao.insert(entry); subscribeStateObservables(Collections.singletonList(entry)); notifyNewSchedule(Collections.<Schedule<? extends ScheduleData>>singletonList(schedule)); Logger.verbose("AutomationEngine - Scheduled entries: %s", schedule); pendingResult.setResult(true); } }); return pendingResult; } AutomationEngine(@NonNull Context context,
                     @NonNull AirshipRuntimeConfig runtimeConfig,
                     @NonNull Analytics analytics,
                     @NonNull PreferenceDataStore dataStore); @VisibleForTesting  AutomationEngine(@NonNull Analytics analytics,
                     @NonNull ActivityMonitor activityMonitor,
                     @NonNull OperationScheduler scheduler,
                     @NonNull AutomationDao dao,
                     @NonNull LegacyDataMigrator legacyDataMigrator); void start(@NonNull AutomationDriver driver); void setPaused(boolean isPaused); void stop(); @NonNull PendingResult<Boolean> schedule(@NonNull final Schedule<? extends ScheduleData> schedule); @NonNull PendingResult<Boolean> schedule(@NonNull final List<Schedule<? extends ScheduleData>> schedules); @NonNull PendingResult<Boolean> cancel(@NonNull final Collection<String> ids); @NonNull PendingResult<Boolean> cancelByType(@NonNull @Schedule.Type final String type); @NonNull PendingResult<Boolean> cancelGroup(@NonNull final String group); @NonNull PendingResult<Void> cancelGroups(@NonNull final Collection<String> groups); @NonNull PendingResult<Void> cancelAll(); @NonNull PendingResult<Collection<Schedule<T>>> getSchedulesByType(@Schedule.Type final String type); @NonNull PendingResult<Schedule<T>> getSchedule(@NonNull final String scheduleId, @Schedule.Type final String type); @NonNull PendingResult<Collection<Schedule<? extends ScheduleData>>> getSchedules(@NonNull final Set<String> scheduleIds); @NonNull PendingResult<Collection<Schedule<T>>> getSchedules(@NonNull final String group, @NonNull @Schedule.Type final String type); @NonNull PendingResult<Boolean> editSchedule(@NonNull final String scheduleId, @NonNull final ScheduleEdits<? extends ScheduleData> edits); void checkPendingSchedules(); @NonNull PendingResult<Collection<Schedule<? extends ScheduleData>>> getSchedules(); void setScheduleListener(@Nullable ScheduleListener scheduleListener); void applyEdits(@NonNull FullSchedule entry, @NonNull ScheduleEdits edits);  }
@Test public void testSchedule() throws Exception { Future<Boolean> pendingResult = automationEngine.schedule(schedule); runLooperTasks(); assertTrue(pendingResult.isDone()); assertFalse(pendingResult.isCancelled()); assertTrue(pendingResult.get()); FullSchedule entity = dao.getSchedule(schedule.getId()); assertNotNull(entity); assertEquals(schedule, ScheduleConverters.convert(entity)); }
@Test public void testPriority() throws Exception { ArrayList<Schedule<? extends ScheduleData>> schedules = new ArrayList<>(); Integer[] addedPriorityLevels = new Integer[] { 5, 2, 1, 0, 0, 4, 3, 3, 2 }; ArrayList<Integer> expectedExecutionOrder = new ArrayList<>(Arrays.asList(addedPriorityLevels)); Collections.sort(expectedExecutionOrder); for (int priority : addedPriorityLevels) { final Schedule<Actions> schedule = Schedule.newBuilder(new Actions(JsonMap.EMPTY_MAP)) .addTrigger(Triggers.newCustomEventTriggerBuilder() .setCountGoal(1) .setEventName("event") .build()) .setPriority(priority) .build(); schedules.add(schedule); schedule(schedule); } CustomEvent.newBuilder("event") .build() .track(); runLooperTasks(); assertEquals(expectedExecutionOrder, driver.priorityList); }
@Test public void testInterval() throws Exception { final Schedule schedule = Schedule.newBuilder(this.schedule) .setInterval(10, TimeUnit.SECONDS) .setLimit(2) .build(); schedule(schedule); CustomEvent.newBuilder("event") .build() .track(); runLooperTasks(); driver.prepareCallbackMap.get(schedule.getId()).onFinish(AutomationDriver.PREPARE_RESULT_CONTINUE); runLooperTasks(); driver.executionCallbackMap.get(schedule.getId()).onFinish(); runLooperTasks(); verifyState(schedule, ScheduleState.PAUSED); advanceAutomationLooperScheduler(TimeUnit.SECONDS.toMillis(10)); verifyState(schedule, ScheduleState.IDLE); }