supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Concurrent requests result in ECONNREFUSED

Open Drew-Kimberly opened this issue 4 years ago • 2 comments

Hi there! 👋

I am attempting to write an integration test case with supertest in which a couple requests are sent to the app concurrently. However, I get an ECONNREFUSED error when the test is executed. Is it possible to support concurrent requests?

Attempted usage:

it('can make concurrent GET requests to the app', async () => {
    const request1 = testApp.get('/');
    const request2 = testApp.get('/');
    await request1.expect(200);
    await request2.expect(200);
});

Here is a demo repo where the issue can be easily reproduced: https://github.com/Drew-Kimberly/supertest-repro

Drew-Kimberly avatar Jul 05 '21 13:07 Drew-Kimberly

This can be done by using a slightly different implementation (by leveraging the end callback):

const app = require('./app');
const request = require('supertest');

let testApp;

const makeGetRequest = path => new Promise((resolve, reject) => {
    testApp.get(path).end((err, res) => {
        if (err) {
            return reject(err);
        }

        resolve(res);
    });
});

beforeEach(() => {
    testApp = request(app);
});

it('can make concurrent GET requests to the app', async () => {
    const request1 = makeGetRequest('/');
    const request2 = makeGetRequest('/');
    await request1;
    await request2;
});

Demonstrated in this branch: https://github.com/Drew-Kimberly/supertest-repro/tree/workaround

I'm going to leave this open just to get some thoughts on why the first implementation results in the hard error.

Drew-Kimberly avatar Jul 05 '21 13:07 Drew-Kimberly

We get a few flakes, around 2-3 each test run in our codebase with ~2000 tests

(same stack trace but with EHOSTUNREACH)

magicmark avatar Oct 08 '21 04:10 magicmark