Skip to content

Commit a55e4d5

Browse files
authored
test: Fix node-integration-test timeouts & cleanup (#13280)
This PR streamlines and fixes some timing/cleanup issues we had in integration tests, which sometimes lead to issues. One problem was introduced here: #13253 This change lead to the server being shut down (because `done` is called) before the HTTP requests are finished, leading to error logs. Another problem was that in some cases we had leaking processes, where we did not properly close servers we started - this was everywhere we used `createTestServer`. I also moved some code from the node-integration-tests package to the remix package, that was only used there (and not properly depended/imported on). For future debugging, this was shown by running tests with `--detectOpenHandles`.
1 parent 88fef5b commit a55e4d5

File tree

38 files changed

+369
-324
lines changed

38 files changed

+369
-324
lines changed

dev-packages/node-integration-tests/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ requests, and assertions. Test server, interceptors and assertions are all run o
3838

3939
`utils/` contains helpers and Sentry-specific assertions that can be used in (`test.ts`).
4040

41-
`TestEnv` class contains methods to create and execute requests on a test server instance. `TestEnv.init()` which starts
42-
a test server and returns a `TestEnv` instance must be called by each test. The test server is automatically shut down
43-
after each test, if a data collection helper method such as `getEnvelopeRequest` and `getAPIResponse` is used. Tests
44-
that do not use those helper methods will need to end the server manually.
45-
46-
`TestEnv` instance has two public properties: `url` and `server`. The `url` property is the base URL for the server. The
47-
`http.Server` instance is used to finish the server eventually.
48-
4941
Nock interceptors are internally used to capture envelope requests by `getEnvelopeRequest` and
5042
`getMultipleEnvelopeRequest` helpers. After capturing required requests, the interceptors are removed. Nock can manually
5143
be used inside the test cases to intercept requests but should be removed before the test ends, as not to cause

dev-packages/node-integration-tests/jest.setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { cleanupChildProcesses } = require('./utils/runner');
22

3-
// Increases test timeout from 5s to 45s
4-
jest.setTimeout(45000);
3+
// Default timeout: 15s
4+
jest.setTimeout(15000);
55

66
afterEach(() => {
77
cleanupChildProcesses();

dev-packages/node-integration-tests/suites/express/multiple-init/server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ app.get('/test/error/:id', (req, res) => {
5454

5555
setTimeout(() => {
5656
// We flush to ensure we are sending exceptions in a certain order
57-
Sentry.flush(3000).then(
57+
Sentry.flush(1000).then(
5858
() => {
59+
// We send this so we can wait for this, to know the test is ended & server can be closed
60+
if (id === '3') {
61+
Sentry.captureException(new Error('Final exception was captured'));
62+
}
5963
res.send({});
6064
},
6165
() => {
6266
res.send({});
6367
},
6468
);
65-
}, 1000);
69+
}, 1);
6670
});
6771

6872
Sentry.setupExpressErrorHandler(app);

dev-packages/node-integration-tests/suites/express/multiple-init/test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ test('allows to call init multiple times', done => {
4848
},
4949
},
5050
})
51+
.expect({
52+
event: {
53+
exception: {
54+
values: [
55+
{
56+
value: 'Final exception was captured',
57+
},
58+
],
59+
},
60+
},
61+
})
5162
.start(done);
5263

5364
runner

dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import * as path from 'path';
33
import { conditionalTest } from '../../../utils';
44
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
55

6+
// This test takes some time because it connects the debugger etc.
7+
// So we increase the timeout here
8+
jest.setTimeout(45_000);
9+
610
const EXPECTED_LOCAL_VARIABLES_EVENT = {
711
exception: {
812
values: [

dev-packages/node-integration-tests/suites/tracing/connect/scenario.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Sentry.init({
1313
const connect = require('connect');
1414
const http = require('http');
1515

16-
const init = async () => {
16+
const run = async () => {
1717
const app = connect();
1818

1919
app.use('/', function (req, res, next) {
@@ -34,4 +34,4 @@ const init = async () => {
3434
sendPortToRunner(port);
3535
};
3636

37-
init();
37+
run();

dev-packages/node-integration-tests/suites/tracing/connect/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
22

3-
jest.setTimeout(20000);
4-
53
describe('connect auto-instrumentation', () => {
64
afterAll(async () => {
75
cleanupChildProcesses();

dev-packages/node-integration-tests/suites/tracing/hapi/scenario.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Boom = require('@hapi/boom');
1313

1414
const port = 5999;
1515

16-
const init = async () => {
16+
const run = async () => {
1717
const server = Hapi.server({
1818
host: 'localhost',
1919
port,
@@ -65,4 +65,4 @@ const init = async () => {
6565
sendPortToRunner(port);
6666
};
6767

68-
init();
68+
run();

dev-packages/node-integration-tests/suites/tracing/hapi/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
22

3-
jest.setTimeout(20000);
4-
53
describe('hapi auto-instrumentation', () => {
64
afterAll(async () => {
75
cleanupChildProcesses();

dev-packages/node-integration-tests/suites/tracing/mongodb/test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { MongoMemoryServer } from 'mongodb-memory-server-global';
22

33
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
44

5-
jest.setTimeout(20000);
6-
75
describe('MongoDB experimental Test', () => {
86
let mongoServer: MongoMemoryServer;
97

0 commit comments

Comments
 (0)