diff --git a/test/unit/context-manager/async-local-context-manager.test.js b/test/unit/context-manager/async-local-context-manager.test.js index 717d0c2819..a935bc30fb 100644 --- a/test/unit/context-manager/async-local-context-manager.test.js +++ b/test/unit/context-manager/async-local-context-manager.test.js @@ -5,17 +5,153 @@ 'use strict' -const { test } = require('tap') - -const runContextManagerTests = require('./context-manager-tests') +const test = require('node:test') +const assert = require('node:assert') const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager') -test('Async Local Context Manager', (t) => { - t.autoend() +test('Should default to null context', () => { + const contextManager = new AsyncLocalContextManager({}) + + const context = contextManager.getContext() - runContextManagerTests(t, createContextManager) + assert.equal(context, null) }) -function createContextManager() { - return new AsyncLocalContextManager({}) -} +test('setContext should update the current context', () => { + const contextManager = new AsyncLocalContextManager({}) + + const expectedContext = { name: 'new context' } + + contextManager.setContext(expectedContext) + const context = contextManager.getContext() + + assert.equal(context, expectedContext) +}) + +test('runInContext()', async (t) => { + await t.test('should execute callback synchronously', () => { + const contextManager = new AsyncLocalContextManager({}) + + let callbackCalled = false + contextManager.runInContext({}, () => { + callbackCalled = true + }) + + assert.equal(callbackCalled, true) + }) + + await t.test('should set context to active for life of callback', (t, end) => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + + contextManager.runInContext(newContext, () => { + const context = contextManager.getContext() + + assert.equal(context, newContext) + end() + }) + }) + + await t.test('should restore previous context when callback completes', () => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + contextManager.runInContext(newContext, () => {}) + + const context = contextManager.getContext() + + assert.equal(context, previousContext) + }) + + await t.test('should restore previous context on exception', () => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + + try { + contextManager.runInContext(newContext, () => { + throw new Error('Something went bad') + }) + } catch (error) { + assert.ok(error) + // swallowing error + } + + const context = contextManager.getContext() + + assert.equal(context, previousContext) + }) + + await t.test('should apply `cbThis` arg to execution', (t, end) => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + const expectedThis = () => {} + + contextManager.runInContext(newContext, functionRunInContext, expectedThis) + + function functionRunInContext() { + assert.equal(this, expectedThis) + end() + } + }) + + await t.test('should apply args array to execution', (t, end) => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + const expectedArg1 = 'first arg' + const expectedArg2 = 'second arg' + const args = [expectedArg1, expectedArg2] + + contextManager.runInContext(newContext, functionRunInContext, null, args) + + function functionRunInContext(arg1, arg2) { + assert.equal(arg1, expectedArg1) + assert.equal(arg2, expectedArg2) + end() + } + }) + + await t.test('should apply arguments construct to execution', (t, end) => { + const contextManager = new AsyncLocalContextManager({}) + + const previousContext = { name: 'previous' } + contextManager.setContext(previousContext) + + const newContext = { name: 'new' } + const expectedArg1 = 'first arg' + const expectedArg2 = 'second arg' + + executingFunction(expectedArg1, expectedArg2) + + function executingFunction() { + contextManager.runInContext( + newContext, + function functionRunInContext(arg1, arg2) { + assert.equal(arg1, expectedArg1) + assert.equal(arg2, expectedArg2) + end() + }, + null, + arguments + ) + } + }) +}) diff --git a/test/unit/context-manager/context-manager-tests.js b/test/unit/context-manager/context-manager-tests.js deleted file mode 100644 index 58098277a6..0000000000 --- a/test/unit/context-manager/context-manager-tests.js +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -/** - * Add a standard set of Legacy Context Manager test cases for testing - * either the standard or diagnostic versions. - */ -function runContextMangerTests(t, createContextManager) { - t.test('Should default to null context', (t) => { - const contextManager = createContextManager() - - const context = contextManager.getContext() - - t.equal(context, null) - - t.end() - }) - - t.test('setContext should update the current context', (t) => { - const contextManager = createContextManager() - - const expectedContext = { name: 'new context' } - - contextManager.setContext(expectedContext) - const context = contextManager.getContext() - - t.equal(context, expectedContext) - - t.end() - }) - - t.test('runInContext()', (t) => { - t.autoend() - - t.test('should execute callback synchronously', (t) => { - const contextManager = createContextManager() - - let callbackCalled = false - contextManager.runInContext({}, () => { - callbackCalled = true - }) - - t.equal(callbackCalled, true) - - t.end() - }) - - t.test('should set context to active for life of callback', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - - contextManager.runInContext(newContext, () => { - const context = contextManager.getContext() - - t.equal(context, newContext) - t.end() - }) - }) - - t.test('should restore previous context when callback completes', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - contextManager.runInContext(newContext, () => {}) - - const context = contextManager.getContext() - - t.equal(context, previousContext) - - t.end() - }) - - t.test('should restore previous context on exception', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - - try { - contextManager.runInContext(newContext, () => { - throw new Error('Something went bad') - }) - } catch (error) { - t.ok(error) - // swallowing error - } - - const context = contextManager.getContext() - - t.equal(context, previousContext) - - t.end() - }) - - t.test('should apply `cbThis` arg to execution', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - const expectedThis = () => {} - - contextManager.runInContext(newContext, functionRunInContext, expectedThis) - - function functionRunInContext() { - t.equal(this, expectedThis) - t.end() - } - }) - - t.test('should apply args array to execution', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - const expectedArg1 = 'first arg' - const expectedArg2 = 'second arg' - const args = [expectedArg1, expectedArg2] - - contextManager.runInContext(newContext, functionRunInContext, null, args) - - function functionRunInContext(arg1, arg2) { - t.equal(arg1, expectedArg1) - t.equal(arg2, expectedArg2) - t.end() - } - }) - - t.test('should apply arguments construct to execution', (t) => { - const contextManager = createContextManager() - - const previousContext = { name: 'previous' } - contextManager.setContext(previousContext) - - const newContext = { name: 'new' } - const expectedArg1 = 'first arg' - const expectedArg2 = 'second arg' - - executingFunction(expectedArg1, expectedArg2) - - function executingFunction() { - contextManager.runInContext( - newContext, - function functionRunInContext(arg1, arg2) { - t.equal(arg1, expectedArg1) - t.equal(arg2, expectedArg2) - t.end() - }, - null, - arguments - ) - } - }) - }) -} - -module.exports = runContextMangerTests diff --git a/test/unit/context-manager/create-context-manager.test.js b/test/unit/context-manager/create-context-manager.test.js index 88998e1a04..542ff11127 100644 --- a/test/unit/context-manager/create-context-manager.test.js +++ b/test/unit/context-manager/create-context-manager.test.js @@ -5,17 +5,17 @@ 'use strict' -const { test } = require('tap') +const test = require('node:test') +const assert = require('node:assert') const createImplementation = require('../../../lib/context-manager/create-context-manager') const AsyncLocalContextManager = require('../../../lib/context-manager/async-local-context-manager') -test('Should return AsyncLocalContextManager by default', (t) => { +test('Should return AsyncLocalContextManager by default', () => { const contextManager = createImplementation({ logging: {}, feature_flag: {} }) - t.ok(contextManager instanceof AsyncLocalContextManager) - t.end() + assert.equal(contextManager instanceof AsyncLocalContextManager, true) })