final Path destLogFile = new Path(this.destLogDir, destLogFileName); this.scheduler.schedule(new LogCopyTask(srcLogFile, destLogFile), this.copyInterval, this.timeUnit);
@Test public void testTaskRunsOnSchedule() throws IOException, InterruptedException { long startTime; long endTime; Task task = new Task("foo"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { startTime = System.currentTimeMillis(); taskScheduler.schedule(task, 1, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); endTime = System.currentTimeMillis(); } Assert.assertEquals(task.getCount(), 3); Assert.assertTrue(endTime - startTime >= (scheduleFiresImmediately ? 2000 : 3000)); }
@Test public void testScheduledTasksAreRetrievableAndCancellable() throws IOException, InterruptedException { Task task1 = new Task("foo"); Task task2 = new Task("bar"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { taskScheduler.schedule(task1, 1, TimeUnit.SECONDS); taskScheduler.schedule(task2, 1, TimeUnit.SECONDS); task1.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); task2.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); Optional<Task> foo = taskScheduler.getScheduledTask("foo"); Assert.assertTrue(foo.isPresent()); Iterable<Task> all = taskScheduler.getScheduledTasks(); Assert.assertEquals(Iterables.size(all), 2); taskScheduler.cancel(foo.get()); foo = taskScheduler.getScheduledTask("foo"); Assert.assertFalse(foo.isPresent()); all = taskScheduler.getScheduledTasks(); Assert.assertEquals(Iterables.size(all), 1); task2.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); } Assert.assertEquals(task1.getCount(), 1); Assert.assertEquals(task2.getCount(), 2); }
@Test public void testCloseCancelsTasks() throws IOException, InterruptedException { Task task = new Task("foo"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { taskScheduler.schedule(task, 1, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); } task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); Assert.assertEquals(task.getCount(), 1); }
final Path destLogFile = new Path(this.destLogDir, destLogFileName); this.scheduler.schedule(new LogCopyTask(srcLogFile, destLogFile), this.copyInterval, this.timeUnit);