I have some code that loops asynchronously, reading from a database and emitting any new record that it finds:
var foo = new EventEmitter();
foo.stopped = false;
function run() {
readSomeData(function(err, data) {
if (foo.stopped) return;
if (err) foo.emit('error', err);
if (data) foo.emit('data', data);
run();
});
}
run();
What's the best way to test that no more data is emitted when stopped?
Below is one attempt. Is there a way to restructure the code to avoid using
setTimeout
? This test just seems generally fragile.
it('does not emit data when stopped', function(done) {
var count = 0;
foo.on('error', done);
foo.on('data', function(data) {
count++;
if (count === 2) next();
if (count > 2) done(new Error('Data should not have been emitted'));
});
// We'll know that this data has been written when the `data` callback
// is called twice
writeSomeData(mydata1, function(err) {
if (err) done(err);
});
writeSomeData(mydata2, function(err) {
if (err) done(err);
});
function next() {
foo.stopped = true;
writeSomeData(mydata3, function(err) {
if (err) return done(err);
// Data has been written but would it have been read yet
// if `foo` was not stopped? Perhaps not. Wait a little bit
// to ensure we don't get a false positive test.
setTimeout(done, 1000);
});
}
});