Skip to content

Commit

Permalink
make agent extend work
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 20, 2024
1 parent 7e3edc6 commit 5711f2e
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 100 deletions.
32 changes: 14 additions & 18 deletions test/app/extend/agent.test.js → test/app/extend/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
'use strict';
import { strict as assert } from 'node:assert';
import { restore, createApp, MockApplication } from '../../utils.js';

const assert = require('assert');

const mm = require('egg-mock');
const utils = require('../../utils');

describe('test/app/extend/agent.test.js', () => {
afterEach(mm.restore);
describe('test/app/extend/agent.test.ts', () => {
afterEach(restore);

describe('agent.addSingleton()', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/singleton-demo');
app = createApp('apps/singleton-demo');
return app.ready();
});
after(() => app.close());
Expand All @@ -21,28 +17,28 @@ describe('test/app/extend/agent.test.js', () => {
assert(config.foo === 'bar');
assert(config.foo2 === 'bar2');

const ds = app.agent.dataService.createInstance({ foo: 'barrr' });
const ds = app.agent.dataService.createInstance({ foo: 'bar2' });
config = await ds.getConfig();
assert(config.foo === 'barrr');
assert(config.foo === 'bar2');

const ds2 = await app.agent.dataService.createInstanceAsync({ foo: 'barrr' });
const ds2 = await app.agent.dataService.createInstanceAsync({ foo: 'bar2' });
config = await ds2.getConfig();
assert(config.foo === 'barrr');
assert(config.foo === 'bar2');

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

try {
app.agent.dataServiceAsync.createInstance({ foo: 'barrr' });
app.agent.dataServiceAsync.createInstance({ foo: 'bar2' });
throw new Error('should not execute');
} catch (err) {
} catch (err: any) {
assert(err.message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync');
}

const ds4 = await app.agent.dataServiceAsync.createInstanceAsync({ foo: 'barrr' });
const ds4 = await app.agent.dataServiceAsync.createInstanceAsync({ foo: 'bar2' });
config = await ds4.getConfig();
assert(config.foo === 'barrr');
assert(config.foo === 'bar2');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const utils = require('../../utils');
import { strict as assert } from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import { scheduler } from 'node:timers/promises';
import { createApp, MockApplication, cluster } from '../../utils.js';

describe('test/app/extend/application.test.js', () => {
describe('test/app/extend/application.test.ts', () => {
describe('app.logger', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());
Expand All @@ -17,23 +18,23 @@ describe('test/app/extend/application.test.js', () => {
assert(app.logger === app.loggers.logger);
});

it('should alias app.coreLooger => app.loggers.coreLooger', () => {
it('should alias app.coreLogger => app.loggers.coreLogger', () => {
assert(app.coreLogger === app.loggers.coreLogger);
});

it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLooger', () => {
it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLogger', () => {
assert(app.getLogger('coreLogger') === app.loggers.coreLogger);
});

it('should alias app.getLogger(\'noexist\') => null', () => {
assert(app.getLogger('noexist') === null);
it('should alias app.getLogger(\'noExist\') => null', () => {
assert(app.getLogger('noExist') === null);
});
});

describe('app.inspect()', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());
Expand All @@ -50,22 +51,22 @@ describe('test/app/extend/application.test.js', () => {
});

describe('app.readyCallback()', () => {
let app;
let app: MockApplication;
after(() => app.close());

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

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

describe('app.locals', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/locals');
app = createApp('apps/locals');
return app.ready();
});
after(() => app.close());
Expand All @@ -84,15 +85,15 @@ describe('test/app/extend/application.test.js', () => {
});

describe('app.locals.foo = bar', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/app-locals-getter');
app = createApp('apps/app-locals-getter');
return app.ready();
});
after(() => app.close());

it('should work', () => {
return app.httpRequest()
it('should work', async () => {
return await app.httpRequest()
.get('/test')
.expect({
locals: {
Expand All @@ -104,9 +105,9 @@ describe('test/app/extend/application.test.js', () => {
});

describe('app.createAnonymousContext()', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());
Expand All @@ -120,7 +121,7 @@ describe('test/app/extend/application.test.js', () => {
'x-forwarded-for': '10.0.0.1',
},
url: '/foobar?ok=1',
});
} as any);
assert(ctx.ip === '10.0.0.1');
assert(ctx.url === '/foobar?ok=1');
assert(ctx.socket.remoteAddress === '10.0.0.1');
Expand All @@ -129,9 +130,9 @@ describe('test/app/extend/application.test.js', () => {
});

describe('app.addSingleton()', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/singleton-demo');
app = createApp('apps/singleton-demo');
return app.ready();
});
after(() => app.close());
Expand All @@ -156,7 +157,7 @@ describe('test/app/extend/application.test.js', () => {
try {
app.dataServiceAsync.createInstance({ foo: 'barrr' });
throw new Error('should not execute');
} catch (err) {
} catch (err: any) {
assert(err.message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync');
}

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

describe('app.runInBackground(scope)', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/ctx-background');
app = createApp('apps/ctx-background');
return app.ready();
});
after(() => app.close());
Expand All @@ -179,72 +180,72 @@ describe('test/app/extend/application.test.js', () => {
.get('/app_background')
.expect(200)
.expect('hello app');
await utils.sleep(2100);
await scheduler.wait(2100);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/mock background run at app result file size: \d+/.test(log));
assert(/mock background run at app anonymous result file size: \d+/.test(log));
assert(
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \([\d\.]+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \([\d\.]+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')),
);
});
});

describe('app.runInAnonymousContextScope(scope)', () => {
it('should run task in anonymous context scope success', async () => {
const app = utils.app('apps/app-runInAnonymousContextScope');
const app = createApp('apps/app-runInAnonymousContextScope');
await app.ready();
await app.close();
await utils.sleep(2100);
await scheduler.wait(2100);
const logdir = app.config.logger.dir;
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-web.log'), 'utf8').split('\n');
// console.log(logs);
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on ctx logger
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on app logger
// 2022-12-15 23:03:16,086 INFO 89216 outside before close on app logger
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on ctx logger/);
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on app logger/);
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.1\/-\/[\d\.]+ms GET \/] inside before close on ctx logger/);
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.1\/-\/[\d\.]+ms GET \/] inside before close on app logger/);
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
});
});

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

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

describe('app.handleRequest(ctx, fnMiddleware)', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());

it('should wait for middleware resolution', async () => {
const ctx = app.createAnonymousContext();
await app.handleRequest(ctx, async ctx => {
await utils.sleep(100);
await app.handleRequest(ctx, async (ctx: any) => {
await scheduler.wait(100);
ctx.body = 'middleware resolution';
});
assert(ctx.body === 'middleware resolution');
});
});

describe('app.keys', () => {
let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());
Expand Down
6 changes: 3 additions & 3 deletions test/lib/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('test/lib/application.test.js', () => {
.expect('foo')
.expect(200);

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

await utils.sleep(1100);
await scheduler.wait(1100);
const logfile = path.join(utils.getFilepath('apps/app-throw'), 'logs/app-throw/common-error.log');
const body = fs.readFileSync(logfile, 'utf8');
assert(body.includes('abc (uncaughtException throw 1 times on pid'));
Expand All @@ -156,7 +156,7 @@ describe('test/lib/application.test.js', () => {
it('should warn if confused configurations exist', async () => {
const app = utils.app('apps/confused-configuration');
await app.ready();
await utils.sleep(1000);
await scheduler.wait(1000);
const logs = fs.readFileSync(utils.getFilepath('apps/confused-configuration/logs/confused-configuration/confused-configuration-web.log'), 'utf8');
assert(logs.match(/Unexpected config key `bodyparser` exists, Please use `bodyParser` instead\./));
assert(logs.match(/Unexpected config key `notFound` exists, Please use `notfound` instead\./));
Expand Down
2 changes: 1 addition & 1 deletion test/lib/cluster/app_worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('test/lib/cluster/app_worker.test.js', () => {

it('should not log when there is no rawPacket', async () => {
await connect(app.port);
await utils.sleep(1000);
await scheduler.wait(1000);
app.expect('stderr', /HPE_INVALID_EOF_STATE/);
app.notExpect('stderr', /A client/);
});
Expand Down
2 changes: 1 addition & 1 deletion test/lib/cluster/cluster-client-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('test/lib/cluster/cluster-client-error.test.js', () => {
});

it('should follower not throw error', async () => {
await utils.sleep(1000);
await scheduler.wait(1000);
const cnt = await readFile(path.join(__dirname, '../../fixtures/apps/cluster-client-error/logs/cluster-client-error/common-error.log'), 'utf8');
assert(!cnt.includes('ECONNRESET'));
});
Expand Down
8 changes: 4 additions & 4 deletions test/lib/cluster/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('test/lib/cluster/master.test.js', () => {
}

// wait for app worker restart
await utils.sleep(5000);
await scheduler.wait(5000);

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

// wait for app worker restart
await utils.sleep(5000);
await scheduler.wait(5000);

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

// wait for app worker restart
await utils.sleep(5000);
await scheduler.wait(5000);

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

// wait for app worker restart
await utils.sleep(5000);
await scheduler.wait(5000);

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

0 comments on commit 5711f2e

Please sign in to comment.