Skip to content

Commit 6adebea

Browse files
committed
add loader tests
1 parent 0dc6ead commit 6adebea

23 files changed

Lines changed: 350 additions & 225 deletions

src/lib/core/base_hook_class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import type { ILifecycleBoot } from '@eggjs/core';
33
import type { Application, Agent } from '../../index.js';
44

55
export class BaseHookClass implements ILifecycleBoot {
6-
fullPath?: string;
6+
declare fullPath?: string;
77
#instance: Application | Agent;
88

99
constructor(instance: Application | Agent) {
1010
this.#instance = instance;
1111
}
1212

13-
get logger(): any {
13+
get logger() {
1414
return this.#instance.logger;
1515
}
1616

src/lib/egg.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export class EggApplicationCore extends EggCore {
134134
/**
135135
* Retrieve base boot
136136
* @member {Boot}
137+
* @alias BaseHookClass
137138
*/
138139
Boot = BaseHookClass;
139140

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import assert from 'node:assert';
2+
import { scheduler } from 'node:timers/promises';
3+
import { Boot } from '../../../../src/index.js';
4+
5+
export default class CustomBoot extends Boot {
6+
constructor(agent) {
7+
super(agent);
8+
agent.bootLog = [];
9+
assert(this.config);
10+
agent.messenger.on('egg-ready', () => {
11+
agent.messenger.sendToApp('agent2app');
12+
});
13+
}
14+
15+
configDidLoad() {
16+
this.agent.bootLog.push('configDidLoad');
17+
}
18+
19+
async didLoad() {
20+
await scheduler.wait(1);
21+
this.agent.bootLog.push('didLoad');
22+
}
23+
24+
async willReady() {
25+
await scheduler.wait(1);
26+
this.agent.bootLog.push('willReady');
27+
}
28+
29+
async didReady() {
30+
await scheduler.wait(1);
31+
this.agent.bootLog.push('didReady');
32+
this.logger.info('agent is ready');
33+
}
34+
35+
async beforeClose() {
36+
await scheduler.wait(1);
37+
this.agent.bootLog.push('beforeClose');
38+
}
39+
40+
async serverDidReady() {
41+
await scheduler.wait(1);
42+
this.agent.bootLog.push('serverDidReady');
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from 'node:assert';
2+
import { scheduler } from 'node:timers/promises';
3+
import { Boot } from '../../../../src/index.js';
4+
5+
export default class CustomBoot extends Boot {
6+
constructor(app) {
7+
super(app);
8+
app.bootLog = [];
9+
assert(this.config);
10+
assert(this.fullPath);
11+
app.messenger.on('agent2app', () => {
12+
app.messengerLog = true;
13+
});
14+
}
15+
16+
configDidLoad() {
17+
this.app.bootLog.push('configDidLoad');
18+
}
19+
20+
async didLoad() {
21+
await scheduler.wait(1);
22+
this.app.bootLog.push('didLoad');
23+
}
24+
25+
async willReady() {
26+
await scheduler.wait(1);
27+
this.app.bootLog.push('willReady');
28+
}
29+
30+
async didReady() {
31+
await scheduler.wait(1);
32+
this.app.bootLog.push('didReady');
33+
this.logger.info('app is ready');
34+
}
35+
36+
async beforeClose() {
37+
await scheduler.wait(1);
38+
this.app.bootLog.push('beforeClose');
39+
}
40+
41+
async serverDidReady() {
42+
await scheduler.wait(1);
43+
this.app.bootLog.push('serverDidReady');
44+
}
45+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "boot-app-esm",
3+
"type": "module"
4+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const assert = require('assert');
2-
const BaseHookClass = require('../../../../lib/core/base_hook_class');
3-
const { sleep } = require('../../../utils');
2+
const { scheduler } = require('node:timers/promises');
43

5-
module.exports = class extends BaseHookClass {
4+
module.exports = class CustomBoot {
65
constructor(agent) {
7-
super(agent);
6+
this.agent = agent;
87
agent.bootLog = [];
9-
assert(this.config);
8+
assert(this.agent.config);
109
agent.messenger.on('egg-ready', () => {
1110
agent.messenger.sendToApp('agent2app');
1211
});
@@ -17,28 +16,28 @@ module.exports = class extends BaseHookClass {
1716
}
1817

1918
async didLoad() {
20-
await sleep(1);
19+
await scheduler.wait(1);
2120
this.agent.bootLog.push('didLoad');
2221
}
2322

2423
async willReady() {
25-
await sleep(1);
24+
await scheduler.wait(1);
2625
this.agent.bootLog.push('willReady');
2726
}
2827

2928
async didReady() {
30-
await sleep(1);
29+
await scheduler.wait(1);
3130
this.agent.bootLog.push('didReady');
32-
this.logger.info('agent is ready');
31+
this.agent.logger.info('agent is ready');
3332
}
3433

3534
async beforeClose() {
36-
await sleep(1);
35+
await scheduler.wait(1);
3736
this.agent.bootLog.push('beforeClose');
3837
}
3938

4039
async serverDidReady() {
41-
await sleep(1);
40+
await scheduler.wait(1);
4241
this.agent.bootLog.push('serverDidReady');
4342
}
4443
};

test/fixtures/apps/boot-app/app.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const assert = require('assert');
2-
const BaseHookClass = require('../../../../lib/core/base_hook_class');
3-
const { sleep } = require('../../../utils');
2+
const { scheduler } = require('node:timers/promises');
43

5-
module.exports = class extends BaseHookClass {
4+
module.exports = class CustomBoot {
65
constructor(app) {
7-
super(app);
6+
this.app = app;
87
app.bootLog = [];
9-
assert(this.config);
8+
assert(this.app.config);
109
app.messenger.on('agent2app', () => {
1110
app.messengerLog = true;
1211
});
@@ -17,28 +16,28 @@ module.exports = class extends BaseHookClass {
1716
}
1817

1918
async didLoad() {
20-
await sleep(1);
19+
await scheduler.wait(1);
2120
this.app.bootLog.push('didLoad');
2221
}
2322

2423
async willReady() {
25-
await sleep(1);
24+
await scheduler.wait(1);
2625
this.app.bootLog.push('willReady');
2726
}
2827

2928
async didReady() {
30-
await sleep(1);
29+
await scheduler.wait(1);
3130
this.app.bootLog.push('didReady');
32-
this.logger.info('app is ready');
31+
this.app.logger.info('app is ready');
3332
}
3433

3534
async beforeClose() {
36-
await sleep(1);
35+
await scheduler.wait(1);
3736
this.app.bootLog.push('beforeClose');
3837
}
3938

4039
async serverDidReady() {
41-
await sleep(1);
40+
await scheduler.wait(1);
4241
this.app.bootLog.push('serverDidReady');
4342
}
4443
};

test/fixtures/apps/demo/config/config.default.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ exports.mysql = {
2020
};
2121

2222
exports.logger = {
23-
consoleLevel: 'NONE',
24-
coreLogger: {
25-
consoleLevel: 'NONE',
26-
},
27-
concentrateError: 'ignore',
28-
level: 'NONE',
23+
// consoleLevel: 'NONE',
24+
// coreLogger: {
25+
// consoleLevel: 'NONE',
26+
// },
27+
// concentrateError: 'ignore',
28+
// level: 'NONE',
2929
};
3030

3131
exports.tips = 'hello egg';
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
exports.logger = {
2-
consoleLevel: 'NONE',
3-
coreLogger: {
4-
consoleLevel: 'NONE',
5-
},
6-
concentrateError: 'ignore',
7-
level: 'NONE',
2+
// consoleLevel: 'NONE',
3+
// coreLogger: {
4+
// consoleLevel: 'NONE',
5+
// },
6+
// concentrateError: 'ignore',
7+
// level: 'NONE',
88
};

test/fixtures/apps/subdir-services/app/service/foo/subdir2/sub2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = app => {
66
super(ctx);
77
}
88

9-
* get(name) {
9+
async get(name) {
1010
return {
1111
name: name,
1212
bar: 'bar3',

0 commit comments

Comments
 (0)