Skip to content

Commit 425aa4f

Browse files
committed
fix tests
1 parent cf776aa commit 425aa4f

File tree

14 files changed

+275
-265
lines changed

14 files changed

+275
-265
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
1414
os: 'ubuntu-latest, macos-latest'
15-
version: '18.19.0, 18, 20, 22'
15+
version: '18.19.0, 18, 20, 22, 23'
1616
secrets:
1717
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://github.com/eggjs/cluster#readme",
3636
"dependencies": {
37-
"@eggjs/utils": "^4.0.2",
37+
"@eggjs/utils": "^4.1.6",
3838
"@fengmk2/ps-tree": "^2.0.1",
3939
"cfork": "^2.0.0",
4040
"cluster-reload": "^2.0.0",
@@ -48,18 +48,18 @@
4848
},
4949
"devDependencies": {
5050
"@arethetypeswrong/cli": "^0.17.1",
51+
"@eggjs/bin": "^7.0.0",
52+
"@eggjs/mock": "beta",
53+
"@eggjs/supertest": "^8.1.1",
5154
"@eggjs/tsconfig": "1",
5255
"@types/mocha": "10",
5356
"@types/node": "22",
5457
"address": "^2.0.3",
5558
"coffee": "^5.5.1",
5659
"egg": "beta",
57-
"egg-bin": "6",
5860
"egg-errors": "^2.2.0",
59-
"egg-mock": "beta",
6061
"eslint": "8",
6162
"eslint-config-egg": "14",
62-
"supertest": "^7.0.0",
6363
"ts-node": "^10.9.1",
6464
"tshy": "3",
6565
"tshy-after": "1",
Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
const assert = require('assert');
2-
const path = require('path');
3-
const coffee = require('coffee');
4-
const mm = require('egg-mock');
5-
const { readFile } = require('fs/promises');
6-
const { sleep } = require('../lib/utils/timer');
7-
const utils = require('./utils');
1+
import { strict as assert } from 'node:assert';
2+
import { readFile } from 'node:fs/promises';
3+
import { scheduler } from 'node:timers/promises';
4+
import coffee from 'coffee';
5+
import { mm, MockApplication } from '@eggjs/mock';
6+
import { cluster, getFilepath } from './utils.js';
87

9-
describe('test/agent_worker.test.js', () => {
10-
let app;
8+
describe('test/agent_worker.test.ts', () => {
9+
let app: MockApplication;
10+
11+
afterEach(mm.restore);
1112

1213
describe('Fork Agent', () => {
1314
afterEach(() => app && app.close());
1415

1516
it('support config agent debug port', () => {
1617
mm(process.env, 'EGG_AGENT_DEBUG_PORT', '15800');
17-
app = utils.cluster('apps/agent-debug-port', { isDebug: true });
18+
app = cluster('apps/agent-debug-port', { isDebug: true } as any);
1819
return app
19-
.expect('stdout', /15800/)
20+
// .debug()
21+
.expect('stdout', /=15800/)
2022
.end();
2123
});
2224

2325
it('agent debug port default 5800', () => {
24-
app = utils.cluster('apps/agent-debug-port', { isDebug: true });
26+
app = cluster('apps/agent-debug-port', { isDebug: true } as any);
2527
return app
26-
.expect('stdout', /5800/)
28+
// .debug()
29+
.expect('stdout', /=5800/)
2730
.end();
2831
});
2932

3033
it('should exist when error happened during boot', () => {
31-
app = utils.cluster('apps/agent-die-onboot');
34+
app = cluster('apps/agent-die-onboot');
3235
return app
3336
// .debug()
3437
.expect('code', 1)
@@ -38,7 +41,7 @@ describe('test/agent_worker.test.js', () => {
3841
});
3942

4043
it('should not start app when error happened during agent starting', () => {
41-
app = utils.cluster('apps/agent-die-onboot');
44+
app = cluster('apps/agent-die-onboot');
4245
return app
4346
.expect('code', 1)
4447
.expect('stderr', /\[master\] agent_worker#1:\d+ start fail, exiting with code:1/)
@@ -48,7 +51,7 @@ describe('test/agent_worker.test.js', () => {
4851
});
4952

5053
it('should refork new agent_worker after app started', async () => {
51-
app = utils.cluster('apps/agent-die');
54+
app = cluster('apps/agent-die');
5255
await app
5356
// .debug()
5457
.expect('stdout', /\[master\] egg started on http:\/\/127.0.0.1:\d+/)
@@ -59,7 +62,7 @@ describe('test/agent_worker.test.js', () => {
5962
action: 'kill-agent',
6063
});
6164

62-
await sleep(20000);
65+
await scheduler.wait(20000);
6366

6467
app.expect('stderr', /\[master\] agent_worker#1:\d+ died/);
6568
app.expect('stdout', /\[master\] try to start a new agent_worker after 1s .../);
@@ -68,22 +71,22 @@ describe('test/agent_worker.test.js', () => {
6871
});
6972

7073
it('should exit agent_worker when master die in accident', async () => {
71-
app = utils.cluster('apps/agent-die');
74+
app = cluster('apps/agent-die');
7275
await app
7376
// .debug()
7477
.expect('stdout', /\[master\] egg started on http:\/\/127.0.0.1:\d+/)
7578
.end();
7679

7780
// kill -9 master
7881
app.process.kill('SIGKILL');
79-
await sleep(5000);
82+
await scheduler.wait(5000);
8083
app.expect('stderr', /\[app_worker\] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false/)
8184
.expect('stderr', /\[agent_worker\] receive disconnect event on child_process fork mode, exiting with code:110/)
8285
.expect('stderr', /\[agent_worker\] exit with code:110/);
8386
});
8487

8588
it('should master exit when agent exit during app worker boot', () => {
86-
app = utils.cluster('apps/agent-die-on-forkapp');
89+
app = cluster('apps/agent-die-on-forkapp');
8790

8891
return app
8992
// .debug()
@@ -97,7 +100,7 @@ describe('test/agent_worker.test.js', () => {
97100
});
98101

99102
it('should exit when emit error during agent worker boot', () => {
100-
app = utils.cluster('apps/agent-start-error');
103+
app = cluster('apps/agent-start-error');
101104
return app
102105
// .debug()
103106
.expect('code', 1)
@@ -108,7 +111,7 @@ describe('test/agent_worker.test.js', () => {
108111
});
109112

110113
it('should FrameworkErrorformater work during agent boot', () => {
111-
app = utils.cluster('apps/agent-start-framework-error');
114+
app = cluster('apps/agent-start-framework-error');
112115
return app
113116
// .debug()
114117
.expect('code', 1)
@@ -117,7 +120,7 @@ describe('test/agent_worker.test.js', () => {
117120
});
118121

119122
it('should FrameworkErrorformater work during agent boot ready', () => {
120-
app = utils.cluster('apps/agent-start-framework-ready-error');
123+
app = cluster('apps/agent-start-framework-ready-error');
121124
return app
122125
// .debug()
123126
.expect('code', 1)
@@ -127,11 +130,11 @@ describe('test/agent_worker.test.js', () => {
127130

128131
// process.send is not exist if started by spawn
129132
it('master should not die if spawn error', async () => {
130-
app = coffee.spawn('node', [ utils.getFilepath('apps/agent-die/start.js') ]);
133+
app = coffee.spawn('node', [ getFilepath('apps/agent-die/start.js') ]) as any;
131134
// app.debug();
132-
app.close = () => app.proc.kill();
135+
app.close = async () => app.proc.kill();
133136

134-
await sleep(3000);
137+
await scheduler.wait(3000);
135138
app.emit('close', 0);
136139
app.expect('stderr', /Error: Cannot find module/);
137140
app.notExpect('stderr', /TypeError: process.send is not a function/);
@@ -140,15 +143,15 @@ describe('test/agent_worker.test.js', () => {
140143

141144
describe('agent custom loggers', () => {
142145
before(() => {
143-
app = utils.cluster('apps/custom-logger');
146+
app = cluster('apps/custom-logger');
144147
return app.ready();
145148
});
146149
after(() => app.close());
147150

148151
it('should support custom logger in agent', async () => {
149-
await sleep(1500);
152+
await scheduler.wait(1500);
150153
const content = await readFile(
151-
path.join(__dirname, 'fixtures/apps/custom-logger/logs/monitor.log'), 'utf8');
154+
getFilepath('apps/custom-logger/logs/monitor.log'), 'utf8');
152155
assert(content === 'hello monitor!\n');
153156
});
154157
});

0 commit comments

Comments
 (0)