From 8902152437fee0ba7ba41b4fa195a76e595e7a32 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 01:22:00 +0800 Subject: [PATCH 1/9] fix: debug beta --- lib/app.js | 2 +- lib/parallel/agent.js | 4 ++-- lib/parallel/app.js | 7 +++++-- lib/parallel/util.js | 2 +- package.json | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) 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..d99b04e 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'); @@ -44,7 +44,7 @@ class MockAgent extends Base { } } - this.options.clusterPort = process.env.CLUSTER_PORT = await detectPort(); + this.options.clusterPort = process.env.EGG_MOCK_CLUSTER_PORT = await detectPort(); debug('get clusterPort %s', this.options.clusterPort); const { Agent } = require(this.options.framework); diff --git a/lib/parallel/app.js b/lib/parallel/app.js index a0f1afb..6aadb46 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 = process.env.EGG_MOCK_CLUSTER_PORT; + if (!this.options.clusterPort) { + throw new Error('cannot get env.EGG_MOCK_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/package.json b/package.json index e4c7cc2..c7361a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2", + "version": "5.8.2-beta.3", "eggPlugin": { "name": "egg-mock" }, From e86401e58b2a29f62c0dd7a5965a46d84f759642 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 01:34:56 +0800 Subject: [PATCH 2/9] f --- bootstrap.js | 6 ++++-- lib/agent.js | 8 ++++++-- register.js | 10 +++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 69de6fe..7d77f28 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -1,5 +1,4 @@ -'use strict'; - +const debug = require('util').debuglog('egg-mock:bootstrap'); const assert = require('assert'); const path = require('path'); const mock = require('./index').default; @@ -13,8 +12,11 @@ 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); + 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..8e5f1d9 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -1,9 +1,12 @@ -let agent; - +const debug = require('util').debuglog('egg-mock:bootstrap'); 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', + process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT); if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) { agent = Agent(getEggOptions()); await agent.ready(); @@ -12,6 +15,7 @@ exports.setupAgent = async () => { }; exports.closeAgent = async () => { + debug('setupAgent call, agent: %s', !!agent); if (agent) { await agent.close(); } 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(); } From 9ffae4038e11a1b4c64e3f0785a3770a1dc77f14 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 01:44:17 +0800 Subject: [PATCH 3/9] f --- bootstrap.js | 3 +++ lib/agent.js | 7 ++++--- package.json | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 7d77f28..0b81e78 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -2,6 +2,7 @@ const debug = require('util').debuglog('egg-mock:bootstrap'); const assert = require('assert'); const path = require('path'); const mock = require('./index').default; +const agent = require('./lib/agent'); const mockParallelApp = require('./lib/parallel/app'); const { getEggOptions } = require('./lib/utils'); @@ -15,6 +16,8 @@ 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) { + // setup agent first + agent.setupAgent(); app = mockParallelApp(options); debug('mockParallelApp app: %s', !!app); } else { diff --git a/lib/agent.js b/lib/agent.js index 8e5f1d9..87a3e76 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -1,12 +1,13 @@ -const debug = require('util').debuglog('egg-mock:bootstrap'); +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', - process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT); + 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) return; if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) { agent = Agent(getEggOptions()); await agent.ready(); diff --git a/package.json b/package.json index c7361a9..f06a4b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2-beta.3", + "version": "5.8.2-beta.5", "eggPlugin": { "name": "egg-mock" }, From 8b4f72ce92d34f82e466939fe368a1fe8423f4d9 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:05:31 +0800 Subject: [PATCH 4/9] f --- bootstrap.js | 12 +++++++++--- lib/parallel/app.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 0b81e78..b88bc8d 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -2,7 +2,7 @@ const debug = require('util').debuglog('egg-mock:bootstrap'); const assert = require('assert'); const path = require('path'); const mock = require('./index').default; -const agent = require('./lib/agent'); +const { setupAgent } = require('./lib/agent'); const mockParallelApp = require('./lib/parallel/app'); const { getEggOptions } = require('./lib/utils'); @@ -17,8 +17,14 @@ 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) { // setup agent first - agent.setupAgent(); - app = mockParallelApp(options); + 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); diff --git a/lib/parallel/app.js b/lib/parallel/app.js index 6aadb46..869c20b 100644 --- a/lib/parallel/app.js +++ b/lib/parallel/app.js @@ -35,7 +35,7 @@ class MockApplication extends Base { delete this.options.beforeInit; } - this.options.clusterPort = process.env.EGG_MOCK_CLUSTER_PORT; + this.options.clusterPort = this.options.clusterPort || process.env.EGG_MOCK_CLUSTER_PORT; if (!this.options.clusterPort) { throw new Error('cannot get env.EGG_MOCK_CLUSTER_PORT, parallel run fail'); } From 258b94ef9ef21bc0e0102537ab0b76636cfcd490 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:09:24 +0800 Subject: [PATCH 5/9] f --- lib/agent.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index 87a3e76..87faa56 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -7,7 +7,7 @@ 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) return; + if (agent) return agent; if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) { agent = Agent(getEggOptions()); await agent.ready(); diff --git a/package.json b/package.json index f06a4b4..eca4f86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2-beta.5", + "version": "5.8.2-beta.6", "eggPlugin": { "name": "egg-mock" }, From 1402a525d1fc9b5c69d53b46cc024297b5e13c70 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:09:38 +0800 Subject: [PATCH 6/9] f --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eca4f86..a30d79f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2-beta.6", + "version": "5.8.2-beta.7", "eggPlugin": { "name": "egg-mock" }, From 873e1062b0aec0ed8159e676974e8f3097ab1653 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:15:45 +0800 Subject: [PATCH 7/9] f --- lib/agent.js | 5 ++++- package.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/agent.js b/lib/agent.js index 87faa56..7af776a 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -7,7 +7,10 @@ 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) return agent; + if (agent) { + await agent.ready(); + return agent; + } if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) { agent = Agent(getEggOptions()); await agent.ready(); diff --git a/package.json b/package.json index a30d79f..7f74cc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2-beta.7", + "version": "5.8.2-beta.8", "eggPlugin": { "name": "egg-mock" }, From 7f52cd01ec3522b407029c4df099283079597df2 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:22:09 +0800 Subject: [PATCH 8/9] f --- lib/parallel/agent.js | 2 +- lib/parallel/app.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/parallel/agent.js b/lib/parallel/agent.js index d99b04e..8c2eafb 100644 --- a/lib/parallel/agent.js +++ b/lib/parallel/agent.js @@ -44,7 +44,7 @@ class MockAgent extends Base { } } - this.options.clusterPort = process.env.EGG_MOCK_CLUSTER_PORT = await detectPort(); + this.options.clusterPort = process.env.CLUSTER_PORT = await detectPort(); debug('get clusterPort %s', this.options.clusterPort); const { Agent } = require(this.options.framework); diff --git a/lib/parallel/app.js b/lib/parallel/app.js index 869c20b..cc92fae 100644 --- a/lib/parallel/app.js +++ b/lib/parallel/app.js @@ -35,9 +35,9 @@ class MockApplication extends Base { delete this.options.beforeInit; } - this.options.clusterPort = this.options.clusterPort || process.env.EGG_MOCK_CLUSTER_PORT; + this.options.clusterPort = this.options.clusterPort || process.env.CLUSTER_PORT; if (!this.options.clusterPort) { - throw new Error('cannot get env.EGG_MOCK_CLUSTER_PORT, parallel run fail'); + throw new Error('cannot get env.CLUSTER_PORT, parallel run fail'); } debug('get clusterPort %s', this.options.clusterPort); const { Application } = require(this.options.framework); From 7eb3d421da1f596a5d1cf42e832c183a2d1940d4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 11 Jan 2023 02:22:40 +0800 Subject: [PATCH 9/9] f --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7f74cc9..e4c7cc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-mock", - "version": "5.8.2-beta.8", + "version": "5.8.2", "eggPlugin": { "name": "egg-mock" },