2017-07-16 02:51:59 -04:00
|
|
|
const DispatchTestService = require('../fixtures/dispatch-test-service');
|
2024-03-04 11:39:17 -05:00
|
|
|
const Worker = require('web-worker');
|
2017-07-16 02:51:59 -04:00
|
|
|
|
|
|
|
const dispatch = require('../../src/dispatch/central-dispatch');
|
|
|
|
const path = require('path');
|
|
|
|
const test = require('tap').test;
|
|
|
|
|
|
|
|
|
|
|
|
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
|
|
|
|
dispatch.workerClass = Worker;
|
|
|
|
|
|
|
|
const runServiceTest = function (serviceName, t) {
|
|
|
|
const promises = [];
|
|
|
|
|
2017-08-05 02:52:31 -04:00
|
|
|
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),
|
|
|
|
e => t.fail(e)
|
|
|
|
));
|
|
|
|
|
|
|
|
promises.push(dispatch.call(serviceName, 'doubleArgument', 123)
|
|
|
|
.then(
|
|
|
|
x => t.equal(x, 246),
|
|
|
|
e => t.fail(e)
|
|
|
|
));
|
2017-07-16 02:51:59 -04:00
|
|
|
|
|
|
|
// I tried using `t.rejects` here but ran into https://github.com/tapjs/node-tap/issues/384
|
|
|
|
promises.push(dispatch.call(serviceName, 'throwException')
|
2017-08-05 02:52:31 -04:00
|
|
|
.then(
|
|
|
|
() => t.fail('exception was not propagated as expected'),
|
|
|
|
() => t.pass('exception was propagated as expected')
|
|
|
|
));
|
2017-07-16 02:51:59 -04:00
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
};
|
|
|
|
|
|
|
|
test('local', t => {
|
2017-08-05 02:52:31 -04:00
|
|
|
dispatch.setService('LocalDispatchTest', new DispatchTestService())
|
|
|
|
.catch(e => t.fail(e));
|
2017-07-16 02:51:59 -04:00
|
|
|
|
|
|
|
return runServiceTest('LocalDispatchTest', t);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('remote', t => {
|
|
|
|
const fixturesDir = path.resolve(__dirname, '../fixtures');
|
2018-11-19 14:57:43 -05:00
|
|
|
const shimPath = path.resolve(fixturesDir, 'dispatch-test-worker-shim.js');
|
|
|
|
const worker = new Worker(shimPath, null, {cwd: fixturesDir});
|
2017-07-16 02:51:59 -04:00
|
|
|
dispatch.addWorker(worker);
|
|
|
|
|
|
|
|
const waitForWorker = new Promise(resolve => {
|
2017-08-05 02:52:31 -04:00
|
|
|
dispatch.setService('test', {onWorkerReady: resolve})
|
|
|
|
.catch(e => t.fail(e));
|
2017-07-16 02:51:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return waitForWorker
|
2017-08-05 02:52:31 -04:00
|
|
|
.then(() => runServiceTest('RemoteDispatchTest', t), e => t.fail(e))
|
|
|
|
.then(() => dispatch._remoteCall(worker, 'dispatch', 'terminate'), e => t.fail(e));
|
2017-07-16 02:51:59 -04:00
|
|
|
});
|
2019-03-22 12:13:37 -04:00
|
|
|
|
|
|
|
test('local, sync', t => {
|
|
|
|
dispatch.setServiceSync('SyncDispatchTest', new DispatchTestService());
|
|
|
|
|
|
|
|
const a = dispatch.callSync('SyncDispatchTest', 'returnFortyTwo');
|
|
|
|
t.equal(a, 42);
|
|
|
|
|
|
|
|
const b = dispatch.callSync('SyncDispatchTest', 'doubleArgument', 9);
|
|
|
|
t.equal(b, 18);
|
|
|
|
|
|
|
|
const c = dispatch.callSync('SyncDispatchTest', 'doubleArgument', 123);
|
|
|
|
t.equal(c, 246);
|
|
|
|
|
2019-03-26 12:03:00 -04:00
|
|
|
t.throws(() => dispatch.callSync('SyncDispatchTest', 'throwException'),
|
|
|
|
new Error('This is a test exception thrown by DispatchTest'));
|
2019-03-22 12:13:37 -04:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|