forked from open-telemetry/opentelemetry-js-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test-utils): runTestFixture utility for running out-of-process t…
…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
Showing
4 changed files
with
321 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
plugins/node/opentelemetry-instrumentation-ioredis/test/fixtures/use-ioredis.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.