Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
test: add basic tests for worker
Browse files Browse the repository at this point in the history
PR-URL: #114
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Olivia Hugger <olivia@fastmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
  • Loading branch information
addaleax committed Nov 2, 2017
1 parent 717ca76 commit f4606e1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/parallel/test-worker-cleanup-handles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread, postMessage } = require('worker');
const { Server } = require('net');
const fs = require('fs');

if (isMainThread) {
const w = new Worker(__filename);
let fd = null;
w.on('message', common.mustCall((fd_) => {
assert.strictEqual(typeof fd_, 'number');
fd = fd_;
}));
w.on('exit', common.mustCall((code) => {
assert.throws(() => fs.fstatSync(fd),
common.expectsError({ code: 'EBADF' }));
}));
} else {
const server = new Server();
server.listen(0);
postMessage(server._handle.fd);
server.unref();
}
14 changes: 14 additions & 0 deletions test/parallel/test-worker-dns-terminate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const { Worker } = require('worker');

const w = new Worker(`
const dns = require('dns');
dns.lookup('nonexistent.org', () => {});
require('worker').postMessage('0');
`, { eval: true });

w.on('message', common.mustCall(() => {
// This should not crash the worker during a DNS request.
w.terminate(common.mustCall());
}));
17 changes: 17 additions & 0 deletions test/parallel/test-worker-uncaught-exception-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread } = require('worker');

if (isMainThread) {
const w = new Worker(__filename);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
// TODO(addaleax): be more specific here
assert(/foo/.test(err));
}));
} else {
setImmediate(() => {
throw new Error('foo');
});
}
15 changes: 15 additions & 0 deletions test/parallel/test-worker-uncaught-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread } = require('worker');

if (isMainThread) {
const w = new Worker(__filename);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
// TODO(addaleax): be more specific here
assert(/foo/.test(err));
}));
} else {
throw new Error('foo');
}
17 changes: 17 additions & 0 deletions test/parallel/test-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread, postMessage } = require('worker');

if (isMainThread) {
const w = new Worker(__filename);
w.on('message', common.mustCall((message) => {
assert.strictEqual(message, 'Hello, world!');
}));
} else {
setImmediate(() => {
process.nextTick(() => {
postMessage('Hello, world!');
});
});
}

0 comments on commit f4606e1

Please sign in to comment.