Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 28, 2024
1 parent cf776aa commit 425aa4f
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 265 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '18.19.0, 18, 20, 22'
version: '18.19.0, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/eggjs/cluster#readme",
"dependencies": {
"@eggjs/utils": "^4.0.2",
"@eggjs/utils": "^4.1.6",
"@fengmk2/ps-tree": "^2.0.1",
"cfork": "^2.0.0",
"cluster-reload": "^2.0.0",
Expand All @@ -48,18 +48,18 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/bin": "^7.0.0",
"@eggjs/mock": "beta",
"@eggjs/supertest": "^8.1.1",
"@eggjs/tsconfig": "1",
"@types/mocha": "10",
"@types/node": "22",
"address": "^2.0.3",
"coffee": "^5.5.1",
"egg": "beta",
"egg-bin": "6",
"egg-errors": "^2.2.0",
"egg-mock": "beta",
"eslint": "8",
"eslint-config-egg": "14",
"supertest": "^7.0.0",
"ts-node": "^10.9.1",
"tshy": "3",
"tshy-after": "1",
Expand Down
61 changes: 32 additions & 29 deletions test/agent_worker.test.js → test/agent_worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
const assert = require('assert');
const path = require('path');
const coffee = require('coffee');
const mm = require('egg-mock');
const { readFile } = require('fs/promises');
const { sleep } = require('../lib/utils/timer');
const utils = require('./utils');
import { strict as assert } from 'node:assert';
import { readFile } from 'node:fs/promises';
import { scheduler } from 'node:timers/promises';
import coffee from 'coffee';
import { mm, MockApplication } from '@eggjs/mock';
import { cluster, getFilepath } from './utils.js';

describe('test/agent_worker.test.js', () => {
let app;
describe('test/agent_worker.test.ts', () => {
let app: MockApplication;

afterEach(mm.restore);

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

it('support config agent debug port', () => {
mm(process.env, 'EGG_AGENT_DEBUG_PORT', '15800');
app = utils.cluster('apps/agent-debug-port', { isDebug: true });
app = cluster('apps/agent-debug-port', { isDebug: true } as any);
return app
.expect('stdout', /15800/)
// .debug()
.expect('stdout', /=15800/)
.end();
});

it('agent debug port default 5800', () => {
app = utils.cluster('apps/agent-debug-port', { isDebug: true });
app = cluster('apps/agent-debug-port', { isDebug: true } as any);
return app
.expect('stdout', /5800/)
// .debug()
.expect('stdout', /=5800/)
.end();
});

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

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

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

await sleep(20000);
await scheduler.wait(20000);

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

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

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

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

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

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

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

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

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

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

describe('agent custom loggers', () => {
before(() => {
app = utils.cluster('apps/custom-logger');
app = cluster('apps/custom-logger');
return app.ready();
});
after(() => app.close());

it('should support custom logger in agent', async () => {
await sleep(1500);
await scheduler.wait(1500);
const content = await readFile(
path.join(__dirname, 'fixtures/apps/custom-logger/logs/monitor.log'), 'utf8');
getFilepath('apps/custom-logger/logs/monitor.log'), 'utf8');
assert(content === 'hello monitor!\n');
});
});
Expand Down
Loading

0 comments on commit 425aa4f

Please sign in to comment.