RunnableContext implements Context, Runnable { @Override public void run() { if (times == INFINITE || times > 0) { task.accept(this); if (times > 0) { times--; } } else { cancel(); } } RunnableContext(Consumer<Context> task, long times); @Override void run(); @Override void cancel(); @Override long times();  }
@Test void run() { runnable.run(); verify(consumer).accept(runnable); assertEquals(0, runnable.times()); runnable.run(); assertEquals(0, runnable.times()); verify(future).cancel(false); }