From 48e2b60c44f536928ea9207d11a81ad81461ca78 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 13 Nov 2021 13:12:15 +0530 Subject: [PATCH] test: add AsyncLocalStorage tests using udp, tcp and tls sockets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/40693 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/40741 Reviewed-By: Stephen Belanger Reviewed-By: Gerhard Stöbich Reviewed-By: Benjamin Gruenbaum --- .../test-async-local-storage-dgram.js | 26 ++++++++++++++ .../test-async-local-storage-socket.js | 27 ++++++++++++++ .../test-async-local-storage-tlssocket.js | 36 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 test/async-hooks/test-async-local-storage-dgram.js create mode 100644 test/async-hooks/test-async-local-storage-socket.js create mode 100644 test/async-hooks/test-async-local-storage-tlssocket.js diff --git a/test/async-hooks/test-async-local-storage-dgram.js b/test/async-hooks/test-async-local-storage-dgram.js new file mode 100644 index 00000000000000..a68ae636435ebe --- /dev/null +++ b/test/async-hooks/test-async-local-storage-dgram.js @@ -0,0 +1,26 @@ +'use strict'; + +require('../common'); + +// Regression tests for https://github.com/nodejs/node/issues/40693 + +const assert = require('assert'); +const dgram = require('dgram'); +const { AsyncLocalStorage } = require('async_hooks'); + +dgram.createSocket('udp4') + .on('message', function(msg, rinfo) { this.send(msg, rinfo.port); }) + .on('listening', function() { + const asyncLocalStorage = new AsyncLocalStorage(); + const store = { val: 'abcd' }; + asyncLocalStorage.run(store, () => { + const client = dgram.createSocket('udp4'); + client.on('message', (msg, rinfo) => { + assert.deepStrictEqual(asyncLocalStorage.getStore(), store); + client.close(); + this.close(); + }); + client.send('Hello, world!', this.address().port); + }); + }) + .bind(0); diff --git a/test/async-hooks/test-async-local-storage-socket.js b/test/async-hooks/test-async-local-storage-socket.js new file mode 100644 index 00000000000000..337b4073df5dfc --- /dev/null +++ b/test/async-hooks/test-async-local-storage-socket.js @@ -0,0 +1,27 @@ +'use strict'; + +require('../common'); + +// Regression tests for https://github.com/nodejs/node/issues/40693 + +const assert = require('assert'); +const net = require('net'); +const { AsyncLocalStorage } = require('async_hooks'); + +net + .createServer((socket) => { + socket.write('Hello, world!'); + socket.pipe(socket); + }) + .listen(0, function() { + const asyncLocalStorage = new AsyncLocalStorage(); + const store = { val: 'abcd' }; + asyncLocalStorage.run(store, () => { + const client = net.connect({ port: this.address().port }); + client.on('data', () => { + assert.deepStrictEqual(asyncLocalStorage.getStore(), store); + client.end(); + this.close(); + }); + }); + }); diff --git a/test/async-hooks/test-async-local-storage-tlssocket.js b/test/async-hooks/test-async-local-storage-tlssocket.js new file mode 100644 index 00000000000000..34ea4c068253cb --- /dev/null +++ b/test/async-hooks/test-async-local-storage-tlssocket.js @@ -0,0 +1,36 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// Regression tests for https://github.com/nodejs/node/issues/40693 + +const assert = require('assert'); +const fixtures = require('../common/fixtures'); +const tls = require('tls'); +const { AsyncLocalStorage } = require('async_hooks'); + +const options = { + cert: fixtures.readKey('rsa_cert.crt'), + key: fixtures.readKey('rsa_private.pem'), + rejectUnauthorized: false +}; + +tls + .createServer(options, (socket) => { + socket.write('Hello, world!'); + socket.pipe(socket); + }) + .listen(0, function() { + const asyncLocalStorage = new AsyncLocalStorage(); + const store = { val: 'abcd' }; + asyncLocalStorage.run(store, () => { + const client = tls.connect({ port: this.address().port, ...options }); + client.on('data', () => { + assert.deepStrictEqual(asyncLocalStorage.getStore(), store); + client.end(); + this.close(); + }); + }); + });