Fast CPU masks test errors

69 Views Asked by At

I have a Node.js application which I'm testing with Mocha on a fast(ish) dev machine. I've noticed that sometimes the fast CPU will mask some errors. If the tests are run on a machine with a slower CPU these errors starting showing up.

Question: Is there an easy way to temporarily slow down or simulate a slow down in CPU processing to surface these errors? Or way to run these tests at full speed and still discover this type of error?

1

There are 1 best solutions below

0
On

One possible reason for these discrepancies is that certain functions might take longer to run depending on the machine they're running on, for instance if it involves heavy computation, or reading from a DB. This might modify the order in which callbacks are called.

To work around this problem, you can get more control over the order in which parallel sequences of operations are run by using (for instance) Sinon.js in your tests: it has great spy/stub features, and also ships fake timers.

By mocking (stubbing) the async functions which take time to run, you can remove the speed factor (machine-dependent). Also, fake timers allow getting control over functions wrapped in setTimeout or setInterval