public SynchronousFork(TaskContext taskContext, Object schema, int branches, int index, ExecutionModel executionModel) throws Exception { super(taskContext, schema, branches, index, executionModel); this.autoResetEvent = new AutoResetEvent(); }
@Override public void runOneIteration() { count++; this.autoResetEvent.set(); } }
@Override protected void processRecords() throws IOException, DataConversionException { try { this.autoResetEvent.waitOne(); if (this.throwable != null) { Throwables.propagateIfPossible(this.throwable, IOException.class, DataConversionException.class); throw new RuntimeException(throwable); } } catch (InterruptedException ie) { Throwables.propagate(ie); } }
@Override protected boolean putRecordImpl(Object record) throws InterruptedException { try { this.processRecord(record); } catch (Throwable t) { this.throwable = t; this.autoResetEvent.set(); } return true; }
@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)); }
public Task(String key) { this.key = key; this.autoResetEvent = new AutoResetEvent(false);; }
@Override public void markParentTaskDone() { super.markParentTaskDone(); this.autoResetEvent.set(); }
@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); }
public SynchronousFork(TaskContext taskContext, Object schema, int branches, int index, ExecutionModel executionModel) throws Exception { super(taskContext, schema, branches, index, executionModel); this.autoResetEvent = new AutoResetEvent(); }
@Override public void markParentTaskDone() { super.markParentTaskDone(); this.autoResetEvent.set(); }
@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); }
@Override protected boolean putRecordImpl(Object record) throws InterruptedException { try { this.processRecord(record); } catch (Throwable t) { this.throwable = t; this.autoResetEvent.set(); } return true; }
@Override protected void processRecords() throws IOException, DataConversionException { try { this.autoResetEvent.waitOne(); if (this.throwable != null) { Throwables.propagateIfPossible(this.throwable, IOException.class, DataConversionException.class); throw new RuntimeException(throwable); } } catch (InterruptedException ie) { Throwables.propagate(ie); } }