@Override public void tick(TimeLapse timeLapse) { // this should throw an exception to prevent inception to take place getModel().start(); }
/** * Tests that trying to start the time twice is prevented. */ @Test public void testStartStart() { getModel().register(new TickListener() { @Override public void tick(TimeLapse timeLapse) { // this should throw an exception to prevent inception to take place getModel().start(); } @Override public void afterTick(TimeLapse timeLapse) {} }); boolean fail = false; try { getModel().start(); } catch (final IllegalStateException e) { assertThat(e.getMessage()).contains("Time is already ticking."); fail = true; } assertThat(fail).isTrue(); }
/** * Tests that an exception thrown inside a {@link TickListener} is nicely * propagated. */ @Test public void testExceptionPropagation() { getModel().register(new TickListener() { @Override public void tick(TimeLapse timeLapse) { throw new IllegalArgumentException("YOLO"); } @Override public void afterTick(TimeLapse timeLapse) {} }); boolean fail = false; try { getModel().start(); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()).isEqualTo("YOLO"); } assertThat(fail).isTrue(); }
/** * Tests that STOPPED event is dispatched when an exception occurs. */ @Test public void testStoppedEventOnException() { final ListenerEventHistory history = new ListenerEventHistory(); getModel().getEventAPI().addListener(history, Clock.ClockEventType.STOPPED); getModel().register(new TickListener() { @Override public void tick(TimeLapse timeLapse) { if (timeLapse.getStartTime() >= 300) { throw new IllegalStateException("Test"); } } @Override public void afterTick(TimeLapse timeLapse) {} }); boolean failure = false; try { getModel().start(); } catch (final IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Test"); assertThat(history.getEventTypeHistory()).containsExactly( ClockEventType.STOPPED); failure = true; } assertThat(failure).isTrue(); }
/** * Tests that time lapses provided via the listener are behaving as they * should. */ @Test public void timeLapseSafety() { final List<IllegalArgumentException> failures = new ArrayList<>(); getModel().register(new LimitingTickListener(getModel(), 3)); getModel().register(new TickListener() { @Override public void tick(TimeLapse timeLapse) { timeLapse.consume(1L); } @Override public void afterTick(TimeLapse timeLapse) { try { timeLapse.consume(1L); } catch (final IllegalArgumentException e) { failures.add(e); } } }); getModel().start(); assertThat(getModel().isTicking()).isFalse(); assertThat(failures).hasSize(3); }
/** * Basic register/unregister tests. */ @Test public void testTicks() { final TickListenerChecker a = checker(); final LimitingTickListener ltl = new LimitingTickListener(getModel(), 1); a.assertCountEquals(0L); assertThat(getModel().register(a)).isTrue(); assertThat(getModel().register(ltl)).isTrue(); assertThat(getModel().getTickListeners()).containsExactly(a, ltl).inOrder(); getModel().start(); assertThat(getModel().getCurrentTime()).isEqualTo( getModel().getTickLength()); assertThat(a.getTickCount()).isEqualTo(1L); getModel().unregister(a); assertThat(getModel().getTickListeners()).containsExactly(ltl); assertThat(a.getTickCount()).isEqualTo(1L); // re-register assertThat(getModel().register(a)).isTrue(); assertThat(getModel().getTickListeners()).containsExactly(ltl, a).inOrder(); }
/** * Test that listeners are called in order of adding to model. */ @Test public void testTickOrder() { final TickListenerChecker a = checker(); final TickListenerChecker b = checker(); getModel().register(a); getModel().register(b); getModel().register(new LimitingTickListener(getModel(), 1)); getModel().start(); assertThat(b.getLastTickTime() - a.getLastTickTime()).isAtLeast(0L); assertThat(a.getLastAfterTickTime() - b.getLastTickTime()).isAtLeast(0L); assertThat(b.getLastAfterTickTime() - a.getLastAfterTickTime()) .isAtLeast(0L); }
assertThat(intervals).isEmpty(); assertThat(leh.getEventTypeHistory()).isEmpty(); getModel().start(); assertThat(intervals).hasSize(2); assertThat(intervals).containsExactly(
assertThat(getModel().register(new LimitingTickListener(getModel(), 4))) .isTrue(); getModel().start(); assertThat(getModel().getCurrentTime()).isEqualTo( 4 * getModel().getTickLength());