1'use strict'; 2const common = require('../common'); 3const Countdown = require('../common/countdown'); 4const assert = require('assert'); 5const { AsyncLocalStorage } = require('async_hooks'); 6const http = require('http'); 7const cls = new AsyncLocalStorage(); 8const NUM_CLIENTS = 10; 9 10// Run multiple clients that receive data from a server 11// in multiple chunks, in a single non-closure function. 12// Use the AsyncLocalStorage (ALS) APIs to maintain the context 13// and data download. Make sure that individual clients 14// receive their respective data, with no conflicts. 15 16// Set up a server that sends large buffers of data, filled 17// with cardinal numbers, increasing per request 18let index = 0; 19const server = http.createServer((q, r) => { 20 // Send a large chunk as response, otherwise the data 21 // may be sent in a single chunk, and the callback in the 22 // client may be called only once, defeating the purpose of test 23 r.end((index++ % 10).toString().repeat(1024 * 1024)); 24}); 25 26const countdown = new Countdown(NUM_CLIENTS, () => { 27 server.close(); 28}); 29 30server.listen(0, common.mustCall(() => { 31 for (let i = 0; i < NUM_CLIENTS; i++) { 32 cls.run(new Map(), common.mustCall(() => { 33 const options = { port: server.address().port }; 34 const req = http.get(options, common.mustCall((res) => { 35 const store = cls.getStore(); 36 store.set('data', ''); 37 38 // Make ondata and onend non-closure 39 // functions and fully dependent on ALS 40 res.setEncoding('utf8'); 41 res.on('data', ondata); 42 res.on('end', common.mustCall(onend)); 43 })); 44 req.end(); 45 })); 46 } 47})); 48 49// Accumulate the current data chunk with the store data 50function ondata(d) { 51 const store = cls.getStore(); 52 assert.notStrictEqual(store, undefined); 53 let chunk = store.get('data'); 54 chunk += d; 55 store.set('data', chunk); 56} 57 58// Retrieve the store data, and test for homogeneity 59function onend() { 60 const store = cls.getStore(); 61 assert.notStrictEqual(store, undefined); 62 const data = store.get('data'); 63 assert.strictEqual(data, data[0].repeat(data.length)); 64 countdown.dec(); 65} 66