Skip to content

Commit

Permalink
add workaround for randomly failing tests
Browse files Browse the repository at this point in the history
On slower machines (such as the tests slaves of travis) the debounce
will sometimes not reduce all events of a unlink to one call. This is
can lead to weird effects as the first event is a 'change' which
triggers an unexpected read.

For now force a longer debounce when running tests
  • Loading branch information
keis committed Apr 15, 2015
1 parent f31f421 commit f4500ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/reload.js
Expand Up @@ -36,6 +36,7 @@ function ReloadJSON(options) {
this.files = {};
this.watching = {};
this.persistent = options.persistent;
this.delay = options.delay || 10;
}

util.inherits(ReloadJSON, EventEmitter);
Expand Down Expand Up @@ -73,6 +74,7 @@ ReloadJSON.prototype.read = function (path) {
ReloadJSON.prototype.configureWatch = function (path) {
var self = this,
watching = this.watching,
delay = this.delay,
watch;

if (watching[path]) {
Expand All @@ -96,7 +98,7 @@ ReloadJSON.prototype.configureWatch = function (path) {
if (ev === 'change' && !self.files[path]) {
self.read(path);
}
}, 10));
}, delay));

watch.on('error', function (err) {
self.emit('error', err);
Expand Down
5 changes: 5 additions & 0 deletions test/component/reload.coffee
Expand Up @@ -21,10 +21,15 @@ describe "reload-json", ->
beforeEach (done) ->
errorcb = sinon.stub()
reload = new Reload
delay: 50
reload.on 'error', errorcb
fs.writeFile filepath, JSON.stringify(file: 'test.json'), (err) ->
done()

afterEach (done) ->
# wait for debounce
setTimeout done, 50

it "returns the same data when reading the same file", (done) ->
readOne = null
readTwo = null
Expand Down
3 changes: 2 additions & 1 deletion test/unit/reload.coffee
Expand Up @@ -90,7 +90,8 @@ describe "reload-json", ->
reload.configureWatch 'foo'
reload.on 'change', (ev, filename) ->
assert.equal filename, 'foo'
done()
# wait for debounce
setTimeout done, 20

watch.emit 'change', 'change', 'foo'

Expand Down

0 comments on commit f4500ba

Please sign in to comment.