From 02db483962ee20a6c2bc02a0243168c9255f6e6e Mon Sep 17 00:00:00 2001 From: Matt Schile Date: Thu, 5 Dec 2024 08:49:23 -0700 Subject: [PATCH] test: fix proxy spec flake (#30717) --- .../test/integration/proxy_spec.js | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/https-proxy/test/integration/proxy_spec.js b/packages/https-proxy/test/integration/proxy_spec.js index 957b56c66761..15268c09e04c 100644 --- a/packages/https-proxy/test/integration/proxy_spec.js +++ b/packages/https-proxy/test/integration/proxy_spec.js @@ -90,29 +90,31 @@ describe('Proxy', () => { }) }) - it('closes outgoing connections when client disconnects', function () { + it('closes outgoing connections when client disconnects', async function () { this.sandbox.spy(net, 'connect') - return request({ + await request({ strictSSL: false, url: 'https://localhost:8444/replace', proxy: 'http://localhost:3333', resolveWithFullResponse: true, }) - .then(() => { - // ensure the outgoing socket created for this connection was destroyed - expect(net.connect).calledOnce - const socket = net.connect.getCalls()[0].returnValue + // ensure the outgoing socket created for this connection was destroyed + expect(net.connect).calledOnce - return new Promise((resolve) => { + const socket = net.connect.getCalls()[0].returnValue + + // sometimes the close event happens before we can attach the listener, causing this test to flake + if (!socket.destroyed || !socket.readyState === 'closed') { + await new Promise((resolve) => { socket.on('close', () => { expect(socket.destroyed).to.be.true resolve() }) }) - }) + } }) it('can boot the httpServer', () => { @@ -127,7 +129,9 @@ describe('Proxy', () => { }) }) - context('generating certificates', () => { + context('generating certificates', function () { + this.retries(4) + it('reuses existing certificates', function () { return request({ strictSSL: false, @@ -270,13 +274,13 @@ describe('Proxy', () => { process.env.npm_config_noproxy = 'just,some,nonsense' process.env.NO_PROXY = '' - process.env.HTTP_PROXY = process.env.HTTPS_PROXY = 'http://localhost:9001' + process.env.HTTP_PROXY = process.env.HTTPS_PROXY = 'http://localhost:2222' this.upstream = new DebugProxy({ keepRequests: true, }) - return this.upstream.start(9001) + return this.upstream.start(2222) }) it('passes a request to an https server through the upstream', function () { @@ -309,7 +313,7 @@ describe('Proxy', () => { return true } - process.env.HTTP_PROXY = (process.env.HTTPS_PROXY = 'http://foo:bar@localhost:9001') + process.env.HTTP_PROXY = process.env.HTTPS_PROXY = 'http://foo:bar@localhost:2222' return request({ strictSSL: false, @@ -335,10 +339,9 @@ describe('Proxy', () => { expect(net.connect).calledOnce const socket = net.connect.getCalls()[0].returnValue - // sometimes the close event happens before we can attach the listener, - // causing this test to flake + // sometimes the close event happens before we can attach the listener, causing this test to flake if (!socket.destroyed || !socket.readyState === 'closed') { - return new Promise((resolve) => { + await new Promise((resolve) => { socket.on('close', () => { expect(socket.destroyed).to.be.true