forwardemail / forwardemail/supertest

Supertest binding to different ephemeral port on every request?

Open
#489 3 comments 5 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

While testing with supertest I have noticed that after a test run netstat reports a connection in TIME_WAIT state for every call (get, post, etc.) made with a supertest agent. This can become a problem for me doing frequent and larger test runs - my system actually runs out of either file handles or ports (not sure which.)

My app binds to port 3000 and it's my understanding that if the app is bound to a port supertest won't bind to an ephemeral port.

Here's an abridged version of how my app and test suite are written:

app.js
```
const express = require('express');
...
let app = express();
...
app.listen(process.env.PORT, function () {
logger.info('App listening on port ' + process.env.PORT);
});

module.exports = app;
```

test.js
```
let app = require('../app.js'),
agent = require('supertest').agent(app),
assert = require('chai').assert;

describe('/api/auth/ typical sign up and login', function () {

let email = 'foo@example.com',
password = 'foobar',
path = '/api';

it('gets no results', async () => {
let res = await agent
.get(path + '/credentials/')
.expect(200);

assert.doesNotHaveAnyKeys(res.body, ['email']);
});

...
}
```

For every `await agent.(get|post|put)...` there seems to be a connection on a different port. Is there some way to reduce the number of connections opened in my test suite? Is it possible to reuse a connection, or even just have connections close so they're not stuck in TIME_WAIT taking up resources?

Below is a sample output from netstat after a suite that made 11 "agent" calls - ports 6379 and 5432 are connections to redis and postgres respectively, all the rest I think are between supertest and my app:
```
tcp4 0 0 127.0.0.1.62021 127.0.0.1.6379 TIME_WAIT
tcp4 0 0 127.0.0.1.62023 127.0.0.1.62024 TIME_WAIT
tcp4 0 0 127.0.0.1.62026 127.0.0.1.62027 TIME_WAIT
tcp4 0 0 127.0.0.1.62028 127.0.0.1.62029 TIME_WAIT
tcp4 0 0 127.0.0.1.62030 127.0.0.1.62031 TIME_WAIT
tcp4 0 0 127.0.0.1.62032 127.0.0.1.62033 TIME_WAIT
tcp4 0 0 127.0.0.1.62034 127.0.0.1.62035 TIME_WAIT
tcp4 0 0 127.0.0.1.62036 127.0.0.1.62037 TIME_WAIT
tcp4 0 0 127.0.0.1.62038 127.0.0.1.62039 TIME_WAIT
tcp4 0 0 127.0.0.1.62040 127.0.0.1.62041 TIME_WAIT
tcp4 0 0 127.0.0.1.62042 127.0.0.1.62043 TIME_WAIT
tcp4 0 0 127.0.0.1.62044 127.0.0.1.62045 TIME_WAIT
tcp4 0 0 127.0.0.1.62022 127.0.0.1.6379 TIME_WAIT
tcp4 0 0 127.0.0.1.62025 127.0.0.1.5432 TIME_WAIT
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.