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 425aa4f commit d9c5e08
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 71 deletions.
17 changes: 14 additions & 3 deletions src/app_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function main() {
framework: string;
require?: string[];
startMode?: 'process' | 'worker_threads';
port?: number;
port: number;
debugPort?: number;
https?: object;
sticky?: boolean;
Expand Down Expand Up @@ -79,9 +79,11 @@ async function main() {
...clusterConfig.https,
...options.https,
};
const port = options.port = options.port ?? listenConfig.port;
const port = app.options.port = options.port || listenConfig.port;
const debugPort = options.debugPort;
const protocol = (httpsOptions.key && httpsOptions.cert) ? 'https' : 'http';
debug('[app_worker:%s] listenConfig: %j, real port: %o, protocol: %o, debugPort: %o',
process.pid, listenConfig, port, protocol, debugPort);

AppWorker.send({
to: 'master',
Expand Down Expand Up @@ -122,6 +124,7 @@ async function main() {
app.emit('server', server);

if (options.sticky && options.stickyWorkerPort) {
// only allow connection from localhost
server.listen(options.stickyWorkerPort, '127.0.0.1');
// Listen to messages was sent from the master. Ignore everything else.
AppWorker.on('message', (message: string, connection: Socket) => {
Expand Down Expand Up @@ -157,7 +160,15 @@ async function main() {
}

server.once('listening', () => {
const address = server.address() || { port };
let address: any = server.address() || { port };
if (typeof address === 'string') {
// https://nodejs.org/api/cluster.html#cluster_event_listening_1
// Unix domain socket
address = {
address,
addressType: -1,
};
}
debug('[app_worker:%s] listening at %j', process.pid, address);
AppWorker.send({
to: 'master',
Expand Down
5 changes: 4 additions & 1 deletion src/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import util from 'node:util';
import path from 'node:path';
import fs from 'node:fs';
import net from 'node:net';
import { debuglog } from 'node:util';
import { ReadyEventEmitter } from 'get-ready';
import { detectPort } from 'detect-port';
import { reload } from 'cluster-reload';
Expand All @@ -23,6 +24,8 @@ import {
import { AppThreadWorker, AppThreadUtils as WorkerThreadsAppWorker } from './utils/mode/impl/worker_threads/app.js';
import { ClusterWorkerExceptionError } from './error/ClusterWorkerExceptionError.js';

const debug = debuglog('@eggjs/cluster/master');

export interface MasterOptions extends ParsedClusterOptions {
clusterPort?: number;
stickyWorkerPort?: number;
Expand Down Expand Up @@ -455,7 +458,7 @@ export class Master extends ReadyEventEmitter {
address: ListeningAddress;
}) {
const worker = this.workerManager.getWorker(data.workerId)!;
// this.log('[master] got app_worker#%s:%s app-start event, data: %j', worker.id, worker.workerId, data);
debug('got app_worker#%s:%s app-start event, data: %j', worker.id, worker.workerId, data);

const address = data.address;
// worker should listen stickyWorkerPort when sticky mode
Expand Down
2 changes: 1 addition & 1 deletion test/agent_worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('test/agent_worker.test.ts', () => {
action: 'kill-agent',
});

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

app.expect('stderr', /\[master\] agent_worker#1:\d+ died/);
app.expect('stdout', /\[master\] try to start a new agent_worker after 1s .../);
Expand Down
54 changes: 36 additions & 18 deletions test/app_worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ describe('test/app_worker.test.ts', () => {
});

return app
// .debug()
.debug()
.expect('code', 1)
.expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
.expect('stderr', /CustomError: mock error/)
// .expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
.end();
});

Expand All @@ -78,7 +79,8 @@ describe('test/app_worker.test.ts', () => {
return app
// .debug()
.expect('code', 1)
.expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
.expect('stderr', /CustomError: mock error/)
// .expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
.end();
});

Expand Down Expand Up @@ -116,7 +118,7 @@ describe('test/app_worker.test.ts', () => {
.expect(200);

// wait app worker restart
await scheduler.wait(10000);
await scheduler.wait(5000);

app.expect('stdout', /app_worker#1:\d+ disconnect/);
app.expect('stdout', /app_worker#2:\d+ started/);
Expand All @@ -130,21 +132,23 @@ describe('test/app_worker.test.ts', () => {
// app.debug();
return app.ready();
});
after(mm.restore);
after(async () => {
await app.close();
await mm.restore();
});

it('should restart', async () => {
it('should restart disable on local env', async () => {
try {
await app.httpRequest()
.get('/exit');
} catch (_) {
// ignore
}

// wait app worker restart
await scheduler.wait(10000);
await scheduler.wait(1000);

app.expect('stdout', /app_worker#1:\d+ disconnect/);
app.expect('stderr', /don't fork new work/);
app.expect('stderr', /worker:\d+ disconnect/);
app.expect('stderr', /don't fork new work \(refork: false, reforkCount: 0\)/);
});
});

Expand All @@ -166,9 +170,9 @@ describe('test/app_worker.test.ts', () => {
}

// wait app worker restart
await scheduler.wait(10000);
await scheduler.wait(1000);

app.expect('stdout', /app_worker#1:\d+ disconnect/);
app.expect('stderr', /worker:\d+ disconnect/);
app.expect('stderr', /don't fork new work/);
});
});
Expand All @@ -192,20 +196,24 @@ describe('test/app_worker.test.ts', () => {
beforeEach(() => {
mm.env('default');
});
afterEach(mm.restore);
afterEach(async () => {
await app.close();
await mm.restore();
});
afterEach(() => rm(sockFile, { force: true, recursive: true }));

it('should error then port is not specified', async () => {
it('should set default port 170xx then config.listen.port is null', async () => {
app = cluster('apps/app-listen-without-port');
// app.debug();
await app.ready();

app.expect('code', 1);
app.expect('stderr', /port should be number, but got null/);
app.expect('code', 0);
app.expect('stdout', /egg started on http:\/\/127.0.0.1:\d+/);
// app.expect('stderr', /port should be number, but got null/);
});

it('should use port in config', async () => {
app = cluster('apps/app-listen-port');
app = cluster('apps/app-listen-port', { port: 0 });
// app.debug();
await app.ready();

Expand All @@ -231,12 +239,22 @@ describe('test/app_worker.test.ts', () => {
.get('/port')
.expect('17010')
.expect(200);

// ipv6
// await request('http://[::1]:17010')
// .get('/')
// .expect('done')
// .expect(200);
// await request('http://[::1]:17010')
// .get('/port')
// .expect('17010')
// .expect(200);
});

it('should use hostname in config', async () => {
const url = ip() + ':17010';

app = cluster('apps/app-listen-hostname');
app = cluster('apps/app-listen-hostname', { port: 0 });
// app.debug();
await app.ready();

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/apps/app-listen-port/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = app => {
});

app.get('/port', ctx => {
ctx.body = ctx.app._options.port;
ctx.body = ctx.app.options.port;
});
};
2 changes: 0 additions & 2 deletions test/fixtures/apps/app-listen-port/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = {
keys: '123',
cluster: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = {
cluster: {
listen: {
Expand Down
10 changes: 4 additions & 6 deletions test/fixtures/apps/framework/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
const { startCluster } = require('egg');

const egg = require('egg');

module.exports = egg;
module.exports.Application = require('./lib/framework');
module.exports.Agent = require('./lib/agent');
exports.startCluster = startCluster;
exports.Application = require('./lib/framework');
exports.Agent = require('./lib/agent');
5 changes: 1 addition & 4 deletions test/fixtures/apps/framework/lib/agent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const path = require('path');
const egg = require('egg');
const Agent = egg.Agent;
const { Agent } = require('egg');

class FrameworkAgent extends Agent {
get [Symbol.for('egg#eggPath')]() {
Expand Down
6 changes: 2 additions & 4 deletions test/fixtures/apps/framework/lib/framework.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';

const path = require('path');
const egg = require('egg');
const Application = egg.Application;
const AppWorkerLoader = egg.AppWorkerLoader;

class Loader extends AppWorkerLoader {
loadConfig() {
async loadConfig() {
this.loadServerConf();
super.loadConfig();
await super.loadConfig();
}

loadServerConf() {}
Expand Down
Loading

0 comments on commit d9c5e08

Please sign in to comment.