signal-desktop/libtextsecure/test/task_with_timeout_test.js

73 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-11-02 18:02:53 +00:00
/* global textsecure */
2018-07-21 21:51:20 +00:00
describe('createTaskWithTimeout', () => {
it('resolves when promise resolves', () => {
2018-11-02 18:02:53 +00:00
const task = () => Promise.resolve('hi!');
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task);
2018-07-21 21:51:20 +00:00
return taskWithTimeout().then(result => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(result, 'hi!');
});
2018-05-02 16:51:22 +00:00
});
2018-07-21 21:51:20 +00:00
it('flows error from promise back', () => {
const error = new Error('original');
2018-11-02 18:02:53 +00:00
const task = () => Promise.reject(error);
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task);
2018-07-21 21:51:20 +00:00
return taskWithTimeout().catch(flowedError => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(error, flowedError);
});
2018-05-02 16:51:22 +00:00
});
2018-11-02 18:02:53 +00:00
it('rejects if promise takes too long (this one logs error to console)', () => {
2018-07-21 21:51:20 +00:00
let complete = false;
2018-11-02 18:02:53 +00:00
const task = () =>
new Promise(resolve => {
2018-07-21 21:51:20 +00:00
setTimeout(() => {
2018-05-02 16:51:22 +00:00
complete = true;
resolve();
}, 3000);
});
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task, this.name, {
2018-05-02 16:51:22 +00:00
timeout: 10,
});
2018-05-02 16:51:22 +00:00
return taskWithTimeout().then(
2018-07-21 21:51:20 +00:00
() => {
2018-05-02 16:51:22 +00:00
throw new Error('it was not supposed to resolve!');
},
2018-07-21 21:51:20 +00:00
() => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(complete, false);
}
);
});
2018-07-21 21:51:20 +00:00
it('resolves if task returns something falsey', () => {
2018-11-02 18:02:53 +00:00
const task = () => {};
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task);
2018-05-02 16:51:22 +00:00
return taskWithTimeout();
});
2018-07-21 21:51:20 +00:00
it('resolves if task returns a non-promise', () => {
2018-11-02 18:02:53 +00:00
const task = () => 'hi!';
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task);
return taskWithTimeout().then(result => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(result, 'hi!');
});
2018-05-02 16:51:22 +00:00
});
2018-11-02 18:02:53 +00:00
it('rejects if task throws (and does not log about taking too long)', () => {
2018-07-21 21:51:20 +00:00
const error = new Error('Task is throwing!');
2018-11-02 18:02:53 +00:00
const task = () => {
2018-05-02 16:51:22 +00:00
throw error;
};
2018-07-21 21:51:20 +00:00
const taskWithTimeout = textsecure.createTaskWithTimeout(task, this.name, {
2018-05-02 16:51:22 +00:00
timeout: 10,
});
2018-05-02 16:51:22 +00:00
return taskWithTimeout().then(
2018-11-02 18:02:53 +00:00
() => {
2018-05-02 16:51:22 +00:00
throw new Error('Overall task should reject!');
},
2018-07-21 21:51:20 +00:00
flowedError => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(flowedError, error);
}
);
});
});