-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
test: add new scenario for async-local storage #32082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
HarshithaKP
wants to merge
3
commits into
nodejs:master
from
HarshithaKP:async_local_storage_http_test
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
test/parallel/test-async-local-storage-http-multiclients.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
|
|
||
| const assert = require('assert'); | ||
| const { AsyncLocalStorage } = require('async_hooks'); | ||
| const http = require('http'); | ||
| const cls = new AsyncLocalStorage(); | ||
| const NUM_CLIENTS = 10; | ||
|
|
||
| // Run multiple clients that receive data from a server | ||
| // in multiple chunks, in a single non-closure function. | ||
| // Use the AsyncLocalStorage (ALS) APIs to maintain the context | ||
| // and data download. Make sure that individual clients | ||
| // receive their respective data, with no conflicts. | ||
|
|
||
| // Set up a server, that sends large buffers of data, filled | ||
| // with cardinal numbers, increasing per request | ||
| let index = 0; | ||
| const server = http.createServer((q, r) => { | ||
| // Send a large chunk as response, otherwise the data | ||
| // may be coalesced, and the callback in the client | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| // may be called only once, defeating the purpose of test | ||
| r.end((index++).toString().repeat(1024 * 1024)); | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| for (let i = 0; i < NUM_CLIENTS; i++) { | ||
| cls.run(new Map(), common.mustCall(() => { | ||
| const options = { port: server.address().port }; | ||
| const req = http.get(options, common.mustCall((res) => { | ||
| const store = cls.getStore(); | ||
| store.set('data', ''); | ||
|
|
||
| // Make ondata and onend non-closure | ||
| // functions and fully dependent on ALS | ||
| res.on('data', ondata); | ||
|
addaleax marked this conversation as resolved.
|
||
| res.on('end', onend); | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| })); | ||
| req.end(); | ||
| })); | ||
| } | ||
| })); | ||
|
|
||
| // Accumulate the instantaneous data with the store data | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| function ondata(d) { | ||
| const store = cls.getStore(); | ||
| assert.notStrictEqual(store, undefined); | ||
| let chunk = store.get('data'); | ||
| chunk += d; | ||
| store.set('data', chunk); | ||
| } | ||
|
|
||
| // Retrieve the store data, and test for homogeneity | ||
| let endCount = 0; | ||
| function onend() { | ||
| const store = cls.getStore(); | ||
| assert.notStrictEqual(store, undefined); | ||
| const chunk = store.get('data'); | ||
| const re = new RegExp(chunk[0], 'g'); | ||
| assert.strictEqual(chunk.replace(re, '').length, 0); | ||
|
addaleax marked this conversation as resolved.
Outdated
|
||
| if (++endCount === NUM_CLIENTS) { | ||
|
jasnell marked this conversation as resolved.
Outdated
|
||
| server.close(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.