DrPaulBrewer / single-market-robot-simulator

Runs numerical experiments for a single market populated by robot traders
Other
7 stars 5 forks source link

immediate deadline test fails intermittently #24

Closed DrPaulBrewer closed 4 years ago

DrPaulBrewer commented 4 years ago

One of the minor tests, described as

deadline: run Simulation sync:true with immediate deadline, request 10 periods of single unit trade scenario only yields one period'

fails intermittently.

The specific part of the test that fails is

it("sim.config.periods should be reduced to 1 period", function () { S.config.periods.should.equal(1); });

Example

     AssertionError: expected 2 to be 1
      + expected - actual

      -2
      +1

      at Assertion.fail (node_modules/should/cjs/should.js:275:17)
      at Assertion.equal (node_modules/should/cjs/should.js:356:19)
      at Context.<anonymous> (test/index.js:1019:31)
DrPaulBrewer commented 4 years ago

What is failing ?

No market functionality is involved

This tests what happens if we start a simulation with an immediate deadline.

What the test expects to happen is to receive 1 period of results.

Instead, sometimes the system is fast enough to return 2 periods.

That fails the test.

Fix

The checking of the deadline has a minor defect.

The simulation code in src/index.js, function run(options) looks at if ((deadline) && (Date.now()>deadline)) and the "strict greater than" > will not end the simulation when Date.now()===deadline. Although Date.now() should have a 1 ms precision, we don't know how well the system keeps it updated... especially since the highest rate of these was on an HP with an older i5-3000s series processor ... but apparently it is possible to process 2 periods before Date.now() updates to be greater than deadline.

The fix is to use Date.now()>=deadline (greater than or equal to) to check the deadline.

This requires two minor changes to the run() function, once for the synchronous options and once in the asynchronous option.

DrPaulBrewer commented 4 years ago

Minor fix to deadline code released as version 5.7.0