JBotSim icon indicating copy to clipboard operation
JBotSim copied to clipboard

Usage of 'step' method

Open pictavien opened this issue 5 years ago • 1 comments

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());
    }
}

pictavien avatar Dec 23 '19 15:12 pictavien

My two cents with my understanding of the situation:

  • Since no Clock has been provided, the ClockManager spawns a DefaultClock.
  • This Clock's implementation is based on a simple Thread and conditions.
  • The step mechanism is basically " Call resume() while in step(), and pause() during the next Topology.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?

remikey avatar Feb 03 '20 08:02 remikey