Skip to content

test(node): Add integration tests for aggregated sessions. #4832

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

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';

import { getEnvelopeRequest, runServer } from '../../../utils';

test('should aggregate successful and crashed sessions', async () => {
const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const envelope = await Promise.race([
getEnvelopeRequest(`${url}/success`),
getEnvelopeRequest(`${url}/error_unhandled`),
getEnvelopeRequest(`${url}/success_slow`),
]);

expect(envelope).toHaveLength(3);
expect(envelope[0]).toMatchObject({
sent_at: expect.any(String),
sdk: {
name: 'sentry.javascript.node',
version: expect.any(String),
},
});

expect(envelope[1]).toMatchObject({
type: 'sessions',
});

expect(envelope[2]).toMatchObject({
aggregates: [
{
started: expect.any(String),
exited: 2,
crashed: 1,
},
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'path';

import { getEnvelopeRequest, runServer } from '../../../utils';

test('should aggregate successful, crashed and erroneous sessions', async () => {
const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const envelope = await Promise.race([
getEnvelopeRequest(`${url}/success_slow`),
getEnvelopeRequest(`${url}/error_handled`),
getEnvelopeRequest(`${url}/error_unhandled`),
]);

expect(envelope).toHaveLength(3);
expect(envelope[0]).toMatchObject({
sent_at: expect.any(String),
sdk: {
name: 'sentry.javascript.node',
version: expect.any(String),
},
});

expect(envelope[1]).toMatchObject({
type: 'sessions',
});

expect(envelope[2]).toMatchObject({
aggregates: [
{
started: expect.any(String),
exited: 1,
crashed: 1,
errored: 1,
},
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';

import { getEnvelopeRequest, runServer } from '../../../utils';

test('should aggregate successful sessions', async () => {
const url = await runServer(__dirname, `${path.resolve(__dirname, '..')}/server.ts`);

const envelope = await Promise.race([
getEnvelopeRequest(`${url}/success`),
getEnvelopeRequest(`${url}/success_next`),
getEnvelopeRequest(`${url}/success_slow`),
]);

expect(envelope).toHaveLength(3);
expect(envelope[0]).toMatchObject({
sent_at: expect.any(String),
sdk: {
name: 'sentry.javascript.node',
version: expect.any(String),
},
});

expect(envelope[1]).toMatchObject({
type: 'sessions',
});

expect(envelope[2]).toMatchObject({
aggregates: [
{
started: expect.any(String),
exited: 3,
},
],
});
});
60 changes: 60 additions & 0 deletions packages/node-integration-tests/suites/sessions/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable no-console */
import * as Sentry from '@sentry/node';
import express from 'express';

const app = express();

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
});

app.use(Sentry.Handlers.requestHandler());

// ### Taken from manual tests ###
// Hack that resets the 60s default flush interval, and replaces it with just a one second interval
// @ts-ignore: need access to `_sessionFlusher`
const flusher = (Sentry.getCurrentHub()?.getClient() as Sentry.NodeClient)?._sessionFlusher;

// @ts-ignore: need access to `_intervalId`
let flusherIntervalId = flusher?._intervalId;

clearInterval(flusherIntervalId);

// @ts-ignore: need access to `_intervalId`
flusherIntervalId = flusher?._intervalId = setInterval(() => flusher?.flush(), 1000);

// @ts-ignore: need access to `_intervalId` again
setTimeout(() => clearInterval(flusherIntervalId), 3000);

app.get('/test/success', (_req, res) => {
res.send('Success!');
});

app.get('/test/success_next', (_req, res, next) => {
res.send('Success!');
next();
});

app.get('/test/success_slow', async (_req, res) => {
await new Promise(res => setTimeout(res, 500));

res.send('Success!');
});

app.get('/test/error_unhandled', () => {
throw new Error('Crash!');
});

app.get('/test/error_handled', (_req, res) => {
try {
throw new Error('Crash!');
} catch (e) {
Sentry.captureException(e);
}
res.send('Crash!');
});

app.use(Sentry.Handlers.errorHandler());

export default app;
5 changes: 3 additions & 2 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ export async function runServer(testDir: string, serverPath?: string, scenarioPa

app.get('/test', async () => {
require(scenarioPath || `${testDir}/scenario`);

setTimeout(() => server.close(), 500);
});

const server = app.listen(port, () => {
resolve();
setTimeout(() => {
server.close();
}, 4000);
});
});

Expand Down