Koobz
Koobz

Reputation: 6938

Vows: Testing Asynchronous Interleaving

Is there a methodology to test (potential) interleaving of asynchronous functions with vows?

For example:

// Topic portion
var user = new User('jacob')
user.set('email,'[email protected]')
user.save() // a
user.set('email',[email protected]')
user.save() // b
user.refresh(this.callback) // Reload from database

// Callback
assert.equals(user.email,'[email protected]')

There is could be a race condition between the two saves. When writing my tests I want to ensure that my API is ensuring that b finishes last (and that we have the correct final value for the email). With the way that's it written, the test will pass coincidentally some of the time.

Upvotes: 1

Views: 692

Answers (1)

Chris Biscardi
Chris Biscardi

Reputation: 3153

Heres the example from the vows docs:

The nested contexts act as nested callbacks and pass the return arguments to the next context.

Docs: http://vowsjs.org/

{ topic: function () {
    fs.stat('~/FILE', this.callback);
  },
  'after a successful `fs.stat`': {
    topic: function (stat) {
      fs.open('~/FILE', "r", stat.mode, this.callback);
    },
    'after a successful `fs.open`': {
      topic: function (fd, stat) {
        fs.read(fd, stat.size, 0, "utf8", this.callback);
      },
      'we can `fs.read` to get the file contents': function (data) {
        assert.isString (data);
      }
    }
  }
}

Upvotes: 1

Related Questions