/** * Helper method to schedule a daily job on a background thread. This is helpful to avoid IO operations * on the main thread. For more information about scheduling daily jobs see {@link #schedule(JobRequest.Builder, long, long)}. * * <br> * <br> * * In case of a failure an error is logged, but the application doesn't crash. */ public static void scheduleAsync(@NonNull JobRequest.Builder baseBuilder, long startMs, long endMs) { scheduleAsync(baseBuilder, startMs, endMs, JobRequest.DEFAULT_JOB_SCHEDULED_CALLBACK); }
@Test public void verifyErrorAsyncDailyJob() throws Exception { JobConfig.setClock(new TestClock()); JobScheduler jobScheduler = mock(JobScheduler.class); when(jobScheduler.schedule(any(JobInfo.class))).thenThrow(new RuntimeException("test")); when(context().getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(jobScheduler); final AtomicReference<Exception> reference = new AtomicReference<>(); DailyJob.scheduleAsync(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), 1000, 2000, new JobRequest.JobScheduledCallback() { @Override public void onJobScheduled(int jobId, @NonNull String tag, @Nullable Exception exception) { assertThat(jobId).isEqualTo(JOB_ID_ERROR); reference.set(exception); } }); waitUntilScheduled(); assertThat(reference.get()).isInstanceOf(RuntimeException.class); }
@Test public void verifyJobIdAsyncDailyJob() throws Exception { JobConfig.setClock(new TestClock()); final AtomicInteger jobId = new AtomicInteger(-2); DailyJob.scheduleAsync(DummyJobs.createBuilder(DummyJobs.SuccessJob.class), 1000, 2000, new JobRequest.JobScheduledCallback() { @Override public void onJobScheduled(int id, @NonNull String tag, @Nullable Exception exception) { jobId.set(id); } }); waitUntilScheduled(); assertThat(manager().getJobRequest(jobId.get())).isNotNull(); }
/** * Helper method to schedule a daily job on a background thread. This is helpful to avoid IO operations * on the main thread. For more information about scheduling daily jobs see {@link #schedule(JobRequest.Builder, long, long)}. * * <br> * <br> * * In case of a failure an error is logged, but the application doesn't crash. */ public static void scheduleAsync(@NonNull JobRequest.Builder baseBuilder, long startMs, long endMs) { scheduleAsync(baseBuilder, startMs, endMs, JobRequest.DEFAULT_JOB_SCHEDULED_CALLBACK); }