Refactor common code in {central,worker}-dispatch

Central dispatch and worker dispatch share most of their code now by
inheriting from a new shared dispatch class.

Also, the format of passed messages has been altered to make it easier
to understand in a debugger.
This commit is contained in:
Christopher Willis-Ford 2017-08-04 23:52:31 -07:00
parent 97d67d75f4
commit b4c0cfe940
6 changed files with 328 additions and 258 deletions
test/unit

View file

@ -12,31 +12,37 @@ dispatch.workerClass = Worker;
const runServiceTest = function (serviceName, t) {
const promises = [];
promises.push(dispatch.call(serviceName, 'returnFortyTwo').then(x => {
t.equal(x, 42);
}));
promises.push(dispatch.call(serviceName, 'returnFortyTwo')
.then(
x => t.equal(x, 42),
e => t.fail(e)
));
promises.push(dispatch.call(serviceName, 'doubleArgument', 9).then(x => {
t.equal(x, 18);
}));
promises.push(dispatch.call(serviceName, 'doubleArgument', 9)
.then(
x => t.equal(x, 18),
e => t.fail(e)
));
promises.push(dispatch.call(serviceName, 'doubleArgument', 123).then(x => {
t.equal(x, 246);
}));
promises.push(dispatch.call(serviceName, 'doubleArgument', 123)
.then(
x => t.equal(x, 246),
e => t.fail(e)
));
// I tried using `t.rejects` here but ran into https://github.com/tapjs/node-tap/issues/384
promises.push(dispatch.call(serviceName, 'throwException')
.then(() => {
t.fail('exception was not propagated as expected');
}, () => {
t.pass('exception was propagated as expected');
}));
.then(
() => t.fail('exception was not propagated as expected'),
() => t.pass('exception was propagated as expected')
));
return Promise.all(promises);
};
test('local', t => {
dispatch.setService('LocalDispatchTest', new DispatchTestService());
dispatch.setService('LocalDispatchTest', new DispatchTestService())
.catch(e => t.fail(e));
return runServiceTest('LocalDispatchTest', t);
});
@ -47,15 +53,11 @@ test('remote', t => {
dispatch.addWorker(worker);
const waitForWorker = new Promise(resolve => {
dispatch.setService('test', {
onWorkerReady: resolve
});
dispatch.setService('test', {onWorkerReady: resolve})
.catch(e => t.fail(e));
});
return waitForWorker
.then(() => runServiceTest('RemoteDispatchTest', t))
.then(() => {
// Allow some time for the worker to finish, then terminate it
setTimeout(() => dispatch.call('RemoteDispatchTest', 'close'), 10);
});
.then(() => runServiceTest('RemoteDispatchTest', t), e => t.fail(e))
.then(() => dispatch._remoteCall(worker, 'dispatch', 'terminate'), e => t.fail(e));
});