From f4500ba77b067a8817f0e1fe34fc45763da8022a Mon Sep 17 00:00:00 2001 From: David Keijser Date: Wed, 15 Apr 2015 09:45:50 +0200 Subject: [PATCH] add workaround for randomly failing tests 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 --- lib/reload.js | 4 +++- test/component/reload.coffee | 5 +++++ test/unit/reload.coffee | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/reload.js b/lib/reload.js index 26494b8..2fe6290 100644 --- a/lib/reload.js +++ b/lib/reload.js @@ -36,6 +36,7 @@ function ReloadJSON(options) { this.files = {}; this.watching = {}; this.persistent = options.persistent; + this.delay = options.delay || 10; } util.inherits(ReloadJSON, EventEmitter); @@ -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]) { @@ -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); diff --git a/test/component/reload.coffee b/test/component/reload.coffee index bf6c675..ced558b 100644 --- a/test/component/reload.coffee +++ b/test/component/reload.coffee @@ -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 diff --git a/test/unit/reload.coffee b/test/unit/reload.coffee index 822ab4c..8226ccf 100644 --- a/test/unit/reload.coffee +++ b/test/unit/reload.coffee @@ -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'