|
5 | 5 |
|
6 | 6 | 'use strict'
|
7 | 7 |
|
8 |
| -const { test } = require('tap') |
9 |
| - |
10 |
| -const runContextManagerTests = require('./context-manager-tests') |
| 8 | +const test = require('node:test') |
| 9 | +const assert = require('node:assert') |
11 | 10 | const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager')
|
12 | 11 |
|
13 |
| -test('Async Local Context Manager', (t) => { |
14 |
| - t.autoend() |
| 12 | +test('Should default to null context', () => { |
| 13 | + const contextManager = new AsyncLocalContextManager({}) |
| 14 | + |
| 15 | + const context = contextManager.getContext() |
15 | 16 |
|
16 |
| - runContextManagerTests(t, createContextManager) |
| 17 | + assert.equal(context, null) |
17 | 18 | })
|
18 | 19 |
|
19 |
| -function createContextManager() { |
20 |
| - return new AsyncLocalContextManager({}) |
21 |
| -} |
| 20 | +test('setContext should update the current context', () => { |
| 21 | + const contextManager = new AsyncLocalContextManager({}) |
| 22 | + |
| 23 | + const expectedContext = { name: 'new context' } |
| 24 | + |
| 25 | + contextManager.setContext(expectedContext) |
| 26 | + const context = contextManager.getContext() |
| 27 | + |
| 28 | + assert.equal(context, expectedContext) |
| 29 | +}) |
| 30 | + |
| 31 | +test('runInContext()', async (t) => { |
| 32 | + await t.test('should execute callback synchronously', () => { |
| 33 | + const contextManager = new AsyncLocalContextManager({}) |
| 34 | + |
| 35 | + let callbackCalled = false |
| 36 | + contextManager.runInContext({}, () => { |
| 37 | + callbackCalled = true |
| 38 | + }) |
| 39 | + |
| 40 | + assert.equal(callbackCalled, true) |
| 41 | + }) |
| 42 | + |
| 43 | + await t.test('should set context to active for life of callback', (t, end) => { |
| 44 | + const contextManager = new AsyncLocalContextManager({}) |
| 45 | + |
| 46 | + const previousContext = { name: 'previous' } |
| 47 | + contextManager.setContext(previousContext) |
| 48 | + |
| 49 | + const newContext = { name: 'new' } |
| 50 | + |
| 51 | + contextManager.runInContext(newContext, () => { |
| 52 | + const context = contextManager.getContext() |
| 53 | + |
| 54 | + assert.equal(context, newContext) |
| 55 | + end() |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + await t.test('should restore previous context when callback completes', () => { |
| 60 | + const contextManager = new AsyncLocalContextManager({}) |
| 61 | + |
| 62 | + const previousContext = { name: 'previous' } |
| 63 | + contextManager.setContext(previousContext) |
| 64 | + |
| 65 | + const newContext = { name: 'new' } |
| 66 | + contextManager.runInContext(newContext, () => {}) |
| 67 | + |
| 68 | + const context = contextManager.getContext() |
| 69 | + |
| 70 | + assert.equal(context, previousContext) |
| 71 | + }) |
| 72 | + |
| 73 | + await t.test('should restore previous context on exception', () => { |
| 74 | + const contextManager = new AsyncLocalContextManager({}) |
| 75 | + |
| 76 | + const previousContext = { name: 'previous' } |
| 77 | + contextManager.setContext(previousContext) |
| 78 | + |
| 79 | + const newContext = { name: 'new' } |
| 80 | + |
| 81 | + try { |
| 82 | + contextManager.runInContext(newContext, () => { |
| 83 | + throw new Error('Something went bad') |
| 84 | + }) |
| 85 | + } catch (error) { |
| 86 | + assert.ok(error) |
| 87 | + // swallowing error |
| 88 | + } |
| 89 | + |
| 90 | + const context = contextManager.getContext() |
| 91 | + |
| 92 | + assert.equal(context, previousContext) |
| 93 | + }) |
| 94 | + |
| 95 | + await t.test('should apply `cbThis` arg to execution', (t, end) => { |
| 96 | + const contextManager = new AsyncLocalContextManager({}) |
| 97 | + |
| 98 | + const previousContext = { name: 'previous' } |
| 99 | + contextManager.setContext(previousContext) |
| 100 | + |
| 101 | + const newContext = { name: 'new' } |
| 102 | + const expectedThis = () => {} |
| 103 | + |
| 104 | + contextManager.runInContext(newContext, functionRunInContext, expectedThis) |
| 105 | + |
| 106 | + function functionRunInContext() { |
| 107 | + assert.equal(this, expectedThis) |
| 108 | + end() |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + await t.test('should apply args array to execution', (t, end) => { |
| 113 | + const contextManager = new AsyncLocalContextManager({}) |
| 114 | + |
| 115 | + const previousContext = { name: 'previous' } |
| 116 | + contextManager.setContext(previousContext) |
| 117 | + |
| 118 | + const newContext = { name: 'new' } |
| 119 | + const expectedArg1 = 'first arg' |
| 120 | + const expectedArg2 = 'second arg' |
| 121 | + const args = [expectedArg1, expectedArg2] |
| 122 | + |
| 123 | + contextManager.runInContext(newContext, functionRunInContext, null, args) |
| 124 | + |
| 125 | + function functionRunInContext(arg1, arg2) { |
| 126 | + assert.equal(arg1, expectedArg1) |
| 127 | + assert.equal(arg2, expectedArg2) |
| 128 | + end() |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + await t.test('should apply arguments construct to execution', (t, end) => { |
| 133 | + const contextManager = new AsyncLocalContextManager({}) |
| 134 | + |
| 135 | + const previousContext = { name: 'previous' } |
| 136 | + contextManager.setContext(previousContext) |
| 137 | + |
| 138 | + const newContext = { name: 'new' } |
| 139 | + const expectedArg1 = 'first arg' |
| 140 | + const expectedArg2 = 'second arg' |
| 141 | + |
| 142 | + executingFunction(expectedArg1, expectedArg2) |
| 143 | + |
| 144 | + function executingFunction() { |
| 145 | + contextManager.runInContext( |
| 146 | + newContext, |
| 147 | + function functionRunInContext(arg1, arg2) { |
| 148 | + assert.equal(arg1, expectedArg1) |
| 149 | + assert.equal(arg2, expectedArg2) |
| 150 | + end() |
| 151 | + }, |
| 152 | + null, |
| 153 | + arguments |
| 154 | + ) |
| 155 | + } |
| 156 | + }) |
| 157 | +}) |
0 commit comments