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
  • Loading branch information
addaleax committed Sep 5, 2017
1 parent 3a97e88 commit eb3cbac
Show file tree
Hide file tree
Showing 4 changed files with 73 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 } = require('worker');
const { Server } = require('net');
const fs = require('fs');

if (process.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);
process.postMessage(server._handle.fd);
server.unref();
}
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 } = require('worker');

if (process.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 } = require('worker');

if (process.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 } = require('worker');

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

0 comments on commit eb3cbac

Please sign in to comment.