diff --git a/bootstrap.js b/bootstrap.js index 69de6fe..b88bc8d 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -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'); @@ -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); + }, + }); + debug('mockParallelApp app: %s', !!app); } else { app = mock.app(options); if (typeof beforeAll === 'function') { diff --git a/lib/agent.js b/lib/agent.js index f261185..7af776a 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -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(); @@ -12,6 +19,7 @@ exports.setupAgent = async () => { }; exports.closeAgent = async () => { + debug('setupAgent call, agent: %s', !!agent); if (agent) { await agent.close(); } diff --git a/lib/app.js b/lib/app.js index 1b1daa7..ae10ea3 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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'); diff --git a/lib/parallel/agent.js b/lib/parallel/agent.js index 95eaa7e..8c2eafb 100644 --- a/lib/parallel/agent.js +++ b/lib/parallel/agent.js @@ -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'); diff --git a/lib/parallel/app.js b/lib/parallel/app.js index a0f1afb..cc92fae 100644 --- a/lib/parallel/app.js +++ b/lib/parallel/app.js @@ -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'); @@ -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); diff --git a/lib/parallel/util.js b/lib/parallel/util.js index 975c6fa..f0d2e05 100644 --- a/lib/parallel/util.js +++ b/lib/parallel/util.js @@ -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'); diff --git a/register.js b/register.js index 3c8e766..93b3395 100644 --- a/register.js +++ b/register.js @@ -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) { @@ -29,6 +36,7 @@ exports.mochaHooks = { } }, async afterEach() { + debug('mochaHooks.afterEach call, _inited: %s, _app: %s', _inited, !!_app); if (_app) { await _app.backgroundTasksFinished(); }