machinomy
Version:
Micropayments powered by Ethereum
36 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Mutex_1 = require("./Mutex");
const expect = require("expect");
describe('Mutex', () => {
function wait(time) {
return new Promise(resolve => setTimeout(() => resolve(time), time));
}
// use setImmediate to mimic an async operation calling the method
function now(task) {
return new Promise((resolve, reject) => {
setImmediate(() => {
task().then(resolve).catch(reject);
});
});
}
it('should process tasks in order', () => {
let outs = [];
let mutex = new Mutex_1.default();
return Promise.all([
now(() => mutex.synchronize(() => wait(2)).then((num) => outs.push(num))),
now(() => mutex.synchronize(() => wait(7)).then((num) => outs.push(num))),
now(() => mutex.synchronize(() => wait(3)).then((num) => outs.push(num)))
]).then(() => expect(outs).toEqual([2, 7, 3]));
});
it('should process tasks in order by queue name', () => {
let outs = [];
let mutex = new Mutex_1.default();
return Promise.all([
now(() => mutex.synchronizeOn('honk', () => wait(2)).then((num) => outs.push(num))),
now(() => mutex.synchronizeOn('beep', () => wait(7)).then((num) => outs.push(num))),
now(() => mutex.synchronizeOn('honk', () => wait(3)).then((num) => outs.push(num)))
]).then(() => expect(outs).toEqual([2, 3, 7]));
});
});
//# sourceMappingURL=Mutex.test.js.map