@CheckReturnValue @Override public TimeModel build(DependencyProvider dependencyProvider) { return new SimulatedTimeModel(this); }
@Override void doStart() { try { while (isTicking()) { tickImpl(); } } catch (final RuntimeException e) { cleanUpAfterException(); throw e; } }
/** * Tests that single ticks works. */ @Test public void testSingleTick() { final TickListenerChecker checker = new TickListenerChecker( getModel().getTickLength(), getModel().getTimeUnit()); getModel().register(checker); assertThat(getModel().getCurrentTime()).isEqualTo(0L); getModel().tick(); assertThat(getModel().getCurrentTime()) .isEqualTo(getModel().getTickLength()); assertThat(checker.getTickCount()).isEqualTo(1); }
/** * Test starting and stopping time. */ @Test public void testStartStop() { final LimitingTickListener ltl = new LimitingTickListener(getModel(), 3); final ListenerEventHistory leh = new ListenerEventHistory(); getModel().getEventAPI().addListener(leh, ClockEventType.values()); getModel().register(ltl); getModel().start(); assertEquals(3 * getModel().getTickLength(), getModel().getCurrentTime()); getModel().start(); assertEquals(6 * getModel().getTickLength(), getModel().getCurrentTime()); assertThat(leh.getEventTypeHistory()).isEqualTo( asList( ClockEventType.STARTED, ClockEventType.STOPPED, ClockEventType.STARTED, ClockEventType.STOPPED)); }
/** * Tests that the time model correctly stops when receiving an interrupt. */ @Test public void testInterrupt() { final Thread main = Thread.currentThread(); Executors.newScheduledThreadPool(1).schedule(new Runnable() { @Override public void run() { main.interrupt(); } }, 100, TimeUnit.MILLISECONDS); getModel().start(); // the actual time is hardware dependent as it depends on how many ticks are // computed in the period before the interrupt is received assertThat(getModel().getCurrentTime()).isGreaterThan(0L); } }
/** * Test for provided types. */ @Test public void testProvidingTypes() { assertThat(getModel().get(Clock.class)).isNotNull(); assertThat(getModel().get(ClockController.class)).isNotNull(); boolean fail = false; try { getModel().get(RealtimeClockController.class); } catch (final IllegalArgumentException e) { fail = true; assertThat(e.getMessage()).contains( "does not provide instances of com.github.rinde.rinsim.core.model.time.RealtimeClockController"); } assertThat(fail).isTrue(); }