Skip to content

Commit

Permalink
fix: make sure kClients exists on Agent (#482)
Browse files Browse the repository at this point in the history
> TypeError: clients is not iterable
  • Loading branch information
fengmk2 authored Dec 22, 2023
1 parent 7567423 commit 574bd47
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ export class HttpClient extends EventEmitter {
getDispatcherPoolStats() {
const agent = this.getDispatcher();
// origin => Pool Instance
const clients: Map<string, WeakRef<Pool>> = agent[undiciSymbols.kClients];
const clients: Map<string, WeakRef<Pool>> | undefined = agent[undiciSymbols.kClients];
const poolStatsMap: Record<string, PoolStat> = {};
if (!clients) {
return poolStatsMap;
}
for (const [ key, ref ] of clients) {
const pool = ref.deref();
const stats = pool?.stats;
Expand Down
2 changes: 2 additions & 0 deletions test/HttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('HttpClient.test.ts', () => {
return true;
},
});
assert.equal(Object.keys(httpclient.getDispatcherPoolStats()).length, 0);

await assert.rejects(async () => {
await httpclient.request(_url);
Expand All @@ -112,6 +113,7 @@ describe('HttpClient.test.ts', () => {

const response = await httpclient.request(_url);
assert.equal(response.status, 200);
assert.equal(Object.keys(httpclient.getDispatcherPoolStats()).length, 1);
});

it('should check non-ip hostname with custom lookup', async () => {
Expand Down
15 changes: 10 additions & 5 deletions test/keep-alive-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ describe('keep-alive-header.test.ts', () => {
try {
const task = httpClient.request(_url);
console.log('after request stats: %o', httpClient.getDispatcherPoolStats());
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].size, 1);
if (httpClient.getDispatcherPoolStats()[origin]) {
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 1);
assert.equal(httpClient.getDispatcherPoolStats()[origin].size, 1);
}
let response = await task;
console.log('after response stats: %o', httpClient.getDispatcherPoolStats());
assert.equal(httpClient.getDispatcherPoolStats()[origin].pending, 0);
Expand Down Expand Up @@ -130,12 +132,15 @@ describe('keep-alive-header.test.ts', () => {
assert.equal(httpClient.getDispatcherPoolStats()[origin].free, 1);
await sleep(keepAliveTimeout);
console.log('after sleep stats: %o', httpClient.getDispatcherPoolStats());
// clients maybe all gone => after sleep stats: {}
// { connected: 0, free: 0, pending: 0, queued: 0, running: 0, size: 0 }
// { connected: 1, free: 1, pending: 0, queued: 0, running: 0, size: 0 }
// { connected: 2, free: 2, pending: 0, queued: 0, running: 0, size: 0 }
assert(httpClient.getDispatcherPoolStats()[origin].connected <= 2);
assert(httpClient.getDispatcherPoolStats()[origin].free <= 2);
assert.equal(httpClient.getDispatcherPoolStats()[origin].size, 0);
if (Object.keys(httpClient.getDispatcherPoolStats()).length > 0) {
assert(httpClient.getDispatcherPoolStats()[origin].connected <= 2);
assert(httpClient.getDispatcherPoolStats()[origin].free <= 2);
assert.equal(httpClient.getDispatcherPoolStats()[origin].size, 0);
}
} catch (err) {
if (err.message === 'other side closed') {
console.log(err);
Expand Down

0 comments on commit 574bd47

Please sign in to comment.