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

Commit 9bd64ba

Browse files
committed
test: add basic tests for worker
1 parent d7ade3b commit 9bd64ba

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread, postMessage } = require('worker');
5+
const { Server } = require('net');
6+
const fs = require('fs');
7+
8+
if (isMainThread) {
9+
const w = new Worker(__filename);
10+
let fd = null;
11+
w.on('message', common.mustCall((fd_) => {
12+
assert.strictEqual(typeof fd_, 'number');
13+
fd = fd_;
14+
}));
15+
w.on('exit', common.mustCall((code) => {
16+
assert.throws(() => fs.fstatSync(fd),
17+
common.expectsError({ code: 'EBADF' }));
18+
}));
19+
} else {
20+
const server = new Server();
21+
server.listen(0);
22+
postMessage(server._handle.fd);
23+
server.unref();
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker');
4+
5+
const w = new Worker(`
6+
const dns = require('dns');
7+
dns.lookup('nonexistent.org', () => {});
8+
require('worker').postMessage('0');
9+
`, { eval: true });
10+
11+
w.on('message', common.mustCall(() => {
12+
w.terminate(common.mustCall());
13+
}));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread } = require('worker');
5+
6+
if (isMainThread) {
7+
const w = new Worker(__filename);
8+
w.on('message', common.mustNotCall());
9+
w.on('error', common.mustCall((err) => {
10+
// TODO(addaleax): be more specific here
11+
assert(/foo/.test(err));
12+
}));
13+
} else {
14+
setImmediate(() => {
15+
throw new Error('foo');
16+
});
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread } = require('worker');
5+
6+
if (isMainThread) {
7+
const w = new Worker(__filename);
8+
w.on('message', common.mustNotCall());
9+
w.on('error', common.mustCall((err) => {
10+
// TODO(addaleax): be more specific here
11+
assert(/foo/.test(err));
12+
}));
13+
} else {
14+
throw new Error('foo');
15+
}

test/parallel/test-worker.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread, postMessage } = require('worker');
5+
6+
if (isMainThread) {
7+
const w = new Worker(__filename);
8+
w.on('message', common.mustCall((message) => {
9+
assert.strictEqual(message, 'Hello, world!');
10+
}));
11+
} else {
12+
setImmediate(() => {
13+
process.nextTick(() => {
14+
postMessage('Hello, world!');
15+
});
16+
});
17+
}

0 commit comments

Comments
 (0)