### Question:
Job implements Delayed { public void cancel() { this.canceled = true; } Job(JobScheduler jobScheduler, String name, long intervalInMilli, JobMethod jobMethod); void execute(); void cancel(); boolean isCanceled(); void setNextTimestampInMilli(); @Override long getDelay(TimeUnit unit); @Override int compareTo(Delayed o); boolean isOneTimeJob(); long getIntervalInMilli(); void setIntervalInMilli(long intervalInMilli); final JobMethod jobMethod; final String name; }### Answer:
@Test public void testAddJob() throws InterruptedException { JobScheduler jobScheduler = new JobScheduler("test"); jobScheduler.start(); final int[] job1_flag = {0}; final int[] job2_flag = {0}; Job job1 = jobScheduler.addJob("abc", 50, () -> job1_flag[0] += 1); Job job2 = jobScheduler.addJob("def", 50, () -> job2_flag[0] += 1); Thread.sleep(10); Assert.assertEquals(job1_flag[0], 1); Assert.assertEquals(job2_flag[0], 1); Thread.sleep(20); Assert.assertEquals(job1_flag[0], 1); Assert.assertEquals(job2_flag[0], 1); Thread.sleep(30); Assert.assertEquals(job1_flag[0], 2); Assert.assertEquals(job2_flag[0], 2); job1.cancel(); job2.cancel(); Thread.sleep(60); Assert.assertEquals(job1_flag[0], 2); Assert.assertEquals(job2_flag[0], 2); }
@Test public void testAddFirstDelayJob() throws InterruptedException { JobScheduler jobScheduler = new JobScheduler("testAddFirstDelayJob"); jobScheduler.start(); final int[] job3_flag_testFirstDelayJob = {0}; Job job3 = jobScheduler.addFirstDelayJob("def", 50, () -> job3_flag_testFirstDelayJob[0] += 1); Thread.sleep(30); Assert.assertEquals(job3_flag_testFirstDelayJob[0], 0); Thread.sleep(30); Assert.assertEquals(job3_flag_testFirstDelayJob[0], 1); Thread.sleep(60); Assert.assertEquals(job3_flag_testFirstDelayJob[0], 2); job3.cancel(); Thread.sleep(60); Assert.assertEquals(job3_flag_testFirstDelayJob[0], 2); }