| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d2fea9f commit fd80c21
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,65 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const Countdown = require('../common/countdown'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { AsyncLocalStorage } = require('async_hooks'); | ||
| 6 | + const http = require('http'); | ||
| 7 | + const cls = new AsyncLocalStorage(); | ||
| 8 | + const 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 | ||
| 18 | + let index = 0; | ||
| 19 | + const 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 | + | ||
| 26 | + const countdown = new Countdown(NUM_CLIENTS, () => { | ||
| 27 | + server.close(); | ||
| 28 | + }); | ||
| 29 | + | ||
| 30 | + server.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 | ||
| 50 | + function 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 | ||
| 59 | + function 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments