npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

interleavings

v1.0.0

Published

Test async systems by reordering callbacks deterministically but randomly.

Downloads

236

Readme

interleavings

Test async systems by reordering callbacks deterministically but randomly.

introduction

In node you write async code, and if you want working async code you need to test it too. But how do you test it? lets say you have something like this:

//call two async functions in parallel,
//then get a result when they have BOTH finished.
both([
  function A (cb) {
    //notice this calls back syncly - we'll get to that soon.
    cb(null, 1)
  },
  function B (cb) {
    cb(null, 2)
  }
],
function (err, values) {
  //err => null
  //values => [1, 2]
  assert.notOk(err)
  assert.deepEqual(values, [1, 2])
})

If each callback is called asyncly, that also means they may be called in any order. A could be called first, or B could be called first - this is what "async" means. Sometimes subtle bugs occur when async things happen in particular orders (aka. "race condition")

So, we need to write test cases that cover at least more than one order, This is okay when there are only two async calls - there are 2 orderings. But as the number of parallel calls increases, the possible orderings increases dramaticall! If there are 5 parallel calls there 120 orderings and for 7 there are 5040!!!

In general - N parallel async calls give N! possible orderings.

It would be easy to miss such a race condition in your testcase!

a solution

Just try different orderings randomly! (but deterministically, so if you find an error, you can reproduce it) You must wrap each callback with the async function to randomly order it. Then, interleavings will run the test many times (default is 100) and check how many times the test passed.

interleavings.test(function (async) {
  both([
    function A (cb) {
      //wrap each callback in `async`,
      //it is now randomly ordered.
      async(cb) (null, 1)
    },
    function B (cb) {
      async(cb) (null, 2)
    }
  ],
  function (err, values) {
    //err => null
    //values => [1, 2]
    assert.notOk(err)
    assert.deepEqual(values, [1, 2])
  })
})

If it passed every time - your code might work - so write some more tests to check. If it fails every time, it's just plain broken. But if it only fails sometimes you have found a race condition!

determinism

If you want your test to be reproducable it must be deterministic. This means any IO must be mocked, and you should not use setTimeout. If you use a nextTick or setImmediate that is probably acceptable. You may sometimes callback syncly - if the callback is wrapped by async(cb) then sometimes it will call it immediately, so you will still test interleavings where sync calls happen.

INTLVR=runs

set the number of times to try different interleavings for each test.

INTLVS=seed

run the test only once - with the given seed.

License

MIT