Skip to content

Commit

Permalink
Make getName() public and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Mar 29, 2024
1 parent 6520a4a commit e10a555
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
6 changes: 5 additions & 1 deletion packages/agent-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export abstract class Agent extends http.Agent {

// In order to properly update the socket pool, we need to call `getName()` on
// the core `https.Agent` if it is a secureEndpoint.
private getName({ secureEndpoint, ...options }: AgentConnectOpts) {
getName(options: AgentConnectOpts): string {
const secureEndpoint =
typeof options.secureEndpoint === 'boolean'
? options.secureEndpoint
: this.isSecureEndpoint(options);
if (secureEndpoint) {
// @ts-expect-error `getName()` isn't defined in `@types/node`
return HttpsAgent.prototype.getName.call(this, options);
Expand Down
98 changes: 54 additions & 44 deletions packages/agent-base/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Agent (TypeScript)', () => {
) {
gotCallback = true;
assert(opts.secureEndpoint === false);
assert.equal(this.getName(opts), `127.0.0.1:${port}:`);
return net.connect(opts);
}
}
Expand Down Expand Up @@ -308,55 +309,60 @@ describe('Agent (TypeScript)', () => {
server2.close();
}
});
});

it('should support `keepAlive: true` with `maxSockets`', async () => {
let reqCount = 0;
let connectCount = 0;
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 === false);
await sleep(10);
return net.connect(opts);
class MyAgent extends Agent {
async connect(
_req: http.ClientRequest,
opts: AgentConnectOpts
) {
connectCount++;
assert(opts.secureEndpoint === false);
await sleep(10);
return net.connect(opts);
}
}
}
const agent = new MyAgent({ keepAlive: true, maxSockets: 1 });
const agent = new MyAgent({ keepAlive: true, maxSockets: 1 });

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

const server = http.createServer(async (req, res) => {
expect(req.headers.connection).toEqual('keep-alive');
reqCount++;
await sleep(10);
res.end();
try {
const resPromise = req(new URL('/foo', addr), { agent });
const res2Promise = req(new URL('/another', addr), {
agent,
});

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();
}
});
const addr = await listen(server);

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

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();
}
});

describe('"https" module', () => {
Expand All @@ -371,6 +377,10 @@ describe('Agent (TypeScript)', () => {
): net.Socket {
gotCallback = true;
assert(opts.secureEndpoint === true);
assert.equal(
this.getName(opts),
`127.0.0.1:${port}::::::::false:::::::::::::`
);
return tls.connect(opts);
}
}
Expand Down

0 comments on commit e10a555

Please sign in to comment.