-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(node): Add tests for socket.io integration
Signed-off-by: Kaung Zin Hein <[email protected]>
- Loading branch information
1 parent
75999bd
commit 93e0e53
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
dev-packages/node-integration-tests/suites/tracing/socket.io/scenario.js
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,54 @@ | ||
const { loggingTransport, sendPortToRunner } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const http = require('http'); | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const { Server } = require('socket.io'); | ||
const ioc = require('socket.io-client'); | ||
|
||
const PORT = 3005; | ||
|
||
const run = async () => { | ||
const app = express(); | ||
app.use(cors()); | ||
|
||
const expressServer = http.createServer(app); | ||
|
||
const io = new Server(expressServer); | ||
|
||
app.get('/', (_req, res) => { | ||
io.emit('test', 'TEST MESSAGE'); | ||
res.send('123'); | ||
}); | ||
|
||
expressServer.listen(PORT, () => { | ||
io.on('connection', client => { | ||
client.on('test_reply', _data => { | ||
client.disconnect(); | ||
io.close(); | ||
}); | ||
}); | ||
|
||
const clientSocket = ioc(`http://localhost:${PORT}`); | ||
|
||
clientSocket.on('test', _msg => { | ||
clientSocket.emit('test_reply', { test_key: 'test_value' }); | ||
}); | ||
}); | ||
|
||
sendPortToRunner(PORT); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
59 changes: 59 additions & 0 deletions
59
dev-packages/node-integration-tests/suites/tracing/socket.io/test.ts
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,59 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('socket.io auto instrumentation', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('should auto-instrument `socket.io` package', done => { | ||
const SERVER_TRANSACTION = { | ||
transaction: 'GET /', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
origin: 'auto.socket.otel.socket_io', | ||
data: { | ||
'sentry.origin': 'auto.socket.otel.socket_io', | ||
'messaging.destination': '/', | ||
'messaging.destination_kind': 'topic', | ||
'messaging.socket.io.event_name': 'test', | ||
'messaging.socket.io.namespace': '/', | ||
'messaging.system': 'socket.io', | ||
'otel.kind': 'PRODUCER', | ||
'sentry.op': 'message', | ||
}, | ||
description: '/ send', | ||
op: 'message', | ||
status: 'ok', | ||
}), | ||
]), | ||
}; | ||
|
||
const CLIENT_TRANSACTION = { | ||
transaction: 'test_reply receive', | ||
contexts: { | ||
trace: expect.objectContaining({ | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
data: expect.objectContaining({ | ||
'sentry.op': 'message', | ||
'sentry.origin': 'auto.socket.otel.socket_io', | ||
'otel.kind': 'CONSUMER', | ||
'messaging.system': 'socket.io', | ||
'messaging.destination': '/', | ||
'messaging.operation': 'receive', | ||
'messaging.socket.io.event_name': 'test_reply', | ||
}), | ||
origin: 'auto.socket.otel.socket_io', | ||
op: 'message', | ||
status: 'ok', | ||
}), | ||
}, | ||
}; | ||
|
||
createRunner(__dirname, 'scenario.js') | ||
.expect({ transaction: SERVER_TRANSACTION }) | ||
.expect({ transaction: CLIENT_TRANSACTION }) | ||
.start(done) | ||
.makeRequest('get', '/'); | ||
}); | ||
}); |
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