forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-net-transfer-guards.js
More file actions
47 lines (40 loc) · 1.52 KB
/
Copy pathtest-net-transfer-guards.js
File metadata and controls
47 lines (40 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
// This test verifies the guards that reject transferring a net.Socket or
// net.Server that is not in a clean, movable state. Serialization is triggered
// with a MessageChannel, so no worker thread is required.
const common = require('../common');
const assert = require('assert');
const net = require('net');
const { MessageChannel } = require('worker_threads');
const { port1 } = new MessageChannel();
// A Server that is not listening (no handle) cannot be transferred.
{
const server = net.createServer();
assert.throws(() => port1.postMessage({ server }, [server]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
}
// A Socket that has already consumed data cannot be transferred, because that
// buffered data would be lost on the sending side.
{
const server = net.createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
assert.throws(() => port1.postMessage({ socket }, [socket]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
// The socket is still usable after a rejected transfer.
assert.ok(socket._handle != null);
socket.destroy();
server.close();
port1.close();
}));
}));
server.listen(0, common.mustCall(() => {
const client = net.connect(server.address().port, common.mustCall(() => {
client.end('data');
}));
// The server destroys the socket after the rejected transfer, which may
// surface as a reset on the client; swallow it either way.
client.on('error', () => {});
}));
}