Skip to content

Commit d9c5e08

Browse files
committed
fix tests
1 parent 425aa4f commit d9c5e08

File tree

12 files changed

+83
-71
lines changed

12 files changed

+83
-71
lines changed

src/app_worker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function main() {
1717
framework: string;
1818
require?: string[];
1919
startMode?: 'process' | 'worker_threads';
20-
port?: number;
20+
port: number;
2121
debugPort?: number;
2222
https?: object;
2323
sticky?: boolean;
@@ -79,9 +79,11 @@ async function main() {
7979
...clusterConfig.https,
8080
...options.https,
8181
};
82-
const port = options.port = options.port ?? listenConfig.port;
82+
const port = app.options.port = options.port || listenConfig.port;
8383
const debugPort = options.debugPort;
8484
const protocol = (httpsOptions.key && httpsOptions.cert) ? 'https' : 'http';
85+
debug('[app_worker:%s] listenConfig: %j, real port: %o, protocol: %o, debugPort: %o',
86+
process.pid, listenConfig, port, protocol, debugPort);
8587

8688
AppWorker.send({
8789
to: 'master',
@@ -122,6 +124,7 @@ async function main() {
122124
app.emit('server', server);
123125

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

159162
server.once('listening', () => {
160-
const address = server.address() || { port };
163+
let address: any = server.address() || { port };
164+
if (typeof address === 'string') {
165+
// https://nodejs.org/api/cluster.html#cluster_event_listening_1
166+
// Unix domain socket
167+
address = {
168+
address,
169+
addressType: -1,
170+
};
171+
}
161172
debug('[app_worker:%s] listening at %j', process.pid, address);
162173
AppWorker.send({
163174
to: 'master',

src/master.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import util from 'node:util';
44
import path from 'node:path';
55
import fs from 'node:fs';
66
import net from 'node:net';
7+
import { debuglog } from 'node:util';
78
import { ReadyEventEmitter } from 'get-ready';
89
import { detectPort } from 'detect-port';
910
import { reload } from 'cluster-reload';
@@ -23,6 +24,8 @@ import {
2324
import { AppThreadWorker, AppThreadUtils as WorkerThreadsAppWorker } from './utils/mode/impl/worker_threads/app.js';
2425
import { ClusterWorkerExceptionError } from './error/ClusterWorkerExceptionError.js';
2526

27+
const debug = debuglog('@eggjs/cluster/master');
28+
2629
export interface MasterOptions extends ParsedClusterOptions {
2730
clusterPort?: number;
2831
stickyWorkerPort?: number;
@@ -455,7 +458,7 @@ export class Master extends ReadyEventEmitter {
455458
address: ListeningAddress;
456459
}) {
457460
const worker = this.workerManager.getWorker(data.workerId)!;
458-
// this.log('[master] got app_worker#%s:%s app-start event, data: %j', worker.id, worker.workerId, data);
461+
debug('got app_worker#%s:%s app-start event, data: %j', worker.id, worker.workerId, data);
459462

460463
const address = data.address;
461464
// worker should listen stickyWorkerPort when sticky mode

test/agent_worker.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('test/agent_worker.test.ts', () => {
6262
action: 'kill-agent',
6363
});
6464

65-
await scheduler.wait(20000);
65+
await scheduler.wait(5000);
6666

6767
app.expect('stderr', /\[master\] agent_worker#1:\d+ died/);
6868
app.expect('stdout', /\[master\] try to start a new agent_worker after 1s .../);

test/app_worker.test.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ describe('test/app_worker.test.ts', () => {
6060
});
6161

6262
return app
63-
// .debug()
63+
.debug()
6464
.expect('code', 1)
65-
.expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
65+
.expect('stderr', /CustomError: mock error/)
66+
// .expect('stderr', /CustomError: mock error \[ https\:\/\/eggjs\.org\/zh-cn\/faq\/customPlugin_99 \]/)
6667
.end();
6768
});
6869

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

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

118120
// wait app worker restart
119-
await scheduler.wait(10000);
121+
await scheduler.wait(5000);
120122

121123
app.expect('stdout', /app_worker#1:\d+ disconnect/);
122124
app.expect('stdout', /app_worker#2:\d+ started/);
@@ -130,21 +132,23 @@ describe('test/app_worker.test.ts', () => {
130132
// app.debug();
131133
return app.ready();
132134
});
133-
after(mm.restore);
135+
after(async () => {
136+
await app.close();
137+
await mm.restore();
138+
});
134139

135-
it('should restart', async () => {
140+
it('should restart disable on local env', async () => {
136141
try {
137142
await app.httpRequest()
138143
.get('/exit');
139144
} catch (_) {
140145
// ignore
141146
}
142147

143-
// wait app worker restart
144-
await scheduler.wait(10000);
148+
await scheduler.wait(1000);
145149

146-
app.expect('stdout', /app_worker#1:\d+ disconnect/);
147-
app.expect('stderr', /don't fork new work/);
150+
app.expect('stderr', /worker:\d+ disconnect/);
151+
app.expect('stderr', /don't fork new work \(refork: false, reforkCount: 0\)/);
148152
});
149153
});
150154

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

168172
// wait app worker restart
169-
await scheduler.wait(10000);
173+
await scheduler.wait(1000);
170174

171-
app.expect('stdout', /app_worker#1:\d+ disconnect/);
175+
app.expect('stderr', /worker:\d+ disconnect/);
172176
app.expect('stderr', /don't fork new work/);
173177
});
174178
});
@@ -192,20 +196,24 @@ describe('test/app_worker.test.ts', () => {
192196
beforeEach(() => {
193197
mm.env('default');
194198
});
195-
afterEach(mm.restore);
199+
afterEach(async () => {
200+
await app.close();
201+
await mm.restore();
202+
});
196203
afterEach(() => rm(sockFile, { force: true, recursive: true }));
197204

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

203-
app.expect('code', 1);
204-
app.expect('stderr', /port should be number, but got null/);
210+
app.expect('code', 0);
211+
app.expect('stdout', /egg started on http:\/\/127.0.0.1:\d+/);
212+
// app.expect('stderr', /port should be number, but got null/);
205213
});
206214

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

@@ -231,12 +239,22 @@ describe('test/app_worker.test.ts', () => {
231239
.get('/port')
232240
.expect('17010')
233241
.expect(200);
242+
243+
// ipv6
244+
// await request('http://[::1]:17010')
245+
// .get('/')
246+
// .expect('done')
247+
// .expect(200);
248+
// await request('http://[::1]:17010')
249+
// .get('/port')
250+
// .expect('17010')
251+
// .expect(200);
234252
});
235253

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

239-
app = cluster('apps/app-listen-hostname');
257+
app = cluster('apps/app-listen-hostname', { port: 0 });
240258
// app.debug();
241259
await app.ready();
242260

test/fixtures/apps/app-listen-port/app/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = app => {
44
});
55

66
app.get('/port', ctx => {
7-
ctx.body = ctx.app._options.port;
7+
ctx.body = ctx.app.options.port;
88
});
99
};

test/fixtures/apps/app-listen-port/config/config.default.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
module.exports = {
42
keys: '123',
53
cluster: {

test/fixtures/apps/app-listen-without-port/config/config.default.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
module.exports = {
42
cluster: {
53
listen: {
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
1+
const { startCluster } = require('egg');
22

3-
const egg = require('egg');
4-
5-
module.exports = egg;
6-
module.exports.Application = require('./lib/framework');
7-
module.exports.Agent = require('./lib/agent');
3+
exports.startCluster = startCluster;
4+
exports.Application = require('./lib/framework');
5+
exports.Agent = require('./lib/agent');

test/fixtures/apps/framework/lib/agent.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
'use strict';
2-
31
const path = require('path');
4-
const egg = require('egg');
5-
const Agent = egg.Agent;
2+
const { Agent } = require('egg');
63

74
class FrameworkAgent extends Agent {
85
get [Symbol.for('egg#eggPath')]() {

test/fixtures/apps/framework/lib/framework.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict';
2-
31
const path = require('path');
42
const egg = require('egg');
53
const Application = egg.Application;
64
const AppWorkerLoader = egg.AppWorkerLoader;
75

86
class Loader extends AppWorkerLoader {
9-
loadConfig() {
7+
async loadConfig() {
108
this.loadServerConf();
11-
super.loadConfig();
9+
await super.loadConfig();
1210
}
1311

1412
loadServerConf() {}

0 commit comments

Comments
 (0)