-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathasync_local_storage.ts
38 lines (34 loc) · 1.18 KB
/
async_local_storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {
asyncLocalStorageMiddleware,
getCurrentContext,
ExecutionContext,
} from '../src/async_local_storage';
import {Request, Response} from '../src/functions';
import {NextFunction} from 'express';
import * as assert from 'assert';
import * as semver from 'semver';
const runOrSkip = semver.lt(process.versions.node, '13.0.0') ? it.skip : it;
describe('asyncLocalStorageMiddleware', () => {
runOrSkip('async local storage', async () => {
const req = {
body: 'test body',
executionId: 'testExecutionId',
spanId: 'testSpanId',
};
let executionContext;
const next = () => {
// The store is accessible to operations created within the callback of run().
executionContext = getCurrentContext() as ExecutionContext;
assert(executionContext);
assert.strictEqual(executionContext.executionId, req.executionId);
assert.strictEqual(executionContext.spanId, req.spanId);
};
await asyncLocalStorageMiddleware(
req as Request,
{} as Response,
next as NextFunction,
);
// The store is not accessible outside of the run()'s callback function.
assert.strictEqual(getCurrentContext(), undefined);
});
});