-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(node): Update mysql tests for better coverage and correctness #21684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mydea
wants to merge
7
commits into
develop
Choose a base branch
from
fn/fix-mysql-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dfcde97
test(node): Update mysql tests for better coverage and correctness
mydea e8fb252
reduce wait time
mydea de4b1d9
actually add the stream context test
mydea 5cef81f
fixes and streamlining
mydea ce514a8
better comments
mydea 6fa25d1
fix timeouts
mydea 14bb273
fix mysql server flakiness
mydea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
dev-packages/node-integration-tests/suites/tracing/mysql/mysql-test-server.ts
This file contains hidden or 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,105 @@ | ||
| /* eslint-disable no-bitwise */ | ||
|
|
||
| // A tiny, dependency-free MySQL server that speaks just enough of the v10 wire | ||
| // protocol for the `mysql` client to connect and run queries. It completes the | ||
| // handshake (so `pool.getConnection()` resolves and reaches `connection.query`) | ||
| // and replies to every command with a success OK packet (the client treats it | ||
| // as a 0-row result, even for `SELECT`). This gives pool queries a real, | ||
| // successful connection to instrument — no docker / real database required. | ||
| import net from 'node:net'; | ||
|
|
||
| // MySQL capability flags (only the ones the client checks here). | ||
| const CLIENT_PROTOCOL_41 = 0x00000200; | ||
| const CLIENT_SECURE_CONNECTION = 0x00008000; | ||
| const CLIENT_PLUGIN_AUTH = 0x00080000; | ||
| const SERVER_CAPABILITIES = CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH; | ||
|
|
||
| /** Wrap a payload in a MySQL packet: 3-byte little-endian length + 1-byte sequence id. */ | ||
| function packet(seq: number, payload: Buffer): Buffer { | ||
| const header = Buffer.alloc(4); | ||
| header.writeUIntLE(payload.length, 0, 3); | ||
| header.writeUInt8(seq, 3); | ||
| return Buffer.concat([header, payload]); | ||
| } | ||
|
|
||
| function initialHandshake() { | ||
| const scramble = Buffer.alloc(20, 1); // 20-byte auth-plugin-data (value is irrelevant — we never verify) | ||
| const parts = [ | ||
| Buffer.from([0x0a]), // protocol version 10 | ||
| Buffer.from('8.0.0-sentry-test\0', 'latin1'), // server version (NUL-terminated) | ||
| Buffer.from([1, 0, 0, 0]), // connection id | ||
| scramble.subarray(0, 8), // auth-plugin-data-part-1 | ||
| Buffer.from([0x00]), // filler | ||
| Buffer.from([SERVER_CAPABILITIES & 0xff, (SERVER_CAPABILITIES >> 8) & 0xff]), // capability flags (lower) | ||
| Buffer.from([0x21]), // charset (utf8_general_ci) | ||
| Buffer.from([0x02, 0x00]), // status flags | ||
| Buffer.from([(SERVER_CAPABILITIES >> 16) & 0xff, (SERVER_CAPABILITIES >> 24) & 0xff]), // capability flags (upper) | ||
| Buffer.from([21]), // length of auth-plugin-data | ||
| Buffer.alloc(10, 0), // reserved | ||
| Buffer.concat([scramble.subarray(8), Buffer.from([0x00])]), // auth-plugin-data-part-2 (+ NUL) | ||
| Buffer.from('mysql_native_password\0', 'latin1'), | ||
| ]; | ||
| return Buffer.concat(parts); | ||
| } | ||
|
|
||
| function okPacket(): Buffer { | ||
| // OK header, 0 affected rows, 0 insert id, status flags, 0 warnings. The client accepts this for any | ||
| // command — including a `SELECT` (treated as a successful 0-row result) — so spans get `status: ok`. | ||
| return Buffer.from([0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]); | ||
| } | ||
|
|
||
| function errPacket(): Buffer { | ||
| // ERR for queries that should fail: code 1146 (ER_NO_SUCH_TABLE), SQL state "42S02". The client | ||
| // surfaces this as a query error, so the span gets `status: internal_error`. | ||
| const head = Buffer.from([0xff, 0x7a, 0x04]); // 0xff + error code 1146 (LE) | ||
| const state = Buffer.from('#42S02', 'latin1'); | ||
| const msg = Buffer.from("Table 'does_not_exist' doesn't exist", 'latin1'); | ||
| return Buffer.concat([head, state, msg]); | ||
| } | ||
|
|
||
| /** Start the server on the given host/port. Returns the `net.Server` (call `.close()` to stop). */ | ||
| export function startMysqlTestServer({ host = '127.0.0.1', port = 0 } = {}) { | ||
| const server = net.createServer(socket => { | ||
| socket.on('error', () => {}); // ignore abrupt client disconnects | ||
| socket.write(packet(0, initialHandshake())); | ||
|
|
||
| let sawHandshakeResponse = false; | ||
| let buffered = Buffer.alloc(0); | ||
| socket.on('data', (chunk: Buffer) => { | ||
| // TCP may coalesce several packets into one `data` event or split one packet across events, so | ||
| // we can't assume one packet per read. Accumulate bytes and frame on the 3-byte length header, | ||
| // consuming only whole packets — otherwise a coalesced handshake-response + COM_QUERY would lose | ||
| // its tail and the client would hang. | ||
| buffered = buffered.length ? Buffer.concat([buffered, chunk]) : chunk; | ||
|
|
||
| while (buffered.length >= 4) { | ||
| // Packet: [3-byte LE payload length][1-byte seq][payload]. | ||
| const payloadLength = buffered.readUIntLE(0, 3); | ||
| const packetLength = 4 + payloadLength; | ||
| if (buffered.length < packetLength) { | ||
| break; // rest of this packet hasn't arrived yet | ||
| } | ||
| const pkt = buffered.subarray(0, packetLength); | ||
| buffered = buffered.subarray(packetLength); | ||
|
|
||
| if (!sawHandshakeResponse) { | ||
| // First inbound packet is the client's handshake response → accept auth. | ||
| sawHandshakeResponse = true; | ||
| socket.write(packet(2, okPacket())); | ||
| continue; | ||
| } | ||
|
|
||
| // Command packet: payload is [1-byte command][args]. For COM_QUERY (0x03) the args are the SQL | ||
| // text. Queries referencing the conventional missing table fail (so error-path tests work); | ||
| // every other command succeeds with an OK. The command resets the sequence, so our reply is seq 1. | ||
| const isQuery = payloadLength > 1 && pkt[4] === 0x03; | ||
| const sql = isQuery ? pkt.subarray(5).toString('latin1') : ''; | ||
| socket.write(packet(1, sql.includes('does_not_exist') ? errPacket() : okPacket())); | ||
| } | ||
| }); | ||
| }); | ||
| // Never let a listen error crash the test process. | ||
| server.on('error', () => {}); | ||
| server.listen(port, host); | ||
| return server; | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
dev-packages/node-integration-tests/suites/tracing/mysql/scenario-streamContext.mjs
This file contains hidden or 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,42 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mysql from 'mysql'; | ||
|
|
||
| const connection = mysql.createConnection({ | ||
| port: Number(process.env.MYSQL_PORT), | ||
| user: 'root', | ||
| password: 'docker', | ||
| }); | ||
|
|
||
| connection.connect(function (err) { | ||
| if (err) { | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| Sentry.startSpanManual( | ||
| { | ||
| op: 'transaction', | ||
| name: 'Test Transaction', | ||
| }, | ||
| span => { | ||
| const query = connection.query('SELECT 1 + 1 AS solution'); | ||
|
|
||
| // This should _not_ be the parent of the listener-child! | ||
| Sentry.startSpanManual({ name: 'inner-span' }, innerSpan => { | ||
| // The instrumentation registers its own `end` listener (which finishes the query span) when | ||
| // `query()` is called, before this one — so by the time we run here, the query span is finished. | ||
| query.on('end', () => { | ||
| // A span started from inside a stream listener should be a child of the parent context that was | ||
| // active when the query was issued (the transaction here), not of the query span itself. This | ||
| // verifies the instrumentation re-binds the streamed query's events to the parent context. | ||
| Sentry.startSpan({ name: 'listener-child' }, () => { | ||
| // noop | ||
| }); | ||
|
|
||
| innerSpan.end(); | ||
| span.end(); | ||
| connection.end(); | ||
| }); | ||
| }); | ||
| }, | ||
| ); |
36 changes: 36 additions & 0 deletions
36
dev-packages/node-integration-tests/suites/tracing/mysql/scenario-streamError.mjs
This file contains hidden or 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,36 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mysql from 'mysql'; | ||
|
|
||
| const connection = mysql.createConnection({ | ||
| port: Number(process.env.MYSQL_PORT), | ||
| user: 'root', | ||
| password: 'docker', | ||
| }); | ||
|
|
||
| connection.connect(function (err) { | ||
| if (err) { | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| Sentry.startSpanManual( | ||
| { | ||
| op: 'transaction', | ||
| name: 'Test Transaction', | ||
| }, | ||
| span => { | ||
| // Query without a callback returns a streamable `Query`. A failing query emits an `error` event | ||
| // (which sets the span status) followed by `end` (which ends the span). | ||
| const query = connection.query('SELECT * FROM does_not_exist'); | ||
|
|
||
| // Swallow the error so it doesn't crash the process | ||
| query.on('error', () => { | ||
| // noop | ||
| }); | ||
|
|
||
| query.on('end', () => { | ||
| span.end(); | ||
| connection.end(); | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
dev-packages/node-integration-tests/suites/tracing/mysql/scenario-withPool.mjs
This file contains hidden or 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,23 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mysql from 'mysql'; | ||
|
|
||
| const pool = mysql.createPool({ | ||
| port: Number(process.env.MYSQL_PORT), | ||
| user: 'root', | ||
| password: 'docker', | ||
| }); | ||
|
|
||
| Sentry.startSpanManual( | ||
| { | ||
| op: 'transaction', | ||
| name: 'Test Transaction', | ||
| }, | ||
| span => { | ||
| pool.query('SELECT 1 + 1 AS solution', function () { | ||
| pool.query('SELECT NOW()', ['1', '2'], () => { | ||
| span.end(); | ||
| pool.end(); | ||
| }); | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.