public void testNonStopTimer() throws Exception { long startTime = System.nanoTime(); int loopTmes = 4; long timeout = 500; for (int i = 0; i < loopTmes; i++) { System.out.println("executing loop count" + i); nonStopManager.begin(timeout); try { blockUntilAborted(); } finally { Assert.assertTrue(abortableOperationManager.isAborted()); Thread.currentThread().interrupt(); nonStopManager.finish(); // check that aborted status is cleared. Assert.assertFalse(abortableOperationManager.isAborted()); // check that interrupted flag is cleared. Assert.assertFalse(Thread.interrupted()); } } long timeTaken = System.nanoTime() - startTime; System.out.println("time taken to execute operations " + timeTaken); Assert.assertTrue( (timeTaken >= loopTmes * TimeUnit.MILLISECONDS.toNanos(timeout) && timeTaken < (loopTmes * TimeUnit.MILLISECONDS.toNanos(timeout) + TimeUnit.SECONDS.toNanos(2)))); }
private void blockUntilAborted() { while (true) { try { Thread.currentThread().join(); } catch (InterruptedException e) { if (abortableOperationManager.isAborted()) { return; } } } }