Skip to content

Commit

Permalink
Add test for keepAlive+maxSockets when using https
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Mar 29, 2024
1 parent e10a555 commit 4650f93
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/agent-base/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,5 +568,63 @@ describe('Agent (TypeScript)', () => {
server.close();
}
});

it('should support `keepAlive: true` with `maxSockets`', async () => {
let reqCount = 0;
let connectCount = 0;

class MyAgent extends Agent {
async connect(
_req: http.ClientRequest,
opts: AgentConnectOpts
) {
connectCount++;
assert(opts.secureEndpoint === true);
await sleep(10);
return tls.connect(opts);
}
}
const agent = new MyAgent({ keepAlive: true, maxSockets: 1 });

const server = https.createServer(sslOptions, async (req, res) => {
expect(req.headers.connection).toEqual('keep-alive');
reqCount++;
await sleep(10);
res.end();
});
const addr = await listen(server);

try {
const resPromise = req(new URL('/foo', addr), {
agent,
rejectUnauthorized: false,
});
const res2Promise = req(new URL('/another', addr), {
agent,
rejectUnauthorized: false,
});

const res = await resPromise;
expect(reqCount).toEqual(1);
expect(connectCount).toEqual(1);
expect(res.headers.connection).toEqual('keep-alive');

res.resume();
const s1 = res.socket;
await once(s1, 'free');

const res2 = await res2Promise;
expect(reqCount).toEqual(2);
expect(connectCount).toEqual(1);
expect(res2.headers.connection).toEqual('keep-alive');
assert(res2.socket === s1);

res2.resume();
await once(res2.socket, 'free');
} finally {
agent.destroy();
server.close();
}
});
});
});

0 comments on commit 4650f93

Please sign in to comment.