Skip to content

Commit

Permalink
createTaskWithTimeout: Don't log expiration if task threw (#1412)
Browse files Browse the repository at this point in the history
FREEBIE
  • Loading branch information
scottnonnenberg committed Aug 30, 2017
1 parent 3f7fbd9 commit 46b64e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 7 additions & 1 deletion js/libtextsecure.js
Original file line number Diff line number Diff line change
Expand Up @@ -40272,7 +40272,13 @@ libsignal.ProvisioningCipher = function() {
return reject(error);
};

var promise = task();
var promise;
try {
promise = task();
} catch(error) {
clearTimer();
throw error;
}
if (!promise || !promise.then) {
clearTimer();
complete = true;
Expand Down
8 changes: 7 additions & 1 deletion libtextsecure/task_with_timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@
return reject(error);
};

var promise = task();
var promise;
try {
promise = task();
} catch(error) {
clearTimer();
throw error;
}
if (!promise || !promise.then) {
clearTimer();
complete = true;
Expand Down
18 changes: 16 additions & 2 deletions libtextsecure/test/task_with_timeout_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ describe('createTaskWithTimeout', function() {
assert.strictEqual(error, flowedError);
});
});
it('rejects if promise takes too long', function() {
it('rejects if promise takes too long (this one logs error to console)', function() {
var error = new Error('original');
var complete = false;
var task = function() {
return new Promise(function(resolve) {
setTimeout(function() {
completed = true;
complete = true;
resolve();
}, 3000);
});
Expand Down Expand Up @@ -57,4 +57,18 @@ describe('createTaskWithTimeout', function() {
assert.strictEqual(result, 'hi!')
});
});
it('rejects if task throws (and does not log about taking too long)', function() {
var error = new Error('Task is throwing!');
var task = function() {
throw error;
};
var taskWithTimeout = textsecure.createTaskWithTimeout(task, this.name, {
timeout: 10
});
return taskWithTimeout().then(function(result) {
throw new Error('Overall task should reject!')
}, function(flowedError) {
assert.strictEqual(flowedError, error);
});
});
});

0 comments on commit 46b64e3

Please sign in to comment.