Skip to content

Commit bf12317

Browse files
committed
starting with abort controller
1 parent fe87306 commit bf12317

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

lib/http-client.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Agent, setGlobalDispatcher, request, MockAgent } from 'undici';
1+
import { Agent, request } from 'undici';
22
import createError from 'http-errors';
33
import Opossum from 'opossum';
44
import abslog from 'abslog';
@@ -31,6 +31,7 @@ export default class HttpClient extends EventEmitter {
3131
*/
3232
constructor({
3333
abortController = undefined,
34+
autoRenewAbortController = false,
3435
keepAliveMaxTimeout = undefined,
3536
keepAliveTimeout = undefined,
3637
connections = 50,
@@ -55,6 +56,7 @@ export default class HttpClient extends EventEmitter {
5556
...(this.#abortController && {
5657
abortController: this.#abortController,
5758
}),
59+
...(autoRenewAbortController && { autoRenewAbortController }),
5860
errorThresholdPercentage: threshold, // When X% of requests fail, trip the circuit
5961
resetTimeout: reset, // After X milliseconds, try again.
6062
timeout, // If our function takes longer than X milliseconds, trigger a failure
@@ -127,10 +129,4 @@ export default class HttpClient extends EventEmitter {
127129
await this.#agent.close();
128130
}
129131
}
130-
131-
// static mock(origin) {
132-
// const agent = new MockAgent();
133-
// setGlobalDispatcher(agent);
134-
// return agent.get(origin);
135-
// }
136132
}

tests/http-client.test.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ let httpServer,
99
host = 'localhost',
1010
port = 3003;
1111

12-
const url = `http://${host}:${port}`;
13-
1412
function beforeEach() {
1513
httpServer = http.createServer(async (request, response) => {
1614
response.writeHead(200);
@@ -19,15 +17,14 @@ function beforeEach() {
1917
httpServer.listen(port, host);
2018
}
2119

22-
function closeServer() {
20+
function closeServer(s) {
2321
return new Promise((resolve) => {
24-
httpServer.close(resolve);
25-
console.log('Closed');
22+
s.close(resolve);
2623
});
2724
}
28-
async function afterEach(client) {
25+
async function afterEach(client, server = httpServer) {
2926
await client.close();
30-
await closeServer();
27+
await closeServer(server);
3128
}
3229

3330
async function queryUrl({
@@ -46,47 +43,80 @@ async function queryUrl({
4643
}
4744
}
4845
await test('http-client - basics', async (t) => {
46+
const url = `http://${host}:2001`;
47+
const server = http.createServer(async (request, response) => {
48+
response.writeHead(200);
49+
response.end();
50+
});
51+
server.listen(2001, host);
4952
await t.test('returns 200 response when given valid input', async () => {
50-
beforeEach();
5153
const client = new HttpClient();
5254
const response = await client.request({
5355
path: '/',
5456
origin: url,
5557
method: 'GET',
5658
});
5759
assert.strictEqual(response.statusCode, 200);
58-
await afterEach(client);
60+
await client.close();
5961
});
6062

61-
// await t.test('does not cause havoc with built in fetch', async () => {
62-
// beforeEach();
63-
// const client = new HttpClient();
64-
// await fetch(url);
65-
// const response = await client.request({
66-
// path: '/',
67-
// origin: url,
68-
// method: 'GET',
69-
// });
70-
// assert.strictEqual(response.statusCode, 200);
71-
// await client.close();
72-
// await fetch(url);
73-
// await afterEach(client);
74-
// });
75-
await t.test('can pass in an abort controller', async () => {
76-
beforeEach();
77-
const abortController = new AbortController();
78-
63+
await t.test('does not cause havoc with built in fetch', async () => {
7964
const client = new HttpClient();
65+
await fetch(url);
8066
const response = await client.request({
8167
path: '/',
8268
origin: url,
8369
method: 'GET',
8470
});
85-
await afterEach(client);
71+
assert.strictEqual(response.statusCode, 200);
72+
await client.close();
73+
await fetch(url);
74+
await client.close();
8675
});
76+
await closeServer(server);
77+
});
78+
79+
await test('http-client - abort controller', async (t) => {
80+
// Slow responding server to enable us to abort a request
81+
const slowServer = http.createServer(async (request, response) => {
82+
await wait(200);
83+
response.writeHead(200);
84+
response.end();
85+
});
86+
slowServer.listen(2010, host);
87+
await t.test('cancel a request', async () => {
88+
const abortController = new AbortController();
89+
let aborted = false;
90+
setTimeout(() => {
91+
abortController.abort();
92+
aborted = true;
93+
}, 100);
94+
const client = new HttpClient({ timeout: 2000 });
95+
await client.request({
96+
path: '/',
97+
origin: 'http://localhost:2010',
98+
method: 'GET',
99+
});
100+
assert.ok(aborted);
101+
await client.close();
102+
});
103+
104+
// await t.test('auto renew an abort controller', async () => {
105+
// const abortController = new AbortController();
106+
// const client = new HttpClient({ timeout: 2000 });
107+
// await client.request({
108+
// autoRenewAbortController: true,
109+
// path: '/',
110+
// origin: 'http://localhost:2010',
111+
// method: 'GET',
112+
// });
113+
// await client.close();
114+
// });
115+
slowServer.close();
87116
});
88117

89118
await test('http-client - circuit breaker behaviour', async (t) => {
119+
const url = `http://${host}:${port}`;
90120
await t.test('opens on failure threshold', async () => {
91121
beforeEach();
92122
const invalidUrl = `http://${host}:3013`;

tests/utilities.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ export async function wait(waitTime = 1000) {
1010
}, waitTime);
1111
});
1212
}
13+
export async function slowResponder(func, timeout = 1000) {
14+
return new Promise((resolve) => {
15+
setTimeout(async () => {
16+
await func();
17+
resolve();
18+
}, timeout);
19+
});
20+
}

0 commit comments

Comments
 (0)