Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of 'step' method #87

Open
pictavien opened this issue Dec 23, 2019 · 1 comment
Open

Usage of 'step' method #87

pictavien opened this issue Dec 23, 2019 · 1 comment
Milestone

Comments

@pictavien
Copy link
Collaborator

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());
    }
}
@remikey
Copy link
Member

remikey commented Feb 3, 2020

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 remikey added this to the ??? milestone Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants