SuperstepKickoffLatch { public void triggerNextSuperstep() { synchronized (monitor) { if (terminated) { throw new IllegalStateException("Already terminated."); } superstepNumber++; monitor.notifyAll(); } }  void triggerNextSuperstep(); void signalTermination(); boolean awaitStartOfSuperstepOrTermination(int superstep);  }
@Test public void testWaitFromOne() { try { SuperstepKickoffLatch latch = new SuperstepKickoffLatch(); Waiter w = new Waiter(latch, 2); Thread waiter = new Thread(w); waiter.setDaemon(true); waiter.start(); WatchDog wd = new WatchDog(waiter, 2000); wd.start(); Thread.sleep(100); latch.triggerNextSuperstep(); wd.join(); if (wd.getError() != null) { throw wd.getError(); } if (w.getError() != null) { throw w.getError(); } } catch (Throwable t) { t.printStackTrace(); Assert.fail("Error: " + t.getMessage()); } }
@Test public void testWaitAlreadyFulfilled() { try { SuperstepKickoffLatch latch = new SuperstepKickoffLatch(); latch.triggerNextSuperstep(); Waiter w = new Waiter(latch, 2); Thread waiter = new Thread(w); waiter.setDaemon(true); waiter.start(); WatchDog wd = new WatchDog(waiter, 2000); wd.start(); Thread.sleep(100); wd.join(); if (wd.getError() != null) { throw wd.getError(); } if (w.getError() != null) { throw w.getError(); } } catch (Throwable t) { t.printStackTrace(); Assert.fail("Error: " + t.getMessage()); } }
@Test public void testWaitIncorrectAsync() { try { SuperstepKickoffLatch latch = new SuperstepKickoffLatch(); latch.triggerNextSuperstep(); latch.triggerNextSuperstep(); Waiter w = new Waiter(latch, 2); Thread waiter = new Thread(w); waiter.setDaemon(true); waiter.start(); WatchDog wd = new WatchDog(waiter, 2000); wd.start(); Thread.sleep(100); wd.join(); if (wd.getError() != null) { throw wd.getError(); } if (w.getError() != null) { if (!(w.getError() instanceof IllegalStateException)) { throw new Exception("wrong exception type " + w.getError()); } } else { Assert.fail("should cause exception"); } } catch (Throwable t) { t.printStackTrace(); Assert.fail("Error: " + t.getMessage()); } }