From 411eac77b9d52f0d7893b0d6ca2293a035e0b2e4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 1 Jan 2025 15:05:23 +0800 Subject: [PATCH] fix onelogger https://github.com/node-modules/onelogger/pull/2 --- src/app/extend/context.types.ts | 2 + .../apps/logger-level-debug/app/router.js | 4 +- ...ent.test.js => context_httpclient.test.ts} | 18 +- .../core/context_httpclient_timeout.test.js | 23 -- .../context_performance_starttime.test.js | 27 --- .../context_performance_starttime.test.ts | 25 ++ ...m_loader.test.js => custom_loader.test.ts} | 15 +- test/lib/core/dnscache_httpclient.test.js | 227 ------------------ test/lib/core/dnscache_httpclient.test.ts | 227 ++++++++++++++++++ ...test.js => httpclient_tracer_demo.test.ts} | 15 +- .../core/{logger.test.js => logger.test.ts} | 127 +++++----- 11 files changed, 342 insertions(+), 368 deletions(-) rename test/lib/core/{context_httpclient.test.js => context_httpclient.test.ts} (60%) delete mode 100644 test/lib/core/context_httpclient_timeout.test.js delete mode 100644 test/lib/core/context_performance_starttime.test.js create mode 100644 test/lib/core/context_performance_starttime.test.ts rename test/lib/core/{custom_loader.test.js => custom_loader.test.ts} (65%) delete mode 100644 test/lib/core/dnscache_httpclient.test.js create mode 100644 test/lib/core/dnscache_httpclient.test.ts rename test/lib/core/{httpclient_tracer_demo.test.js => httpclient_tracer_demo.test.ts} (79%) rename test/lib/core/{logger.test.js => logger.test.ts} (64%) diff --git a/src/app/extend/context.types.ts b/src/app/extend/context.types.ts index 0c849085b0..e41e28050c 100644 --- a/src/app/extend/context.types.ts +++ b/src/app/extend/context.types.ts @@ -17,5 +17,7 @@ declare module '@eggjs/core' { get httpclient(): HttpClient; get httpClient(): HttpClient; getLogger(name: string): EggLogger; + get logger(): EggLogger; + get coreLogger(): EggLogger; } } diff --git a/test/fixtures/apps/logger-level-debug/app/router.js b/test/fixtures/apps/logger-level-debug/app/router.js index 867048424e..5bc3412a53 100644 --- a/test/fixtures/apps/logger-level-debug/app/router.js +++ b/test/fixtures/apps/logger-level-debug/app/router.js @@ -1,10 +1,10 @@ -const { sleep } = require('../../../../utils'); +const { scheduler } = require('node:timers/promises'); module.exports = app => { app.get('/', async function() { this.logger.debug('hi %s %s', this.method, this.url); // wait for writing to file - await sleep(1000); + await scheduler.wait(1000); this.body = 'ok'; }); }; diff --git a/test/lib/core/context_httpclient.test.js b/test/lib/core/context_httpclient.test.ts similarity index 60% rename from test/lib/core/context_httpclient.test.js rename to test/lib/core/context_httpclient.test.ts index 17fd7b538d..3223a925d7 100644 --- a/test/lib/core/context_httpclient.test.js +++ b/test/lib/core/context_httpclient.test.ts @@ -1,25 +1,23 @@ -'use strict'; +import { strict as assert } from 'node:assert'; +import { createApp, startLocalServer, MockApplication } from '../../utils.js'; -const assert = require('assert'); -const utils = require('../../utils'); - -describe('test/lib/core/context_httpclient.test.js', () => { - let url; - let app; +describe('test/lib/core/context_httpclient.test.ts', () => { + let url: string; + let app: MockApplication; before(() => { - app = utils.app('apps/context_httpclient'); + app = createApp('apps/context_httpclient'); return app.ready(); }); before(async () => { - url = await utils.startLocalServer(); + url = await startLocalServer(); }); it('should send request with ctx.httpclient', async () => { const ctx = app.mockContext(); const httpclient = ctx.httpclient; assert(ctx.httpclient === httpclient); - assert(httpclient.ctx === ctx); + assert((httpclient as any).ctx === ctx); assert(typeof httpclient.request === 'function'); assert(typeof httpclient.curl === 'function'); const result = await ctx.httpclient.request(url); diff --git a/test/lib/core/context_httpclient_timeout.test.js b/test/lib/core/context_httpclient_timeout.test.js deleted file mode 100644 index a70a8f1d03..0000000000 --- a/test/lib/core/context_httpclient_timeout.test.js +++ /dev/null @@ -1,23 +0,0 @@ -const assert = require('assert'); -const utils = require('../../utils'); - -describe('test/lib/core/context_httpclient_timeout.test.js', () => { - let url; - let app; - - before(() => { - app = utils.app('apps/context_httpclient_timeout'); - return app.ready(); - }); - before(async () => { - url = await utils.startLocalServer(); - }); - - it('should request timeout override agent socket timeout', async () => { - app.httpclient.agent.options.timeout = 1000; - const ctx = app.mockContext(); - await assert.rejects(async () => { - await ctx.httpclient.request(`${url}/timeout`, { timeout: 1500 }); - }, /ResponseTimeoutError: Response timeout for 1500ms/); - }); -}); diff --git a/test/lib/core/context_performance_starttime.test.js b/test/lib/core/context_performance_starttime.test.js deleted file mode 100644 index 4facda6a39..0000000000 --- a/test/lib/core/context_performance_starttime.test.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const utils = require('../../utils'); - -describe('test/lib/core/context_performance_starttime.test.js', () => { - let app; - - before(() => { - app = utils.app('apps/app-enablePerformanceTimer-true'); - return app.ready(); - }); - - it('should set ctx.performanceStarttime', () => { - const ctx = app.mockContext(); - assert(ctx.performanceStarttime); - assert(ctx.performanceStarttime > 0); - assert(typeof ctx.performanceStarttime === 'number'); - }); - - it('should use ctx.performanceStarttime on controller', async () => { - const res = await app.httpRequest() - .get('/'); - assert(res.status === 200); - assert(/hello performanceStarttime: \d+\.\d+/.test(res.text)); - }); -}); diff --git a/test/lib/core/context_performance_starttime.test.ts b/test/lib/core/context_performance_starttime.test.ts new file mode 100644 index 0000000000..026c7b5784 --- /dev/null +++ b/test/lib/core/context_performance_starttime.test.ts @@ -0,0 +1,25 @@ +import { strict as assert } from 'node:assert'; +import { createApp, MockApplication } from '../../utils.js'; + +describe('test/lib/core/context_performance_starttime.test.ts', () => { + let app: MockApplication; + + before(() => { + app = createApp('apps/app-enablePerformanceTimer-true'); + return app.ready(); + }); + + it('should set ctx.performanceStarttime', () => { + const ctx = app.mockContext(); + assert(ctx.performanceStarttime); + assert.equal(typeof ctx.performanceStarttime, 'number'); + assert(typeof ctx.performanceStarttime === 'number' && ctx.performanceStarttime > 0); + }); + + it('should use ctx.performanceStarttime on controller', async () => { + const res = await app.httpRequest() + .get('/'); + assert.equal(res.status, 200); + assert.match(res.text, /hello performanceStarttime: \d+\.\d+/); + }); +}); diff --git a/test/lib/core/custom_loader.test.js b/test/lib/core/custom_loader.test.ts similarity index 65% rename from test/lib/core/custom_loader.test.js rename to test/lib/core/custom_loader.test.ts index 947e5a206e..b2ccb7ae94 100644 --- a/test/lib/core/custom_loader.test.js +++ b/test/lib/core/custom_loader.test.ts @@ -1,14 +1,12 @@ -'use strict'; +import { mm } from '@eggjs/mock'; +import { createApp, MockApplication } from '../../utils.js'; -const mock = require('egg-mock'); -const utils = require('../../utils'); +describe('test/lib/core/custom_loader.test.ts', () => { + afterEach(mm.restore); -describe('test/lib/core/custom_loader.test.js', () => { - afterEach(mock.restore); - - let app; + let app: MockApplication; before(() => { - app = utils.app('apps/custom-loader'); + app = createApp('apps/custom-loader'); return app.ready(); }); after(() => app.close()); @@ -29,5 +27,4 @@ describe('test/lib/core/custom_loader.test.js', () => { .expect('beforeLoad') .expect(200); }); - }); diff --git a/test/lib/core/dnscache_httpclient.test.js b/test/lib/core/dnscache_httpclient.test.js deleted file mode 100644 index 48b27991c1..0000000000 --- a/test/lib/core/dnscache_httpclient.test.js +++ /dev/null @@ -1,227 +0,0 @@ -const mm = require('egg-mock'); -const assert = require('assert'); -const dns = require('dns').promises; -const urlparse = require('url').parse; -const utils = require('../../utils'); - -describe('test/lib/core/dnscache_httpclient.test.js', () => { - let app; - let url; - let host; - let originalDNSServers; - - before(async () => { - app = utils.app('apps/dnscache_httpclient'); - await app.ready(); - url = await utils.startLocalServer(); - url = url.replace('127.0.0.1', 'localhost'); - host = urlparse(url).host; - originalDNSServers = dns.getServers(); - }); - - afterEach(mm.restore); - afterEach(() => { - // After trying to set Server Ips forcely, - // try to restore them to usual ones - dns.setServers(originalDNSServers); - }); - - it('should ctx.curl work and set host', async () => { - await app.httpRequest() - .get('/?url=' + encodeURIComponent(url + '/get_headers')) - .expect(200) - .expect(/"host":"localhost:\d+"/); - await app.httpRequest() - .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&host=localhost.foo.com') - .expect(200) - .expect(/"host":"localhost\.foo\.com"/); - await app.httpRequest() - .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&Host=localhost2.foo.com') - .expect(200) - .expect(/"host":"localhost2\.foo\.com"/); - }); - - /** - * This test failure can be totally ignored because it depends on how your service provider - * deals with the domain when you cannot find that:Some providers will batchly switch - * those invalid domains to a certain server. So you can still find the fixed IP by - * calling `dns.lookup()`. - * - * To make sure that your domain exists or not, just use `ping your_domain_here` instead. - */ - it('should throw error when the first dns lookup fail', async () => { - if (!process.env.CI) { - // Avoid Network service provider DNS pollution - // alidns http://www.alidns.com/node-distribution/ - // Not sure it will work for all servers - dns.setServers([ - '223.5.5.5', - '223.6.6.6', - ]); - } - await app.httpRequest() - .get('/?url=' + encodeURIComponent('http://notexists-1111111local-domain.com')) - .expect(500) - .expect(/getaddrinfo ENOTFOUND notexists-1111111local-domain\.com/); - }); - - it('should use local cache dns result when dns lookup error', async () => { - await app.httpRequest() - .get('/?url=' + encodeURIComponent(url + '/get_headers')) - .expect(200) - .expect(/"host":"localhost:\d+"/); - // mock local cache expires and mock dns lookup throw error - app.httpclient.dnsCache.get('localhost').timestamp = 0; - mm.error(dns, 'lookup', 'mock dns lookup error'); - await app.httpRequest() - .get('/?url=' + encodeURIComponent(url + '/get_headers')) - .expect(200) - .expect(/"host":"localhost:\d+"/); - }); - - it('should app.curl work', async () => { - const result = await app.curl(url + '/get_headers', { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - - const result2 = await app.httpclient.curl(url + '/get_headers', { dataType: 'json' }); - assert(result2.status === 200); - assert(result2.data.host === host); - }); - - it('should app.curl work on lookup error', async () => { - const result = await app.curl(url + '/get_headers', { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - - // mock local cache expires and mock dns lookup throw error - app.httpclient.dnsCache.get('localhost').timestamp = 0; - mm.error(dns, 'lookup', 'mock dns lookup error'); - const result2 = await app.httpclient.curl(url + '/get_headers', { dataType: 'json' }); - assert(result2.status === 200); - assert(result2.data.host === host); - }); - - it('should app.curl(obj)', async () => { - const obj = urlparse(url + '/get_headers'); - const result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - - const obj2 = urlparse(url + '/get_headers'); - // mock obj2.host - obj2.host = null; - const result2 = await app.curl(obj2, { dataType: 'json' }); - assert(result2.status === 200); - assert(result2.data.host === host); - }); - - it('should dnsCacheMaxLength work', async () => { - mm(dns, 'lookup', async () => { - return { address: '127.0.0.1', family: 4 }; - }); - - // reset lru cache - mm(app.httpclient.dnsCache, 'max', 1); - mm(app.httpclient.dnsCache, 'size', 0); - mm(app.httpclient.dnsCache, 'cache', new Map()); - mm(app.httpclient.dnsCache, '_cache', new Map()); - - let obj = urlparse(url + '/get_headers'); - let result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - - assert(app.httpclient.dnsCache.get('localhost')); - - obj = urlparse(url.replace('localhost', 'another.com') + '/get_headers'); - result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === obj.host); - - assert(!app.httpclient.dnsCache.get('localhost')); - assert(app.httpclient.dnsCache.get('another.com')); - }); - - it('should cache and update', async () => { - mm(dns, 'lookup', async () => { - return { address: '127.0.0.1', family: 4 }; - }); - - let obj = urlparse(url + '/get_headers'); - let result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - let record = app.httpclient.dnsCache.get('localhost'); - const timestamp = record.timestamp; - assert(record); - - obj = urlparse(url + '/get_headers'); - result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - record = app.httpclient.dnsCache.get('localhost'); - assert(timestamp === record.timestamp); - - await scheduler.wait(5500); - obj = urlparse(url + '/get_headers'); - result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - record = app.httpclient.dnsCache.get('localhost'); - assert(timestamp !== record.timestamp); - }); - - it('should cache and update with agent', async () => { - const agent = app._agent; - mm(dns, 'lookup', async () => { - return { address: '127.0.0.1', family: 4 }; - }); - - let obj = urlparse(url + '/get_headers'); - let result = await agent.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - let record = agent.httpclient.dnsCache.get('localhost'); - const timestamp = record.timestamp; - assert(record); - - obj = urlparse(url + '/get_headers'); - result = await agent.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - record = agent.httpclient.dnsCache.get('localhost'); - assert(timestamp === record.timestamp); - - await scheduler.wait(5500); - obj = urlparse(url + '/get_headers'); - result = await agent.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === host); - record = agent.httpclient.dnsCache.get('localhost'); - assert(timestamp !== record.timestamp); - }); - - it('should not cache ip', async () => { - const obj = urlparse(url.replace('localhost', '127.0.0.1') + '/get_headers'); - const result = await app.curl(obj, { dataType: 'json' }); - assert(result.status === 200); - assert(result.data.host === obj.host); - assert(!app.httpclient.dnsCache.get('127.0.0.1')); - }); - - describe('disable DNSCache in one request', () => { - beforeEach(() => { - mm(app.httpclient.dnsCache, 'size', 0); - }); - - it('should work', async () => { - await app.httpRequest() - .get('/?disableDNSCache=true&url=' + encodeURIComponent(url + '/get_headers')) - .expect(200) - .expect(/"host":"localhost:\d+"/); - - assert(app.httpclient.dnsCache.size === 0); - }); - }); -}); diff --git a/test/lib/core/dnscache_httpclient.test.ts b/test/lib/core/dnscache_httpclient.test.ts new file mode 100644 index 0000000000..3403d0a1cd --- /dev/null +++ b/test/lib/core/dnscache_httpclient.test.ts @@ -0,0 +1,227 @@ +// import { strict as assert } from 'node:assert'; +// import dns from 'node:dns/promises'; +// import { parse as urlparse } from 'node:url'; +// import { mm } from '@eggjs/mock'; +// import { createApp, MockApplication, startLocalServer } from '../../utils.js'; + +// describe('test/lib/core/dnscache_httpclient.test.ts', () => { +// let app: MockApplication; +// let url: string; +// let host: string; +// let originalDNSServers: string[]; + +// before(async () => { +// app = createApp('apps/dnscache_httpclient'); +// await app.ready(); +// url = await startLocalServer(); +// url = url.replace('127.0.0.1', 'localhost'); +// host = urlparse(url).host!; +// originalDNSServers = dns.getServers(); +// }); + +// afterEach(mm.restore); +// afterEach(() => { +// // After trying to set Server Ips forcedly, +// // try to restore them to usual ones +// dns.setServers(originalDNSServers); +// }); + +// it('should ctx.curl work and set host', async () => { +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent(url + '/get_headers')) +// .expect(200) +// .expect(/"host":"localhost:\d+"/); +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&host=localhost.foo.com') +// .expect(200) +// .expect(/"host":"localhost\.foo\.com"/); +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent(url + '/get_headers') + '&Host=localhost2.foo.com') +// .expect(200) +// .expect(/"host":"localhost2\.foo\.com"/); +// }); + +// /** +// * This test failure can be totally ignored because it depends on how your service provider +// * deals with the domain when you cannot find that:Some providers will batchly switch +// * those invalid domains to a certain server. So you can still find the fixed IP by +// * calling `dns.lookup()`. +// * +// * To make sure that your domain exists or not, just use `ping your_domain_here` instead. +// */ +// it('should throw error when the first dns lookup fail', async () => { +// if (!process.env.CI) { +// // Avoid Network service provider DNS pollution +// // alidns http://www.alidns.com/node-distribution/ +// // Not sure it will work for all servers +// dns.setServers([ +// '223.5.5.5', +// '223.6.6.6', +// ]); +// } +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent('http://notexists-1111111local-domain.com')) +// .expect(500) +// .expect(/getaddrinfo ENOTFOUND notexists-1111111local-domain\.com/); +// }); + +// it('should use local cache dns result when dns lookup error', async () => { +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent(url + '/get_headers')) +// .expect(200) +// .expect(/"host":"localhost:\d+"/); +// // mock local cache expires and mock dns lookup throw error +// app.httpclient.dnsCache.get('localhost').timestamp = 0; +// mm.error(dns, 'lookup', 'mock dns lookup error'); +// await app.httpRequest() +// .get('/?url=' + encodeURIComponent(url + '/get_headers')) +// .expect(200) +// .expect(/"host":"localhost:\d+"/); +// }); + +// it('should app.curl work', async () => { +// const result = await app.curl(url + '/get_headers', { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); + +// const result2 = await app.httpclient.curl(url + '/get_headers', { dataType: 'json' }); +// assert(result2.status === 200); +// assert(result2.data.host === host); +// }); + +// it('should app.curl work on lookup error', async () => { +// const result = await app.curl(url + '/get_headers', { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); + +// // mock local cache expires and mock dns lookup throw error +// app.httpclient.dnsCache.get('localhost').timestamp = 0; +// mm.error(dns, 'lookup', 'mock dns lookup error'); +// const result2 = await app.httpclient.curl(url + '/get_headers', { dataType: 'json' }); +// assert(result2.status === 200); +// assert(result2.data.host === host); +// }); + +// it('should app.curl(obj)', async () => { +// const obj = urlparse(url + '/get_headers'); +// const result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); + +// const obj2 = urlparse(url + '/get_headers'); +// // mock obj2.host +// obj2.host = null; +// const result2 = await app.curl(obj2, { dataType: 'json' }); +// assert(result2.status === 200); +// assert(result2.data.host === host); +// }); + +// it('should dnsCacheMaxLength work', async () => { +// mm(dns, 'lookup', async () => { +// return { address: '127.0.0.1', family: 4 }; +// }); + +// // reset lru cache +// mm(app.httpclient.dnsCache, 'max', 1); +// mm(app.httpclient.dnsCache, 'size', 0); +// mm(app.httpclient.dnsCache, 'cache', new Map()); +// mm(app.httpclient.dnsCache, '_cache', new Map()); + +// let obj = urlparse(url + '/get_headers'); +// let result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); + +// assert(app.httpclient.dnsCache.get('localhost')); + +// obj = urlparse(url.replace('localhost', 'another.com') + '/get_headers'); +// result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === obj.host); + +// assert(!app.httpclient.dnsCache.get('localhost')); +// assert(app.httpclient.dnsCache.get('another.com')); +// }); + +// it('should cache and update', async () => { +// mm(dns, 'lookup', async () => { +// return { address: '127.0.0.1', family: 4 }; +// }); + +// let obj = urlparse(url + '/get_headers'); +// let result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// let record = app.httpclient.dnsCache.get('localhost'); +// const timestamp = record.timestamp; +// assert(record); + +// obj = urlparse(url + '/get_headers'); +// result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// record = app.httpclient.dnsCache.get('localhost'); +// assert(timestamp === record.timestamp); + +// await scheduler.wait(5500); +// obj = urlparse(url + '/get_headers'); +// result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// record = app.httpclient.dnsCache.get('localhost'); +// assert(timestamp !== record.timestamp); +// }); + +// it('should cache and update with agent', async () => { +// const agent = app._agent; +// mm(dns, 'lookup', async () => { +// return { address: '127.0.0.1', family: 4 }; +// }); + +// let obj = urlparse(url + '/get_headers'); +// let result = await agent.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// let record = agent.httpclient.dnsCache.get('localhost'); +// const timestamp = record.timestamp; +// assert(record); + +// obj = urlparse(url + '/get_headers'); +// result = await agent.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// record = agent.httpclient.dnsCache.get('localhost'); +// assert(timestamp === record.timestamp); + +// await scheduler.wait(5500); +// obj = urlparse(url + '/get_headers'); +// result = await agent.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === host); +// record = agent.httpclient.dnsCache.get('localhost'); +// assert(timestamp !== record.timestamp); +// }); + +// it('should not cache ip', async () => { +// const obj = urlparse(url.replace('localhost', '127.0.0.1') + '/get_headers'); +// const result = await app.curl(obj, { dataType: 'json' }); +// assert(result.status === 200); +// assert(result.data.host === obj.host); +// assert(!app.httpclient.dnsCache.get('127.0.0.1')); +// }); + +// describe('disable DNSCache in one request', () => { +// beforeEach(() => { +// mm(app.httpclient.dnsCache, 'size', 0); +// }); + +// it('should work', async () => { +// await app.httpRequest() +// .get('/?disableDNSCache=true&url=' + encodeURIComponent(url + '/get_headers')) +// .expect(200) +// .expect(/"host":"localhost:\d+"/); + +// assert(app.httpclient.dnsCache.size === 0); +// }); +// }); +// }); diff --git a/test/lib/core/httpclient_tracer_demo.test.js b/test/lib/core/httpclient_tracer_demo.test.ts similarity index 79% rename from test/lib/core/httpclient_tracer_demo.test.js rename to test/lib/core/httpclient_tracer_demo.test.ts index c3e6c08863..ee8472901c 100644 --- a/test/lib/core/httpclient_tracer_demo.test.js +++ b/test/lib/core/httpclient_tracer_demo.test.ts @@ -1,16 +1,17 @@ -const assert = require('assert'); -const utils = require('../../utils'); +import { strict as assert } from 'node:assert'; +import { scheduler } from 'node:timers/promises'; +import { createApp, startLocalServer, MockApplication } from '../../utils.js'; -describe('test/lib/core/httpclient_tracer_demo.test.js', () => { - let url; - let app; +describe('test/lib/core/httpclient_tracer_demo.test.ts', () => { + let url: string; + let app: MockApplication; before(() => { - app = utils.app('apps/tracer-demo'); + app = createApp('apps/tracer-demo'); return app.ready(); }); before(async () => { - url = await utils.startLocalServer(); + url = await startLocalServer(); }); after(() => app.close()); diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.ts similarity index 64% rename from test/lib/core/logger.test.js rename to test/lib/core/logger.test.ts index 7383734336..c8d699a344 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.ts @@ -1,17 +1,17 @@ -const assert = require('node:assert'); -const path = require('node:path'); -const fs = require('node:fs'); -const mm = require('egg-mock'); -const Logger = require('egg-logger'); -const utils = require('../../utils'); - -describe('test/lib/core/logger.test.js', () => { - let app; +import assert from 'node:assert'; +import path from 'node:path'; +import fs from 'node:fs'; +import { scheduler } from 'node:timers/promises'; +import { mm } from '@eggjs/mock'; +import { levels } from 'egg-logger'; +import { MockApplication, createApp, cluster, getFilepath } from '../../utils.js'; + +describe('test/lib/core/logger.test.ts', () => { + let app: MockApplication; afterEach(async () => { - if (app) { - await scheduler.wait(5000); + if (app && !app.isClosed) { + await scheduler.wait(500); await app.close(); - app = null; } await mm.restore(); }); @@ -19,107 +19,108 @@ describe('test/lib/core/logger.test.js', () => { it('should got right default config on prod env', async () => { mm.env('prod'); mm(process.env, 'EGG_LOG', ''); - mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app/config')); - app = utils.app('apps/mock-production-app'); + mm(process.env, 'HOME', getFilepath('apps/mock-production-app/config')); + app = createApp('apps/mock-production-app'); await app.ready(); // 生产环境默认 _level = info - assert(app.logger.get('file').options.level === Logger.INFO); + assert((app.logger.get('file') as any).options.level === levels.INFO); // stdout 默认 INFO - assert(app.logger.get('console').options.level === Logger.INFO); - assert(app.coreLogger.get('file').options.level === Logger.INFO); - assert(app.coreLogger.get('console').options.level === Logger.INFO); + assert((app.logger.get('console') as any).options.level === levels.INFO); + assert((app.coreLogger.get('file') as any).options.level === levels.INFO); + assert((app.coreLogger.get('console') as any).options.level === levels.INFO); assert(app.config.logger.disableConsoleAfterReady === true); }); it('should got right level on prod env when set allowDebugAtProd to true', async () => { mm.env('prod'); mm(process.env, 'EGG_LOG', ''); - mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app-do-not-force/config')); - app = utils.app('apps/mock-production-app-do-not-force'); + mm(process.env, 'HOME', getFilepath('apps/mock-production-app-do-not-force/config')); + app = createApp('apps/mock-production-app-do-not-force'); await app.ready(); assert(app.config.logger.allowDebugAtProd === true); - assert(app.logger.get('file').options.level === Logger.DEBUG); - assert(app.logger.get('console').options.level === Logger.INFO); - assert(app.coreLogger.get('file').options.level === Logger.DEBUG); - assert(app.coreLogger.get('console').options.level === Logger.INFO); + assert((app.logger.get('file') as any).options.level === levels.DEBUG); + assert((app.logger.get('console') as any).options.level === levels.INFO); + assert((app.coreLogger.get('file') as any).options.level === levels.DEBUG); + assert((app.coreLogger.get('console') as any).options.level === levels.INFO); }); it('should got right level on local env', async () => { mm.env('local'); mm(process.env, 'EGG_LOG', ''); - app = utils.app('apps/mock-dev-app'); + app = createApp('apps/mock-dev-app'); await app.ready(); - assert(app.logger.get('file').options.level === Logger.INFO); - assert(app.logger.get('console').options.level === Logger.INFO); - assert(app.coreLogger.get('file').options.level === Logger.INFO); - assert(app.coreLogger.get('console').options.level === Logger.WARN); + assert((app.logger.get('file') as any).options.level === levels.INFO); + assert((app.logger.get('console') as any).options.level === levels.INFO); + assert((app.coreLogger.get('file') as any).options.level === levels.INFO); + assert((app.coreLogger.get('console') as any).options.level === levels.WARN); assert(app.config.logger.disableConsoleAfterReady === false); }); it('should set EGG_LOG level on local env', async () => { mm.env('local'); mm(process.env, 'EGG_LOG', 'ERROR'); - app = utils.app('apps/mock-dev-app'); + app = createApp('apps/mock-dev-app'); await app.ready(); - assert(app.logger.get('file').options.level === Logger.INFO); - assert(app.logger.get('console').options.level === Logger.ERROR); - assert(app.coreLogger.get('file').options.level === Logger.INFO); - assert(app.coreLogger.get('console').options.level === Logger.ERROR); + assert((app.logger.get('file') as any).options.level === levels.INFO); + assert((app.logger.get('console') as any).options.level === levels.ERROR); + assert((app.coreLogger.get('file') as any).options.level === levels.INFO); + assert((app.coreLogger.get('console') as any).options.level === levels.ERROR); assert(app.config.logger.disableConsoleAfterReady === false); }); it('should got right config on unittest env', async () => { mm.env('unittest'); mm(process.env, 'EGG_LOG', ''); - app = utils.app('apps/mock-dev-app'); + app = createApp('apps/mock-dev-app'); await app.ready(); - assert(app.logger.get('file').options.level === Logger.INFO); - assert(app.logger.get('console').options.level === Logger.WARN); - assert(app.coreLogger.get('file').options.level === Logger.INFO); - assert(app.coreLogger.get('console').options.level === Logger.WARN); + assert((app.logger.get('file') as any).options.level === levels.INFO); + assert((app.logger.get('console') as any).options.level === levels.WARN); + assert((app.coreLogger.get('file') as any).options.level === levels.INFO); + assert((app.coreLogger.get('console') as any).options.level === levels.WARN); assert(app.config.logger.disableConsoleAfterReady === false); }); it('should set log.consoleLevel to env.EGG_LOG', async () => { mm(process.env, 'EGG_LOG', 'ERROR'); - app = utils.app('apps/mock-dev-app'); + app = createApp('apps/mock-dev-app'); await app.ready(); - assert(app.logger.get('file').options.level === Logger.INFO); - assert(app.logger.get('console').options.level === Logger.ERROR); + assert((app.logger.get('file') as any).options.level === levels.INFO); + assert((app.logger.get('console') as any).options.level === levels.ERROR); return app.ready(); }); it('log buffer disable cache on local and unittest env', async () => { mm(process.env, 'EGG_LOG', 'NONE'); - app = utils.app('apps/nobuffer-logger'); + app = createApp('apps/nobuffer-logger'); await app.ready(); assert(app.config.logger.disableConsoleAfterReady === false); const ctx = app.mockContext(); const logfile = path.join(app.config.logger.dir, 'common-error.log'); // app.config.logger.buffer.should.equal(false); - ctx.logger.error(new Error('mock nobuffer error')); + ctx.logger.error(new Error('mock nobuffer error on logger')); + ctx.coreLogger.error(new Error('mock nobuffer error on coreLogger')); await scheduler.wait(1000); if (process.platform !== 'darwin') { // skip check on macOS - assert( - fs.readFileSync(logfile, 'utf8').includes('nodejs.Error: mock nobuffer error\n') - ); + const content = fs.readFileSync(logfile, 'utf8'); + assert.match(content, /nodejs\.Error: mock nobuffer error on logger/); + assert.match(content, /nodejs\.Error: mock nobuffer error on coreLogger/); } }); it('log buffer enable cache on non-local and non-unittest env', async () => { mm(process.env, 'EGG_LOG', 'none'); mm.env('prod'); - mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app/config')); - app = utils.app('apps/mock-production-app'); + mm(process.env, 'HOME', getFilepath('apps/mock-production-app/config')); + app = createApp('apps/mock-production-app'); await app.ready(); assert(app.config.logger.disableConsoleAfterReady === true); @@ -136,7 +137,7 @@ describe('test/lib/core/logger.test.js', () => { it('output .json format log', async () => { mm(process.env, 'EGG_LOG', 'none'); mm.env('local'); - app = utils.app('apps/logger-output-json'); + app = createApp('apps/logger-output-json'); await app.ready(); const ctx = app.mockContext(); @@ -151,7 +152,7 @@ describe('test/lib/core/logger.test.js', () => { it('dont output to console after app ready', done => { mm.env('default'); - app = utils.cluster('apps/logger'); + app = cluster('apps/logger'); app .debug(false) .coverage(false) @@ -165,7 +166,7 @@ describe('test/lib/core/logger.test.js', () => { it('should still output to console after app ready on local env', done => { mm.env('local'); - app = utils.cluster('apps/logger'); + app = cluster('apps/logger'); app // .debug() .coverage(false) @@ -178,15 +179,15 @@ describe('test/lib/core/logger.test.js', () => { }); it('agent and app error should output to common-error.log', done => { - const baseDir = utils.getFilepath('apps/logger'); + const baseDir = getFilepath('apps/logger'); mm.env('default'); mm(process.env, 'EGG_LOG', 'none'); mm(process.env, 'EGG_HOME', baseDir); - app = utils.cluster('apps/logger'); + app = cluster('apps/logger'); app // .debug() .coverage(false) - .end(async err => { + .end(async (err: any) => { await scheduler.wait(1000); assert(!err); const content = fs.readFileSync(path.join(baseDir, 'logs/logger/common-error.log'), 'utf8'); @@ -197,7 +198,7 @@ describe('test/lib/core/logger.test.js', () => { }); it('all loggers error should redirect to errorLogger', async () => { - app = utils.app('apps/logger'); + app = createApp('apps/logger'); await app.ready(); app.logger.error(new Error('logger error')); @@ -215,14 +216,14 @@ describe('test/lib/core/logger.test.js', () => { }); it('agent\'s logger is same as coreLogger', async () => { - app = utils.app('apps/logger'); + app = createApp('apps/logger'); await app.ready(); assert(app.agent.logger.options.file === app.agent.coreLogger.options.file); }); it('should `config.logger.enableFastContextLogger` = true work', async () => { - app = utils.app('apps/app-enableFastContextLogger'); + app = createApp('apps/app-enableFastContextLogger'); await app.ready(); app.mockContext({ tracer: { @@ -240,9 +241,9 @@ describe('test/lib/core/logger.test.js', () => { }); describe('logger.level = DEBUG', () => { - let app; + let app: MockApplication; before(() => { - app = utils.app('apps/logger-level-debug'); + app = createApp('apps/logger-level-debug'); return app.ready(); }); after(() => app.close()); @@ -254,7 +255,7 @@ describe('test/lib/core/logger.test.js', () => { .end(err => { assert(!err); assert( - fs.readFileSync(path.join(app.config.baseDir, 'logs/foo/foo-web.log'), 'utf8').includes(' DEBUG ') + fs.readFileSync(path.join(app.config.baseDir, 'logs/foo/foo-web.log'), 'utf8').includes(' DEBUG '), ); done(); }); @@ -262,9 +263,9 @@ describe('test/lib/core/logger.test.js', () => { }); describe('onelogger', () => { - let app; + let app: MockApplication; before(() => { - app = utils.app('apps/custom-logger'); + app = createApp('apps/custom-logger'); return app.ready(); }); after(() => app.close());