JBotSim
JBotSim copied to clipboard
Usage of 'step' method
Hi,
I've written a small test that plays with Topology.step() method. The aim of the test is to check that N invocations of the method should yield N invocations of the onClock method of a ClockListener. Both assertions at the end of the test fail. Where do I make a mistake ?
import io.jbotsim.core.event.ClockListener;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class TopologyStepTest {
public static class CycleCounter implements ClockListener {
public int nbCycles = 0;
@Override
public void onClock() {
nbCycles++;
}
}
@Test
void test_Step() {
int N = 10;
Topology tp = new Topology();
CycleCounter cc = new CycleCounter();
tp.addClockListener(cc);
for (int r = 0; r < N; r++) {
tp.step();
}
assertEquals(N, cc.nbCycles);
assertFalse(tp.isRunning());
}
}
My two cents with my understanding of the situation:
- Since no
Clockhas been provided, theClockManagerspawns aDefaultClock. - This
Clock's implementation is based on a simpleThreadand conditions. - The step mechanism is basically " Call
resume()while instep(), andpause()during the nextTopology.onClock()" .
The clock thread simply does not have the time to wake up. Adding a System.sleep(500) to you loop "fixes" the test.
Note: using a JClock won't help, here.
I suppose that the step mechanism has originally been added in a UI-oriented context, and not designed to handle strong thread competition without help from the developer?