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

test: add basic tests for worker #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!');
});
});
}