Skip to content

Commit

Permalink
fix: should reset agent on restore (#171)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced new test cases to verify auto restoration after each test
and to ensure the usage of the first mock data on duplicate URL mock.

- **Bug Fixes**
- Corrected a typo in a comment from "asyncrounus" to "asynchronous" for
clarity in `lib/app.js`.

- **Refactor**
- Updated `mockAgentRestore` and `restore` functions to use async/await
for better handling of asynchronous operations.
- Enhanced logic to manage multiple `httpclient` instances with debug
logging.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jul 4, 2024
1 parent e2a4b26 commit 088ac41
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/extend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
return mockAgent.getAgent(this);
},

mockAgentRestore() {
return mockAgent.restore(this);
async mockAgentRestore() {
await mockAgent.restore();
},

/**
Expand Down
4 changes: 2 additions & 2 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ module.exports = {
return mockAgent.getAgent(this);
},

mockAgentRestore() {
return mockAgent.restore(this);
async mockAgentRestore() {
await mockAgent.restore();
},

/**
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ module.exports.default = mock;

// inherit & extends mm
Object.assign(mock, mm, {
restore() {
async restore() {
cluster.restore();
mockAgent.restore(app);
// return promise
return mm.restore();
await mockAgent.restore();
return await mm.restore();
},

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module.exports = function(options) {
// don't delegate properties on MockApplication
if (MOCK_APP_METHOD.includes(prop)) return getProperty(target, prop);
if (!target[APP_INIT]) throw new Error(`can't get ${prop} before ready`);
// it's asyncrounus when agent and app are loading,
// it's asynchronous when agent and app are loading,
// so should get the properties after loader ready
debug('proxy handler.get %s', prop);
return target._app[prop];
Expand Down
15 changes: 12 additions & 3 deletions lib/mock_agent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { debuglog } = require('util');
const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib');

const debug = debuglog('egg-mock:lib:mock_agent');

let _mockAgent;
let _global;
const APP_HTTPCLIENT_AGENT = Symbol('app.httpclient.agent');
const httpClients = [];

module.exports = {
getAgent(app) {
Expand All @@ -12,6 +16,8 @@ module.exports = {
if (!app?.httpclient?.[APP_HTTPCLIENT_AGENT] && typeof app?.httpclient?.getDispatcher === 'function') {
// save the raw dispatcher
app.httpclient[APP_HTTPCLIENT_AGENT] = app.httpclient.getDispatcher();
httpClients.push(app.httpclient);
debug('add new httpClient, size: %d', httpClients.length);
}
if (!_mockAgent) {
_mockAgent = new MockAgent();
Expand All @@ -22,14 +28,17 @@ module.exports = {
}
return _mockAgent;
},
restore(app) {
async restore() {
if (!_mockAgent) return;
if (_global) {
setGlobalDispatcher(_global);
}
if (app?.httpclient?.[APP_HTTPCLIENT_AGENT]) {
app.httpclient.setDispatcher(app.httpclient[APP_HTTPCLIENT_AGENT]);
for (const httpClient of httpClients) {
httpClient.setDispatcher(httpClient[APP_HTTPCLIENT_AGENT]);
}
debug('restore httpClient, size: %d', httpClients.length);
const agent = _mockAgent;
_mockAgent = null;
await agent.close();
},
};
2 changes: 1 addition & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function call(method) {
if (method === 'app') {
assert(app.logger._mockLogs);
assert(app.coreLogger._mockLogs);
mm.restore();
await mm.restore();
assert(!app.logger._mockLogs);
assert(!app.coreLogger._mockLogs);
}
Expand Down
11 changes: 11 additions & 0 deletions test/mock_httpclient_next_h2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ describe('test/mock_httpclient_next_h2.test.js', () => {
.expect(200);
});

it('should auto restore after each case', async () => {
app.mockCsrf();
await request(server)
.get('/urllib')
.expect({
get: 'url get',
post: 'url post',
})
.expect(200);
});

it('should use first mock data on duplicate url mock', async () => {
app.mockCsrf();
app.mockHttpclient(url, 'post', {
Expand Down

0 comments on commit 088ac41

Please sign in to comment.