0

So here's the rub - I have a system that, when an event occurs, fires off a chain of disparate asynchronous code (some code even fires off more events).

Now during acceptance testing I fire that same event - but what strategy should use to notify the test runner that all the subsequent events are finished and ready to move on to the next step?

I started out just waiting for fixed amount of time - but I that was always just a fudge. And now, I'm hooking in to the tail events and moving on when they have all finished. But I can see this becoming v. complex as the system grows.

Just wondering if there is an alternative strategy that I've missed. Any suggestions?

FWIW I'm using cucumber.js & zombie to test an express app on node.js.

Cheers and thanks for your time,

Gordon

1 Answers1

1

Obviously, the solution will be different depending on the application, but I find it helpful to architecture my applications in such a way that callbacks for the top-level asynchronous functions are tracked all the way to the end. Once all the top-level callbacks are done, you can fire some event or call some callback that indicates everything is over. Using something like async's parallel, you could potentially do something like this:

eventEmitter.on('someEvent', function(some, data, callback) {
  async.parallel([
    function(cb) { firstAsyncThing(some, cb); },
    function(cb) { secondAsyncThing(data, cb); }
  ], function(err, results) {
    // called when all functions passed in the array have their `cb` called
    callback(err, results);
  });
});

So then you can pass a callback into your event:

eventEmitter.emit('someEvent', 'some', 'data', function(error, results) {
  // the event has been fully handled
});
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • parallel looks very useful - I'll give it a whirl. And I'll start passing a callback thru the chain of events and see how that works out. Thanks! – Gordon McAllister May 25 '12 at 19:21