Skip to content

Commit 5711f2e

Browse files
committed
make agent extend work
1 parent 7e3edc6 commit 5711f2e

13 files changed

+97
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import { restore, createApp, MockApplication } from '../../utils.js';
23

3-
const assert = require('assert');
4-
5-
const mm = require('egg-mock');
6-
const utils = require('../../utils');
7-
8-
describe('test/app/extend/agent.test.js', () => {
9-
afterEach(mm.restore);
4+
describe('test/app/extend/agent.test.ts', () => {
5+
afterEach(restore);
106

117
describe('agent.addSingleton()', () => {
12-
let app;
8+
let app: MockApplication;
139
before(() => {
14-
app = utils.app('apps/singleton-demo');
10+
app = createApp('apps/singleton-demo');
1511
return app.ready();
1612
});
1713
after(() => app.close());
@@ -21,28 +17,28 @@ describe('test/app/extend/agent.test.js', () => {
2117
assert(config.foo === 'bar');
2218
assert(config.foo2 === 'bar2');
2319

24-
const ds = app.agent.dataService.createInstance({ foo: 'barrr' });
20+
const ds = app.agent.dataService.createInstance({ foo: 'bar2' });
2521
config = await ds.getConfig();
26-
assert(config.foo === 'barrr');
22+
assert(config.foo === 'bar2');
2723

28-
const ds2 = await app.agent.dataService.createInstanceAsync({ foo: 'barrr' });
24+
const ds2 = await app.agent.dataService.createInstanceAsync({ foo: 'bar2' });
2925
config = await ds2.getConfig();
30-
assert(config.foo === 'barrr');
26+
assert(config.foo === 'bar2');
3127

3228
config = await app.agent.dataServiceAsync.get('second').getConfig();
3329
assert(config.foo === 'bar');
3430
assert(config.foo2 === 'bar2');
3531

3632
try {
37-
app.agent.dataServiceAsync.createInstance({ foo: 'barrr' });
33+
app.agent.dataServiceAsync.createInstance({ foo: 'bar2' });
3834
throw new Error('should not execute');
39-
} catch (err) {
35+
} catch (err: any) {
4036
assert(err.message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync');
4137
}
4238

43-
const ds4 = await app.agent.dataServiceAsync.createInstanceAsync({ foo: 'barrr' });
39+
const ds4 = await app.agent.dataServiceAsync.createInstanceAsync({ foo: 'bar2' });
4440
config = await ds4.getConfig();
45-
assert(config.foo === 'barrr');
41+
assert(config.foo === 'bar2');
4642
});
4743
});
4844
});

test/app/extend/application.test.js test/app/extend/application.test.ts

+47-46
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

2-
const assert = require('assert');
3-
const fs = require('fs');
4-
const path = require('path');
5-
const utils = require('../../utils');
2+
import { strict as assert } from 'node:assert';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import { scheduler } from 'node:timers/promises';
6+
import { createApp, MockApplication, cluster } from '../../utils.js';
67

7-
describe('test/app/extend/application.test.js', () => {
8+
describe('test/app/extend/application.test.ts', () => {
89
describe('app.logger', () => {
9-
let app;
10+
let app: MockApplication;
1011
before(() => {
11-
app = utils.app('apps/demo');
12+
app = createApp('apps/demo');
1213
return app.ready();
1314
});
1415
after(() => app.close());
@@ -17,23 +18,23 @@ describe('test/app/extend/application.test.js', () => {
1718
assert(app.logger === app.loggers.logger);
1819
});
1920

20-
it('should alias app.coreLooger => app.loggers.coreLooger', () => {
21+
it('should alias app.coreLogger => app.loggers.coreLogger', () => {
2122
assert(app.coreLogger === app.loggers.coreLogger);
2223
});
2324

24-
it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLooger', () => {
25+
it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLogger', () => {
2526
assert(app.getLogger('coreLogger') === app.loggers.coreLogger);
2627
});
2728

28-
it('should alias app.getLogger(\'noexist\') => null', () => {
29-
assert(app.getLogger('noexist') === null);
29+
it('should alias app.getLogger(\'noExist\') => null', () => {
30+
assert(app.getLogger('noExist') === null);
3031
});
3132
});
3233

3334
describe('app.inspect()', () => {
34-
let app;
35+
let app: MockApplication;
3536
before(() => {
36-
app = utils.app('apps/demo');
37+
app = createApp('apps/demo');
3738
return app.ready();
3839
});
3940
after(() => app.close());
@@ -50,22 +51,22 @@ describe('test/app/extend/application.test.js', () => {
5051
});
5152

5253
describe('app.readyCallback()', () => {
53-
let app;
54+
let app: MockApplication;
5455
after(() => app.close());
5556

5657
it('should log info when plugin is not ready', async () => {
57-
app = utils.cluster('apps/notready');
58+
app = cluster('apps/notready');
5859
// it won't be ready, so wait for the timeout
59-
await utils.sleep(11000);
60+
await scheduler.wait(11000);
6061

6162
app.expect('stderr', /\[egg:core:ready_timeout] 10 seconds later a was still unable to finish./);
6263
});
6364
});
6465

6566
describe('app.locals', () => {
66-
let app;
67+
let app: MockApplication;
6768
before(() => {
68-
app = utils.app('apps/locals');
69+
app = createApp('apps/locals');
6970
return app.ready();
7071
});
7172
after(() => app.close());
@@ -84,15 +85,15 @@ describe('test/app/extend/application.test.js', () => {
8485
});
8586

8687
describe('app.locals.foo = bar', () => {
87-
let app;
88+
let app: MockApplication;
8889
before(() => {
89-
app = utils.app('apps/app-locals-getter');
90+
app = createApp('apps/app-locals-getter');
9091
return app.ready();
9192
});
9293
after(() => app.close());
9394

94-
it('should work', () => {
95-
return app.httpRequest()
95+
it('should work', async () => {
96+
return await app.httpRequest()
9697
.get('/test')
9798
.expect({
9899
locals: {
@@ -104,9 +105,9 @@ describe('test/app/extend/application.test.js', () => {
104105
});
105106

106107
describe('app.createAnonymousContext()', () => {
107-
let app;
108+
let app: MockApplication;
108109
before(() => {
109-
app = utils.app('apps/demo');
110+
app = createApp('apps/demo');
110111
return app.ready();
111112
});
112113
after(() => app.close());
@@ -120,7 +121,7 @@ describe('test/app/extend/application.test.js', () => {
120121
'x-forwarded-for': '10.0.0.1',
121122
},
122123
url: '/foobar?ok=1',
123-
});
124+
} as any);
124125
assert(ctx.ip === '10.0.0.1');
125126
assert(ctx.url === '/foobar?ok=1');
126127
assert(ctx.socket.remoteAddress === '10.0.0.1');
@@ -129,9 +130,9 @@ describe('test/app/extend/application.test.js', () => {
129130
});
130131

131132
describe('app.addSingleton()', () => {
132-
let app;
133+
let app: MockApplication;
133134
before(() => {
134-
app = utils.app('apps/singleton-demo');
135+
app = createApp('apps/singleton-demo');
135136
return app.ready();
136137
});
137138
after(() => app.close());
@@ -156,7 +157,7 @@ describe('test/app/extend/application.test.js', () => {
156157
try {
157158
app.dataServiceAsync.createInstance({ foo: 'barrr' });
158159
throw new Error('should not execute');
159-
} catch (err) {
160+
} catch (err: any) {
160161
assert(err.message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync');
161162
}
162163

@@ -167,9 +168,9 @@ describe('test/app/extend/application.test.js', () => {
167168
});
168169

169170
describe('app.runInBackground(scope)', () => {
170-
let app;
171+
let app: MockApplication;
171172
before(() => {
172-
app = utils.app('apps/ctx-background');
173+
app = createApp('apps/ctx-background');
173174
return app.ready();
174175
});
175176
after(() => app.close());
@@ -179,72 +180,72 @@ describe('test/app/extend/application.test.js', () => {
179180
.get('/app_background')
180181
.expect(200)
181182
.expect('hello app');
182-
await utils.sleep(2100);
183+
await scheduler.wait(2100);
183184
const logdir = app.config.logger.dir;
184185
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
185186
assert(/mock background run at app result file size: \d+/.test(log));
186187
assert(/mock background run at app anonymous result file size: \d+/.test(log));
187188
assert(
188-
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \([\d\.]+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
189+
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \([\d\.]+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')),
189190
);
190191
});
191192
});
192193

193194
describe('app.runInAnonymousContextScope(scope)', () => {
194195
it('should run task in anonymous context scope success', async () => {
195-
const app = utils.app('apps/app-runInAnonymousContextScope');
196+
const app = createApp('apps/app-runInAnonymousContextScope');
196197
await app.ready();
197198
await app.close();
198-
await utils.sleep(2100);
199+
await scheduler.wait(2100);
199200
const logdir = app.config.logger.dir;
200201
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-web.log'), 'utf8').split('\n');
201202
// console.log(logs);
202203
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on ctx logger
203204
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on app logger
204205
// 2022-12-15 23:03:16,086 INFO 89216 outside before close on app logger
205-
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on ctx logger/);
206-
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on app logger/);
206+
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.1\/-\/[\d\.]+ms GET \/] inside before close on ctx logger/);
207+
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.1\/-\/[\d\.]+ms GET \/] inside before close on app logger/);
207208
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
208209
});
209210
});
210211

211212
describe('app.runInAnonymousContextScope(scope,request)', () => {
212213
it('should run task in anonymous context scope with req success', async () => {
213-
const app = utils.app('apps/app-runInAnonymousContextScope-withRequest');
214+
const app = createApp('apps/app-runInAnonymousContextScope-withRequest');
214215
await app.ready();
215216
await app.close();
216-
await utils.sleep(2100);
217+
await scheduler.wait(2100);
217218
const logdir = app.config.logger.dir;
218219
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-withRequest-web.log'), { encoding: 'utf8' }).split('\n');
219220

220-
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on ctx logger/);
221-
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on app logger/);
221+
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.2\/-\/[\d\.]+ms GET \/] inside before close on ctx logger/);
222+
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.2\/-\/[\d\.]+ms GET \/] inside before close on app logger/);
222223
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
223224
});
224225
});
225226

226227
describe('app.handleRequest(ctx, fnMiddleware)', () => {
227-
let app;
228+
let app: MockApplication;
228229
before(() => {
229-
app = utils.app('apps/demo');
230+
app = createApp('apps/demo');
230231
return app.ready();
231232
});
232233
after(() => app.close());
233234

234235
it('should wait for middleware resolution', async () => {
235236
const ctx = app.createAnonymousContext();
236-
await app.handleRequest(ctx, async ctx => {
237-
await utils.sleep(100);
237+
await app.handleRequest(ctx, async (ctx: any) => {
238+
await scheduler.wait(100);
238239
ctx.body = 'middleware resolution';
239240
});
240241
assert(ctx.body === 'middleware resolution');
241242
});
242243
});
243244

244245
describe('app.keys', () => {
245-
let app;
246+
let app: MockApplication;
246247
before(() => {
247-
app = utils.app('apps/demo');
248+
app = createApp('apps/demo');
248249
return app.ready();
249250
});
250251
after(() => app.close());

test/lib/application.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('test/lib/application.test.js', () => {
124124
.expect('foo')
125125
.expect(200);
126126

127-
await utils.sleep(1100);
127+
await scheduler.wait(1100);
128128
const logfile = path.join(utils.getFilepath('apps/app-throw'), 'logs/app-throw/common-error.log');
129129
const body = fs.readFileSync(logfile, 'utf8');
130130
assert(body.includes('ReferenceError: a is not defined (uncaughtException throw'));
@@ -145,7 +145,7 @@ describe('test/lib/application.test.js', () => {
145145
.expect('foo')
146146
.expect(200);
147147

148-
await utils.sleep(1100);
148+
await scheduler.wait(1100);
149149
const logfile = path.join(utils.getFilepath('apps/app-throw'), 'logs/app-throw/common-error.log');
150150
const body = fs.readFileSync(logfile, 'utf8');
151151
assert(body.includes('abc (uncaughtException throw 1 times on pid'));
@@ -156,7 +156,7 @@ describe('test/lib/application.test.js', () => {
156156
it('should warn if confused configurations exist', async () => {
157157
const app = utils.app('apps/confused-configuration');
158158
await app.ready();
159-
await utils.sleep(1000);
159+
await scheduler.wait(1000);
160160
const logs = fs.readFileSync(utils.getFilepath('apps/confused-configuration/logs/confused-configuration/confused-configuration-web.log'), 'utf8');
161161
assert(logs.match(/Unexpected config key `bodyparser` exists, Please use `bodyParser` instead\./));
162162
assert(logs.match(/Unexpected config key `notFound` exists, Please use `notfound` instead\./));

test/lib/cluster/app_worker.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('test/lib/cluster/app_worker.test.js', () => {
120120

121121
it('should not log when there is no rawPacket', async () => {
122122
await connect(app.port);
123-
await utils.sleep(1000);
123+
await scheduler.wait(1000);
124124
app.expect('stderr', /HPE_INVALID_EOF_STATE/);
125125
app.notExpect('stderr', /A client/);
126126
});

test/lib/cluster/cluster-client-error.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('test/lib/cluster/cluster-client-error.test.js', () => {
2222
});
2323

2424
it('should follower not throw error', async () => {
25-
await utils.sleep(1000);
25+
await scheduler.wait(1000);
2626
const cnt = await readFile(path.join(__dirname, '../../fixtures/apps/cluster-client-error/logs/cluster-client-error/common-error.log'), 'utf8');
2727
assert(!cnt.includes('ECONNRESET'));
2828
});

test/lib/cluster/master.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('test/lib/cluster/master.test.js', () => {
2424
}
2525

2626
// wait for app worker restart
27-
await utils.sleep(5000);
27+
await scheduler.wait(5000);
2828

2929
// error pipe to console
3030
app.expect('stdout', /app_worker#1:\d+ disconnect/);
@@ -42,7 +42,7 @@ describe('test/lib/cluster/master.test.js', () => {
4242
}
4343

4444
// wait for app worker restart
45-
await utils.sleep(5000);
45+
await scheduler.wait(5000);
4646

4747
app.expect('stderr', /\[graceful:worker:\d+:uncaughtException] throw error 1 times/);
4848
app.expect('stdout', /app_worker#\d:\d+ started/);
@@ -68,7 +68,7 @@ describe('test/lib/cluster/master.test.js', () => {
6868
}
6969

7070
// wait for app worker restart
71-
await utils.sleep(5000);
71+
await scheduler.wait(5000);
7272

7373
// error pipe to console
7474
app.notExpect('stdout', /app_worker#1:\d+ disconnect/);
@@ -83,7 +83,7 @@ describe('test/lib/cluster/master.test.js', () => {
8383
}
8484

8585
// wait for app worker restart
86-
await utils.sleep(5000);
86+
await scheduler.wait(5000);
8787

8888
app.expect('stderr', /\[graceful:worker:\d+:uncaughtException] throw error 1 times/);
8989
app.expect('stderr', /matches ignore list/);

0 commit comments

Comments
 (0)