Skip to content

Commit

Permalink
feat(test-utils): runTestFixture utility for running out-of-process t…
Browse files Browse the repository at this point in the history
…ests

Running scripts out-of-process for testing allows running tests with
different node options and with isolation. The former is useful for
ESM testing. This change includes adding an ESM test for ioredis.
This depends on open-telemetry#1694 to pass.

Closes: open-telemetry#1731
  • Loading branch information
trentm committed Oct 13, 2023
1 parent a8c225d commit 86dbd54
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ This package:

By using this package, testing instrumentation code can be shorter, and good practices for writing tests are more easily applied.

### Supporter Version
### Supported Version

Since [root hook plugin](https://mochajs.org/#root-hook-plugins) are used, this package is compatible to mocha v8.0.0 and above.
Since [root hook plugin](https://mochajs.org/#root-hook-plugins) are used, this package is compatible to mocha v7.2.0 and above.

### Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/context-async-hooks": "^1.8.0",
"@opentelemetry/contrib-test-utils": "^0.34.2",
"@opentelemetry/sdk-node": "^0.44.0",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/mocha": "7.0.2",
"@types/sinon": "10.0.18",
"@types/node": "18.6.5",
"@types/sinon": "10.0.18",
"cross-env": "7.0.3",
"ioredis": "5.2.2",
"mocha": "7.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Use ioredis from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-ioredis.mjs [REDIS_URL]

// ---
// TODO: Move this block to @opentelemetry/contrib-test-utils and import as:
// import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
import { NodeSDK, tracing, api } from '@opentelemetry/sdk-node';
function createTestNodeSdk(opts) {
// Typically, when run via `runTestFixture`, OTEL_EXPORTER_OTLP_ENDPOINT will
// be set to export to a test collector. Fallback to writing to the console
// for dev usage.
const spanProcessor = (process.env.OTEL_EXPORTER_OTLP_ENDPOINT
? undefined
: new tracing.SimpleSpanProcessor(new tracing.ConsoleSpanExporter()));
const sdk = new NodeSDK({
serviceName: opts.serviceName || 'test-service',
spanProcessor,
instrumentations: opts.instrumentations
});
return sdk;
}
// ---

import { IORedisInstrumentation } from '../../build/src/index.js';
const sdk = createTestNodeSdk({
serviceName: 'use-ioredis',
instrumentations: [
new IORedisInstrumentation()
]
})
sdk.start();

import assert from 'assert';
import Redis from 'ioredis';

const REDIS_URL = process.argv[2] || '';
const redis = new Redis(REDIS_URL);

// Randomize the key to avoid collisions with parallel testing.
const randomId = ((Math.random() * 2 ** 32) >>> 0).toString(16);
const testKeyName = `test-${randomId}`;

const tracer = api.trace.getTracer();
await tracer.startActiveSpan('manual', async (span) => {
redis.set(testKeyName, 'bar');
let val = await redis.get(testKeyName);
assert(val === 'bar');
span.end();
});

await redis.quit();
await sdk.shutdown();
Loading

0 comments on commit 86dbd54

Please sign in to comment.