Skip to content

Commit

Permalink
fix: run in egg cluster mode (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jan 30, 2022
1 parent eb241f2 commit d6146cc
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/context/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class MidwayContainer implements IMidwayContainer, IModuleStore {
configuration.load(module);
for (const ns of configuration.getNamespaceList()) {
this.namespaceSet.add(ns);
debug(`[core]: load configuration in namespace="${ns} complete"`);
debug(`[core]: load configuration in namespace="${ns}" complete`);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
"@midwayjs/decorator": "^3.0.2",
"@midwayjs/logger": "^2.14.0",
"@midwayjs/mock": "^3.0.2",
"axios": "^0.25.0",
"dayjs": "1.10.7",
"egg-logger": "2.7.1",
"egg-mock": "4.2.0",
"egg-scripts": "2.15.2",
"egg-socket.io": "4.1.6",
"egg-view-nunjucks": "2.3.0",
"fake-egg": "1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export const createAppWorkerLoader = () => {
appDir: this.appDir,
baseDir: this.baseDir,
ignore: ['**/app/extend/**'],
}).then(r => {
application: this.app,
}).then(_ => {
debug('[egg]: global context: init complete');
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/web/src/framework/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export class MidwayWebFramework extends BaseFramework<
async applicationInitialize(options: Partial<IMidwayBootstrapOptions>) {
if (!this.isClusterMode) {
await this.initSingleProcessEgg();
} else {
// get app in cluster mode
this.app = options['application'];
}

// not found middleware
Expand Down
24 changes: 24 additions & 0 deletions packages/web/test/child.ts
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'
});
});
102 changes: 102 additions & 0 deletions packages/web/test/cluster.test.ts
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);
// });

});
3 changes: 3 additions & 0 deletions packages/web/test/fixtures/cluster/base-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
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,
}
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 packages/web/test/fixtures/cluster/base-app/src/configuration.ts
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 packages/web/test/fixtures/cluster/base-app/src/controller/api.ts
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 packages/web/test/fixtures/cluster/base-app/src/service/user.ts
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,
};
}
}
3 changes: 3 additions & 0 deletions packages/web/test/fixtures/issue/base-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
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,
}
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 packages/web/test/fixtures/issue/base-app/src/configuration.ts
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 packages/web/test/fixtures/issue/base-app/src/controller/api.ts
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 packages/web/test/fixtures/issue/base-app/src/service/user.ts
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,
};
}
}
Loading

0 comments on commit d6146cc

Please sign in to comment.