-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: run in egg cluster mode (#1645)
- Loading branch information
1 parent
eb241f2
commit d6146cc
Showing
19 changed files
with
373 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as Master from 'egg-cluster/lib/master'; | ||
import { join } from 'path'; | ||
|
||
const name = process.argv[2]; | ||
|
||
const master = new Master({ | ||
baseDir: join(__dirname, 'fixtures', name), | ||
workers: 1, | ||
port: 8080, | ||
framework: join(__dirname, '../'), | ||
typescript: true, | ||
require: 'ts-node/register' | ||
}) | ||
process.on('message', (data) => { | ||
if (data?.['action'] === 'app_end') { | ||
master.close(); | ||
process.exit(0); | ||
} | ||
}); | ||
master.ready(() => { | ||
process.send({ | ||
action: 'app_ready' | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { closeCuster, createCluster, createHttpClient } from './utils'; | ||
|
||
describe('/test/cluster.test.ts', () => { | ||
describe('test new decorator', () => { | ||
let master; | ||
beforeAll(async () => { | ||
master = await createCluster('cluster/base-app'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await closeCuster(master); | ||
}); | ||
|
||
it('test setHeader decorator', async () => { | ||
const result = await createHttpClient('http://127.0.0.1:8080/set_header', { | ||
method: 'get', | ||
params: { name: 'harry' } | ||
}) | ||
expect(result.status).toEqual(200); | ||
expect(result.data).toEqual('bbb'); | ||
expect(result.headers['bbb']).toEqual('aaa'); | ||
expect(result.headers['ccc']).toEqual('ddd'); | ||
}); | ||
|
||
it('test get status 204', async () => { | ||
const result = await createHttpClient('http://127.0.0.1:8080/204'); | ||
expect(result.status).toEqual(204); | ||
}); | ||
|
||
it('test get method with return value', async () => { | ||
const result = await createHttpClient('http://127.0.0.1:8080/', { | ||
method: 'get', | ||
params: { name: 'harry' } | ||
}) | ||
expect(result.status).toEqual(201); | ||
expect(result.data).toEqual('hello world,harry'); | ||
}); | ||
|
||
it('test get method with redirect', async () => { | ||
const result = await createHttpClient('http://127.0.0.1:8080/login', { | ||
maxRedirects: 0, | ||
}).catch(err => { | ||
return err.response.status; | ||
}); | ||
expect(result).toEqual(302); | ||
}); | ||
|
||
it('test get data with ctx.body', async () => { | ||
const result = await createHttpClient('http://127.0.0.1:8080/ctx-body'); | ||
expect(result.data).toEqual('ctx-body'); | ||
}); | ||
}); | ||
// | ||
// it('should test global use midway middleware id in egg', async () => { | ||
// const app = await creatApp('feature/base-app-middleware'); | ||
// const result = await createHttpRequest(app).get('/'); | ||
// expect(result.text).toEqual('11112222333344445555egg_middleware'); | ||
// await closeApp(app); | ||
// }); | ||
// | ||
// it('should got component config in app', async () => { | ||
// const app = await creatApp('feature/base-app-component-config'); | ||
// const result = await createHttpRequest(app).get('/'); | ||
// expect(result.text).toEqual('hello world1'); | ||
// await closeApp(app); | ||
// }); | ||
// | ||
// it('should got plugin in app middleware and controller', async () => { | ||
// const app = await creatApp('feature/base-app-plugin-inject'); | ||
// const result = await createHttpRequest(app).get('/'); | ||
// expect(result.text).toEqual('hello worldaaaa1aaaa'); | ||
// await closeApp(app); | ||
// }); | ||
// | ||
// it('should test set custom logger in egg by midway logger', async () => { | ||
// await remove(join(__dirname, 'fixtures/feature/base-app-set-ctx-logger', 'logs')); | ||
// const app = await creatApp('feature/base-app-set-ctx-logger'); | ||
// const result = await createHttpRequest(app) | ||
// .get('/') | ||
// .query({ name: 'harry' }); | ||
// expect(result.status).toEqual(200); | ||
// expect(result.text).toEqual('hello world,harry'); | ||
// await sleep(); | ||
// expect(matchContentTimes(join(app.getAppDir(), 'logs', 'ali-demo', 'midway-web.log'), 'GET /] aaaaa')).toEqual(3); | ||
// expect(matchContentTimes(join(app.getAppDir(), 'logs', 'ali-demo', 'midway-web.log'), 'abcde] custom label')).toEqual(1); | ||
// await closeApp(app); | ||
// }); | ||
// | ||
// it('should use midway logger ignore replaceEggLogger config', async () => { | ||
// const app = await creatApp('feature/base-app-egg-logger'); | ||
// await closeApp(app); | ||
// }); | ||
// | ||
// it('should test not found will got 404', async () => { | ||
// const app = await creatApp('feature/base-app-404'); | ||
// const result = await createHttpRequest(app) | ||
// .get('/error'); | ||
// expect(result.status).toEqual(404); | ||
// await closeApp(app); | ||
// }); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/web/test/fixtures/cluster/base-app/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
export const keys = 'key'; | ||
|
||
export const hello = { | ||
a: 1, | ||
b: 2, | ||
d: [1, 2, 3], | ||
}; | ||
|
||
export const midwayFeature = { | ||
replaceEggLogger: true, | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/web/test/fixtures/cluster/base-app/src/config/config.unittest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
exports.hello = { | ||
b: 4, | ||
c: 3, | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/web/test/fixtures/cluster/base-app/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Configuration } from '@midwayjs/decorator'; | ||
import { join } from 'path'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
require('../../../../../') | ||
], | ||
importConfigs: [ | ||
join(__dirname, './config') | ||
] | ||
}) | ||
export class ContainerConfiguration { | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/web/test/fixtures/cluster/base-app/src/controller/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
Controller, | ||
Post, | ||
Get, | ||
Provide, | ||
Inject, | ||
Query, | ||
Body, | ||
HttpCode, | ||
Redirect, | ||
SetHeader, | ||
} from '@midwayjs/decorator'; | ||
import { UserService } from '../service/user'; | ||
|
||
@Provide() | ||
@Controller('/') | ||
export class APIController { | ||
@Inject() | ||
ctx: any; | ||
|
||
@Inject() | ||
userService: UserService; | ||
|
||
@Post() | ||
async postData(@Body('bbbbb') bbbb) { | ||
return 'data'; | ||
} | ||
|
||
@Get('/set_header') | ||
@SetHeader('bbb', 'aaa') | ||
@SetHeader({ | ||
'ccc': 'ddd' | ||
}) | ||
async homeSet() { | ||
return 'bbb'; | ||
} | ||
|
||
@Get('/', { middleware: [] }) | ||
@HttpCode(201) | ||
async home(@Query('name') name: string) { | ||
return 'hello world,' + name; | ||
} | ||
|
||
@Get('/login') | ||
@Redirect('/') | ||
async redirect() { | ||
} | ||
|
||
@Get('/204') | ||
async r204() { | ||
// | ||
} | ||
|
||
@Get('/ctx-body') | ||
async getCtxBody() { | ||
this.ctx.body = 'ctx-body'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/web/test/fixtures/cluster/base-app/src/service/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Provide } from '@midwayjs/decorator'; | ||
|
||
@Provide() | ||
export class UserService { | ||
async hello(name) { | ||
return { | ||
name, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/web/test/fixtures/issue/base-app/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
export const keys = 'key'; | ||
|
||
export const hello = { | ||
a: 1, | ||
b: 2, | ||
d: [1, 2, 3], | ||
}; | ||
|
||
export const midwayFeature = { | ||
replaceEggLogger: true, | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/web/test/fixtures/issue/base-app/src/config/config.unittest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
exports.hello = { | ||
b: 4, | ||
c: 3, | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/web/test/fixtures/issue/base-app/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Configuration } from '@midwayjs/decorator'; | ||
import { join } from 'path'; | ||
|
||
@Configuration({ | ||
importConfigs: [ | ||
join(__dirname, './config') | ||
] | ||
}) | ||
export class ContainerConfiguration { | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/web/test/fixtures/issue/base-app/src/controller/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
Controller, | ||
Post, | ||
Get, | ||
Provide, | ||
Inject, | ||
Query, | ||
Body, | ||
HttpCode, | ||
Redirect, | ||
SetHeader, | ||
} from '@midwayjs/decorator'; | ||
import { UserService } from '../service/user'; | ||
|
||
@Provide() | ||
@Controller('/') | ||
export class APIController { | ||
@Inject() | ||
ctx: any; | ||
|
||
@Inject() | ||
userService: UserService; | ||
|
||
@Post() | ||
async postData(@Body('bbbbb') bbbb) { | ||
return 'data'; | ||
} | ||
|
||
@Get('/set_header') | ||
@SetHeader('bbb', 'aaa') | ||
@SetHeader({ | ||
'ccc': 'ddd' | ||
}) | ||
async homeSet() { | ||
return 'bbb'; | ||
} | ||
|
||
@Get('/', { middleware: [] }) | ||
@HttpCode(201) | ||
async home(@Query('name') name: string) { | ||
return 'hello world,' + name; | ||
} | ||
|
||
@Get('/login') | ||
@Redirect('/') | ||
async redirect() { | ||
} | ||
|
||
@Get('/204') | ||
async r204() { | ||
// | ||
} | ||
|
||
@Get('/ctx-body') | ||
async getCtxBody() { | ||
this.ctx.body = 'ctx-body'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/web/test/fixtures/issue/base-app/src/service/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Provide } from '@midwayjs/decorator'; | ||
|
||
@Provide() | ||
export class UserService { | ||
async hello(name) { | ||
return { | ||
name, | ||
}; | ||
} | ||
} |
Oops, something went wrong.