|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Simple Node.js v8 compatibility smoke test. Intentionally avoids dependencies |
| 8 | + * so that we can keep updating tooling used for unit and integration tests. |
| 9 | + * |
| 10 | + * Intended to be run directly with node node8-compat.js |
| 11 | + */ |
| 12 | + |
| 13 | +const assert = require('assert'); |
| 14 | + |
| 15 | +const GLOBAL_API_SYMBOL = Symbol.for('opentelemetry.js.api.1'); |
| 16 | + |
| 17 | +function clearGlobal() { |
| 18 | + delete global[GLOBAL_API_SYMBOL]; |
| 19 | +} |
| 20 | + |
| 21 | +function freshApi() { |
| 22 | + // Evict every cached module so each call returns a brand-new instance. |
| 23 | + Object.keys(require.cache).forEach(function (key) { |
| 24 | + delete require.cache[key]; |
| 25 | + }); |
| 26 | + return require('../../build/src/index.js'); |
| 27 | +} |
| 28 | + |
| 29 | +// Test 1: test global registration (the global used must exist) |
| 30 | +clearGlobal(); |
| 31 | +const api = freshApi(); |
| 32 | + |
| 33 | +// this will throw or return false if registration fails. |
| 34 | +const result = api.diag.setLogger( |
| 35 | + new api.DiagConsoleLogger(), |
| 36 | + api.DiagLogLevel.ALL |
| 37 | +); |
| 38 | +assert.strictEqual( |
| 39 | + result, |
| 40 | + true, |
| 41 | + 'Test 1 failed: diag.setLogger should return true' |
| 42 | +); |
| 43 | + |
| 44 | +// Test 2: test that multiple loads share the same global state (e.g. logger) |
| 45 | +clearGlobal(); |
| 46 | + |
| 47 | +const api1 = freshApi(); |
| 48 | +const api2 = freshApi(); // fresh load – different module object, same _global |
| 49 | + |
| 50 | +const infoMessages = []; |
| 51 | +const sharedLogger = { |
| 52 | + error: function () {}, |
| 53 | + warn: function () {}, |
| 54 | + info: function (msg) { |
| 55 | + infoMessages.push(msg); |
| 56 | + }, |
| 57 | + debug: function () {}, |
| 58 | + verbose: function () {}, |
| 59 | +}; |
| 60 | + |
| 61 | +const ok = api1.diag.setLogger(sharedLogger, api1.DiagLogLevel.ALL); |
| 62 | +assert.strictEqual( |
| 63 | + ok, |
| 64 | + true, |
| 65 | + 'Test 2 setup failed: api1.diag.setLogger should return true' |
| 66 | +); |
| 67 | + |
| 68 | +api2.diag.info('shared-state-check'); |
| 69 | +assert.strictEqual( |
| 70 | + infoMessages.length, |
| 71 | + 1, |
| 72 | + 'Test 2 failed: api2 should forward logs to the logger registered via api1' |
| 73 | +); |
| 74 | +assert.strictEqual( |
| 75 | + infoMessages[0], |
| 76 | + 'shared-state-check', |
| 77 | + 'Test 2 failed: unexpected log message received' |
| 78 | +); |
| 79 | + |
| 80 | +// clean-up |
| 81 | +clearGlobal(); |
0 commit comments