@Override
    public void cancel() {
        synchronized (monitor) {
            if (!periodicExecutionCancellation.isPresent()) {
                throw new IllegalStateException("setPeriodicExecutionCancellationCallback has not been called before cancel");
            }

            cancelled = true;
            if (running) return;
        }

        runlet.close();
        periodicExecutionCancellation.get().run();
    }
@Test
    public void testCancelWhileIdle() {
        // Cancel while runlet is not running and verify closure and executor cancellation
        cancellable.cancel();
        assertFalse(executor.isExecutionRunning());
        assertTrue(runlet.isClosed());

        // Ensure a spurious run is ignored.
        executor.runAsync();
        executor.runToCompletion(3);
        assertEquals(2, runlet.getRunsStarted());
    }