-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Improved callback timeout to ensure all callbacks and promises timeout.
- Loading branch information
1 parent
1b53622
commit df1f565
Showing
16 changed files
with
221 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
jest.dontMock('../callbackPromiseHandler'); | ||
|
||
var CallbackPromiseHandler = require('../callbackPromiseHandler'); | ||
|
||
describe("CallbackPromiseHandler", function() { | ||
it("should push promise into queue", function() { | ||
var promiseHandler = new CallbackPromiseHandler(); | ||
promiseHandler.push(function() { }); | ||
expect(promiseHandler.promises.length).toEqual(1); | ||
}); | ||
|
||
it("should handle pushing a null promise", function() { | ||
var promiseHandler = new CallbackPromiseHandler(); | ||
promiseHandler.push(null); | ||
}); | ||
|
||
it("should call all callbacks when completePromises is called", function(done) { | ||
var promiseHandler = new CallbackPromiseHandler(); | ||
var value = 610; | ||
promiseHandler.push(null); | ||
promiseHandler.push(function(returnValue) { | ||
expect(returnValue).toEqual(value); | ||
done(); | ||
}); | ||
promiseHandler.push(); | ||
|
||
setTimeout(function() { | ||
promiseHandler.completePromises(value); | ||
}, 500); | ||
}); | ||
|
||
it("should call all callbacks if completePromises is not called", function(done) { | ||
var promiseHandler = new CallbackPromiseHandler(); | ||
promiseHandler.push(null); | ||
promiseHandler.push(function(returnValue) { | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var log = require('../lib/logger'); | ||
var request = require('../api/base'); | ||
|
||
// This class holds callback promises and ensures that they will get called | ||
// before the request.timeout time. | ||
function CallbackPromiseHandler(opts) { | ||
this.promises = []; | ||
this.name = opts ? opts.name : null; | ||
} | ||
|
||
CallbackPromiseHandler.prototype.push = function(callback) { | ||
var hasExecutedCallback = false; | ||
var self = this; | ||
|
||
function executeCallback(value) { | ||
if (hasExecutedCallback) return; | ||
|
||
if (callback) { | ||
hasExecutedCallback = true; | ||
callback(value); | ||
} | ||
} | ||
|
||
this.promises.push(executeCallback); | ||
|
||
setTimeout(function() { | ||
if (!hasExecutedCallback) { | ||
log.log(self.name + " promise timed out", null, log.LOG); | ||
executeCallback(); | ||
} | ||
}, request.timeout); | ||
} | ||
|
||
CallbackPromiseHandler.prototype.completePromises = function(value) { | ||
for (var i=0; i < this.promises.length; i++) { | ||
var promise = this.promises[i]; | ||
if (promise) promise(value); | ||
} | ||
this.promises = []; | ||
} | ||
|
||
module.exports = CallbackPromiseHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.