Skip to content

Commit

Permalink
chore(mysql2): adding TAV script (#641)
Browse files Browse the repository at this point in the history
* chore(mysql2): adding TAV script

* chore: add testUtils getPackageVersion helper

* chore: skip poolCluster.end callback for unsupported versions

* chore: use exact version for test-all-versions

* chore: fix lint error

* test: don't pass callback when ignored
  • Loading branch information
Yaniv Davidi committed Aug 30, 2021
1 parent 1771b60 commit ee0debf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/opentelemetry-test-utils/test-utils.ts
Expand Up @@ -28,6 +28,7 @@ import {
hrTimeToMilliseconds,
hrTimeToMicroseconds,
} from '@opentelemetry/core';
import * as path from 'path';

const dockerRunCmds = {
cassandra:
Expand Down Expand Up @@ -135,3 +136,9 @@ export interface TimedEvent {
/** The attributes of the event. */
attributes?: SpanAttributes;
}

export const getPackageVersion = (packageName: string) => {
const packagePath = require.resolve(packageName);
const packageJsonPath = path.join(path.dirname(packagePath), 'package.json');
return require(packageJsonPath).version;
};
6 changes: 6 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mysql2/.tav.yml
@@ -0,0 +1,6 @@
mysql2:
versions: 2.*
commands: npm run test

# Fix missing `test-utils` package
pretest: npm run --prefix ../../../ lerna:link
Expand Up @@ -15,6 +15,7 @@
"prepare": "npm run compile",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test-all-versions": "tav",
"version:update": "node ../../../scripts/version-update.js"
},
"keywords": [
Expand Down Expand Up @@ -58,6 +59,7 @@
"mysql2": "2.3.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"test-all-versions": "5.0.1",
"ts-mocha": "8.0.0",
"typescript": "4.3.5"
},
Expand Down
Expand Up @@ -27,6 +27,7 @@ import {
import * as assert from 'assert';
import { MySQL2Instrumentation } from '../src';

const LIB_VERSION = testUtils.getPackageVersion('mysql2');
const port = Number(process.env.MYSQL_PORT) || 33306;
const database = process.env.MYSQL_DATABASE || 'test_db';
const host = process.env.MYSQL_HOST || '127.0.0.1';
Expand Down Expand Up @@ -117,11 +118,16 @@ describe('mysql@2.x', () => {
instrumentation.disable();
connection.end(() => {
pool.end(() => {
// PoolCluster.end types in the package are invalid
// https://github.com/sidorares/node-mysql2/pull/1332
(poolCluster as any).end(() => {
if (isPoolClusterEndIgnoreCallback()) {
poolCluster.end();
done();
});
} else {
// PoolCluster.end types in the package are invalid
// https://github.com/sidorares/node-mysql2/pull/1332
(poolCluster as any).end(() => {
done();
});
}
});
});
});
Expand Down Expand Up @@ -659,3 +665,9 @@ function assertSpan(
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
}
}

function isPoolClusterEndIgnoreCallback() {
// Since v2.2.0 `end` function respect callback
// https://github.com/sidorares/node-mysql2/commit/1481015626e506754adc4308e5508356a3a03aa0
return ['2.0.0', '2.0.1', '2.0.2', '2.1.0'].includes(LIB_VERSION);
}

0 comments on commit ee0debf

Please sign in to comment.