Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't close used mock agent #170

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ module.exports.default = mock;
Object.assign(mock, mm, {
restore() {
cluster.restore();
mm.restore();
// return promise
return mockAgent.restore(app);
mockAgent.restore(app);
return mm.restore();
},

/**
Expand Down
20 changes: 8 additions & 12 deletions lib/mock_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ module.exports = {
getAgent(app) {
if (!_global) {
_global = getGlobalDispatcher();
if (typeof app?.httpclient?.getDispatcher === 'function') {
if (!app[APP_HTTPCLIENT_AGENT]) {
// save the raw dispatcher
app[APP_HTTPCLIENT_AGENT] = app.httpclient.getDispatcher();
}
}
}
if (!app[APP_HTTPCLIENT_AGENT] && typeof app?.httpclient?.getDispatcher === 'function') {
// save the raw dispatcher
app[APP_HTTPCLIENT_AGENT] = app.httpclient.getDispatcher();
}
if (!_mockAgent) {
_mockAgent = new MockAgent();
Expand All @@ -24,16 +22,14 @@ module.exports = {
}
return _mockAgent;
},
async restore(app) {
restore(app) {
if (!_mockAgent) return;
if (_global) {
setGlobalDispatcher(_global);
if (app?.[APP_HTTPCLIENT_AGENT]) {
app.httpclient.setDispatcher(app[APP_HTTPCLIENT_AGENT]);
}
}
const agent = _mockAgent;
if (app?.[APP_HTTPCLIENT_AGENT]) {
app.httpclient.setDispatcher(app[APP_HTTPCLIENT_AGENT]);
}
_mockAgent = null;
await agent.close();
},
};
Loading