Skip to content

Commit 43e8650

Browse files
RaisinTentargos
authored andcommitted
test: add AsyncLocalStorage tests using udp, tcp and tls sockets
Fixes: #40693 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40741 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 16ee842 commit 43e8650

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
// Regression tests for https://github.com/nodejs/node/issues/40693
6+
7+
const assert = require('assert');
8+
const dgram = require('dgram');
9+
const { AsyncLocalStorage } = require('async_hooks');
10+
11+
dgram.createSocket('udp4')
12+
.on('message', function(msg, rinfo) { this.send(msg, rinfo.port); })
13+
.on('listening', function() {
14+
const asyncLocalStorage = new AsyncLocalStorage();
15+
const store = { val: 'abcd' };
16+
asyncLocalStorage.run(store, () => {
17+
const client = dgram.createSocket('udp4');
18+
client.on('message', (msg, rinfo) => {
19+
assert.deepStrictEqual(asyncLocalStorage.getStore(), store);
20+
client.close();
21+
this.close();
22+
});
23+
client.send('Hello, world!', this.address().port);
24+
});
25+
})
26+
.bind(0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
// Regression tests for https://github.com/nodejs/node/issues/40693
6+
7+
const assert = require('assert');
8+
const net = require('net');
9+
const { AsyncLocalStorage } = require('async_hooks');
10+
11+
net
12+
.createServer((socket) => {
13+
socket.write('Hello, world!');
14+
socket.pipe(socket);
15+
})
16+
.listen(0, function() {
17+
const asyncLocalStorage = new AsyncLocalStorage();
18+
const store = { val: 'abcd' };
19+
asyncLocalStorage.run(store, () => {
20+
const client = net.connect({ port: this.address().port });
21+
client.on('data', () => {
22+
assert.deepStrictEqual(asyncLocalStorage.getStore(), store);
23+
client.end();
24+
this.close();
25+
});
26+
});
27+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// Regression tests for https://github.com/nodejs/node/issues/40693
8+
9+
const assert = require('assert');
10+
const fixtures = require('../common/fixtures');
11+
const tls = require('tls');
12+
const { AsyncLocalStorage } = require('async_hooks');
13+
14+
const options = {
15+
cert: fixtures.readKey('rsa_cert.crt'),
16+
key: fixtures.readKey('rsa_private.pem'),
17+
rejectUnauthorized: false
18+
};
19+
20+
tls
21+
.createServer(options, (socket) => {
22+
socket.write('Hello, world!');
23+
socket.pipe(socket);
24+
})
25+
.listen(0, function() {
26+
const asyncLocalStorage = new AsyncLocalStorage();
27+
const store = { val: 'abcd' };
28+
asyncLocalStorage.run(store, () => {
29+
const client = tls.connect({ port: this.address().port, ...options });
30+
client.on('data', () => {
31+
assert.deepStrictEqual(asyncLocalStorage.getStore(), store);
32+
client.end();
33+
this.close();
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)