-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: spwan pool on request when use agent (#207)
* fix: spwan pool on request when use agent * test: ensure pool is not spawn twice for same host
- Loading branch information
1 parent
95f2d48
commit 6a31087
Showing
3 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
const proxyquire = require('proxyquire') | ||
const http = require('http') | ||
const get = require('simple-get').concat | ||
const undici = require('undici') | ||
const { getUndiciOptions } = require('../lib/request') | ||
|
||
t.plan(10) | ||
|
||
const instance = Fastify() | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
const target = http.createServer((req, res) => { | ||
res.statusCode = 200 | ||
res.end('hello world') | ||
}) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from() | ||
}) | ||
|
||
t.teardown(target.close.bind(target)) | ||
|
||
target.listen(0, err => { | ||
t.error(err) | ||
let poolCreation = 0 | ||
|
||
const From = proxyquire('..', { | ||
'./lib/request.js': proxyquire('../lib/request.js', { | ||
undici: proxyquire('undici', { | ||
'./lib/agent': proxyquire('undici/lib/agent.js', { | ||
'./pool': class Pool extends undici.Pool { | ||
constructor (url, options) { | ||
super(url, options) | ||
poolCreation++ | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
instance.register(From, { | ||
base: `http://localhost:${target.address().port}`, | ||
undici: buildUndiciOptions() | ||
}) | ||
|
||
instance.listen(0, err => { | ||
t.error(err) | ||
|
||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.toString(), 'hello world') | ||
t.equal(poolCreation, 1) | ||
|
||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equal(data.toString(), 'hello world') | ||
t.equal(poolCreation, 1) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function buildUndiciOptions () { | ||
return getUndiciOptions({ | ||
connections: 42, | ||
pipelining: 24, | ||
keepAliveTimeout: 4242, | ||
strictContentLength: false | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters