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: app should wait for agent ready on parallel mode #144

Merged
merged 9 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const debug = require('util').debuglog('egg-mock:bootstrap');
const assert = require('assert');
const path = require('path');
const mock = require('./index').default;
const { setupAgent } = require('./lib/agent');
const mockParallelApp = require('./lib/parallel/app');
const { getEggOptions } = require('./lib/utils');

Expand All @@ -13,8 +13,19 @@ const pkgInfo = require(path.join(options.baseDir || process.cwd(), 'package.jso
if (pkgInfo.eggPlugin) throw new Error('DO NOT USE bootstrap to test plugin');

let app;
debug('env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s',
process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
app = mockParallelApp(options);
// setup agent first
app = mockParallelApp({
...options,
beforeInit: async _app => {
const agent = await setupAgent();
_app.options.clusterPort = agent.options.clusterPort;
debug('mockParallelApp beforeInit get clusterPort: %s', _app.options.clusterPort);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

终于成功了,mocha global hook 有时序问题

},
});
debug('mockParallelApp app: %s', !!app);
} else {
app = mock.app(options);
if (typeof beforeAll === 'function') {
Expand Down
12 changes: 10 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
let agent;

const debug = require('util').debuglog('egg-mock:lib:agent');
const Agent = require('./parallel/agent');
const { getEggOptions } = require('./utils');

let agent;

exports.setupAgent = async () => {
debug('setupAgent call, env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s, agent: %s',
process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT, !!agent);
if (agent) {
await agent.ready();
return agent;
}
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
agent = Agent(getEggOptions());
await agent.ready();
Expand All @@ -12,6 +19,7 @@ exports.setupAgent = async () => {
};

exports.closeAgent = async () => {
debug('setupAgent call, agent: %s', !!agent);
if (agent) {
await agent.close();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require('util').debuglog('egg-mock');
const debug = require('util').debuglog('egg-mock:lib:app');
const os = require('os');
const path = require('path');
const EventEmitter = require('events');
Expand Down
2 changes: 1 addition & 1 deletion lib/parallel/agent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require('util').debuglog('egg-mock');
const debug = require('util').debuglog('egg-mock:lib:parallel:agent');
const path = require('path');
const Base = require('sdk-base');
const detectPort = require('detect-port');
Expand Down
7 changes: 5 additions & 2 deletions lib/parallel/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require('util').debuglog('egg-mock');
const debug = require('util').debuglog('egg-mock:lib:parallel:app');
const Base = require('sdk-base');
const context = require('../context');
const formatOptions = require('../format_options');
Expand Down Expand Up @@ -35,7 +35,10 @@ class MockApplication extends Base {
delete this.options.beforeInit;
}

this.options.clusterPort = process.env.CLUSTER_PORT;
this.options.clusterPort = this.options.clusterPort || process.env.CLUSTER_PORT;
if (!this.options.clusterPort) {
throw new Error('cannot get env.CLUSTER_PORT, parallel run fail');
}
debug('get clusterPort %s', this.options.clusterPort);
const { Application } = require(this.options.framework);

Expand Down
2 changes: 1 addition & 1 deletion lib/parallel/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require('util').debuglog('egg-mock');
const debug = require('util').debuglog('egg-mock:lib:parallel:util');
const ConsoleLogger = require('egg-logger').EggConsoleLogger;
const { getProperty } = require('../utils');

Expand Down
10 changes: 9 additions & 1 deletion register.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
const debug = require('util').debuglog('egg-mock:register');
const mock = require('./index').default;
const agent = require('./lib/agent');

exports.mochaGlobalSetup = async () => {
debug('mochaGlobalSetup, agent.setupAgent() start');
await agent.setupAgent();
debug('mochaGlobalSetup, agent.setupAgent() end');
};

exports.mochaGlobalTeardown = async () => {
debug('mochaGlobalTeardown, agent.closeAgent() start');
await agent.closeAgent();
debug('mochaGlobalTeardown, agent.closeAgent() end');
};

let _inited = false;
let _app;
exports.mochaHooks = {
async beforeAll() {
debug('mochaHooks.beforeAll call, _inited: %s, _app: %s', _inited, !!_app);
if (!_inited) {
_inited = true;
try {
const { app } = require('./bootstrap');
_app = app;
} catch {
} catch (err) {
// ignore require error
// it will throw error on non egg project, e.g.: Error: egg is not found in /foo/bar
debug('require bootstrap app error: %s', err);
return;
}
if (_app) {
Expand All @@ -29,6 +36,7 @@ exports.mochaHooks = {
}
},
async afterEach() {
debug('mochaHooks.afterEach call, _inited: %s, _app: %s', _inited, !!_app);
if (_app) {
await _app.backgroundTasksFinished();
}
Expand Down