Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,9 @@ export class HelloService {
```typescript
export interface Application {
/**
* 创建 module 上下文
* 创建 module 上下文 scope
*/
mockModuleContext(data?: any): Promise <Context> ;
/**
* 销毁 module 上下文
*/
destroyModuleContext(context: Context): Promise <void> ;
mockModuleContextScope<R=any>(this: MockApplication, fn: (ctx: Context) => Promise<R>, data?: any): Promise<R>;
}
```

Expand Down
2 changes: 1 addition & 1 deletion plugin/aop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@eggjs/tegg-config": "^3.1.0",
"@eggjs/tegg-plugin": "^3.1.0",
"egg": "^3.9.1",
"egg-mock": "^4.0.1"
"egg-mock": "^5.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion plugin/controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@eggjs/tegg-config": "^3.1.0",
"@eggjs/tegg-plugin": "^3.1.0",
"egg": "^3.9.1",
"egg-mock": "^3.25.1",
"egg-mock": "^5.5.0",
"egg-tracer": "^1.1.0",
"koa-router": "^8.0.8"
},
Expand Down
2 changes: 1 addition & 1 deletion plugin/eventbus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@eggjs/tegg-plugin": "^3.1.0",
"await-event": "^2.1.0",
"egg": "^3.9.1",
"egg-mock": "^4.0.1",
"egg-mock": "^5.5.0",
"mz-modules": "^2.1.0"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion plugin/orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@eggjs/tegg-config": "^3.1.0",
"@eggjs/tegg-plugin": "^3.1.0",
"egg": "^3.9.1",
"egg-mock": "^3.25.1",
"egg-mock": "^5.5.0",
"egg-tracer": "^1.1.0",
"koa-router": "^8.0.8",
"mysql": "^2.18.1"
Expand Down
2 changes: 1 addition & 1 deletion plugin/schedule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@eggjs/tegg-config": "^3.1.0",
"@eggjs/tegg-plugin": "^3.1.0",
"egg": "^3.9.1",
"egg-mock": "^3.25.1",
"egg-mock": "^5.5.0",
"egg-schedule": "^4.0.0",
"mz-modules": "^2.1.0"
},
Expand Down
23 changes: 8 additions & 15 deletions plugin/tegg/app/extend/application.unittest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import mm, { MockApplication } from 'egg-mock';
import { MockApplication } from 'egg-mock';
import { Context } from 'egg';
import { EggContextImpl } from '../../lib/EggContextImpl';
import { ContextHandler, EggContext, EggContextLifecycleContext } from '@eggjs/tegg-runtime';
import { TEGG_CONTEXT } from '@eggjs/egg-module-common';
import { TEggPluginContext } from './context';
import { EggContext, EggContextLifecycleContext } from '@eggjs/tegg-runtime';

const TEGG_LIFECYCLE_CACHE: Map<EggContext, EggContextLifecycleContext> = new Map();

Expand All @@ -14,11 +12,8 @@ export default {
if (hasMockModuleContext) {
throw new Error('should not call mockModuleContext twice, should use mockModuleContextScope.');
}
const ctx = this.mockContext(data) as TEggPluginContext;
const ctx = this.mockContext(data);
const teggCtx = new EggContextImpl(ctx);
mm(ContextHandler, 'getContext', () => {
return teggCtx;
});
const lifecycle = {};
TEGG_LIFECYCLE_CACHE.set(teggCtx, lifecycle);
if (teggCtx.init) {
Expand All @@ -30,8 +25,7 @@ export default {
async destroyModuleContext(ctx: Context) {
hasMockModuleContext = false;

const teggPluginCtx = ctx as TEggPluginContext;
const teggCtx = teggPluginCtx[TEGG_CONTEXT];
const teggCtx = ctx.teggContext;
if (!teggCtx) {
return;
}
Expand All @@ -41,13 +35,12 @@ export default {
}
},

async moduleModuleContextScope<R=any>(this: MockApplication, fn: (ctx: Context) => Promise<R>, data?: any): Promise<R> {
async mockModuleContextScope<R=any>(this: MockApplication, fn: (ctx: Context) => Promise<R>, data?: any): Promise<R> {
if (hasMockModuleContext) {
throw new Error('mockModuleContextScope can not use with mockModuleContext, should use mockModuleContextScope only.');
}
const ctx = this.mockContext(data);
const teggCtx = new EggContextImpl(ctx);
return await ContextHandler.run<R>(teggCtx, async () => {
return this.mockContextScope(async ctx => {
const teggCtx = new EggContextImpl(ctx);
const lifecycle = {};
if (teggCtx.init) {
await teggCtx.init(lifecycle);
Expand All @@ -57,6 +50,6 @@ export default {
} finally {
await teggCtx.destroy(lifecycle);
}
});
}, data);
},
};
7 changes: 3 additions & 4 deletions plugin/tegg/lib/EggContextHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Application } from 'egg';
import { ContextHandler, EggContext } from '@eggjs/tegg-runtime';
import { EGG_CONTEXT, TEGG_CONTEXT } from '@eggjs/egg-module-common';
import { EGG_CONTEXT } from '@eggjs/egg-module-common';

export class EggContextHandler {
private readonly app: Application;
Expand All @@ -10,9 +10,8 @@ export class EggContextHandler {
}

getContextCallback(): EggContext {
// TODO
const ctx = (this.app as any).currentContext;
return ctx && ctx[TEGG_CONTEXT];
const ctx = this.app.currentContext;
return ctx && ctx.teggContext;
}

async run<R>(eggContext: EggContext, fn: () => Promise<R>): Promise<R> {
Expand Down
2 changes: 1 addition & 1 deletion plugin/tegg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"devDependencies": {
"@eggjs/tegg-config": "^3.1.0",
"egg": "^3.9.1",
"egg-mock": "^5.4.0",
"egg-mock": "^5.5.0",
"egg-schedule": "^4.0.0",
"egg-tracer": "^1.1.0",
"mz-modules": "^2.1.0"
Expand Down
18 changes: 10 additions & 8 deletions plugin/tegg/test/AccessLevelCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ describe('test/AccessLevelCheck.test.ts', () => {
});

it('should work: private has some name', async () => {
const ctx = await app.mockModuleContext();
const mainService: MainService = await ctx.getEggObject(MainService);
assert(mainService);
assert(mainService.invokeFoo() === 'moduleMain-FooService-Method');
await app.mockModuleContextScope(async ctx => {
const mainService: MainService = await ctx.getEggObject(MainService);
assert(mainService);
assert(mainService.invokeFoo() === 'moduleMain-FooService-Method');
});
});

it('should work: public/private has some name', async () => {
const ctx = await app.mockModuleContext();
const mainService: MainService = await ctx.getEggObject(MainService);
assert(mainService);
assert(mainService.invokeBar() === 'moduleMain-BarService-Method');
await app.mockModuleContextScope(async ctx => {
const mainService: MainService = await ctx.getEggObject(MainService);
assert(mainService);
assert(mainService.invokeBar() === 'moduleMain-BarService-Method');
});
});
});
44 changes: 25 additions & 19 deletions plugin/tegg/test/EggCompatible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,28 @@ describe('test/EggCompatible.test.ts', () => {
});

it('inject config should work', async () => {
const ctx = await app.mockModuleContext();
const baseDir = app.module.multiModuleService.configService.getBaseDir();
assert(baseDir);
await app.destroyModuleContext(ctx);
await app.mockModuleContextScope(async () => {
const baseDir = app.module.multiModuleService.configService.getBaseDir();
assert(baseDir);
});
});

it('inject user should work', async () => {
app.mockUser();
const ctx = await app.mockModuleContext();
const userName = app.module.multiModuleService.configService.getCurrentUserName();
assert(userName);
await app.destroyModuleContext(ctx);
await app.mockModuleContextScope(async () => {
const userName = app.module.multiModuleService.configService.getCurrentUserName();
assert(userName);
}, {
user: {
userName: 'mock_user',
},
});
});

it('custom logger should work', async () => {
const ctx = await app.mockModuleContext();
await app.module.multiModuleService.customLoggerService.printLog();
await app.destroyModuleContext(ctx);
await app.mockModuleContextScope(async ctx => {
await app.module.multiModuleService.customLoggerService.printLog();
await app.destroyModuleContext(ctx);
});
});

it('use singleton proto should work', async () => {
Expand All @@ -109,15 +113,17 @@ describe('test/EggCompatible.test.ts', () => {
});

it('module proxy cache should work', async () => {
await app.mockModuleContext();
const moduleMultiModuleService1 = app.module.multiModuleService;
const moduleMultiModuleService2 = app.module.multiModuleService;
assert(moduleMultiModuleService1 === moduleMultiModuleService2);
await app.mockModuleContextScope(async () => {
const moduleMultiModuleService1 = app.module.multiModuleService;
const moduleMultiModuleService2 = app.module.multiModuleService;
assert(moduleMultiModuleService1 === moduleMultiModuleService2);
});
});

it('should load egg object with no side effect', async () => {
const ctx = await app.mockModuleContext();
assert(ctx.counter === 0);
assert(ctx.counter === 1);
await app.mockModuleContextScope(async ctx => {
assert(ctx.counter === 0);
assert(ctx.counter === 1);
});
});
});
9 changes: 5 additions & 4 deletions plugin/tegg/test/SameProtoName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ describe('test/SameProtoName.test.ts', () => {
});

it('should work', async () => {
const ctx = await app.mockModuleContext();
const barService = await ctx.getEggObject(BarService);
assert(barService);
assert(barService.fooService);
await app.mockModuleContextScope(async ctx => {
const barService = await ctx.getEggObject(BarService);
assert(barService);
assert(barService.fooService);
});
});
});
8 changes: 4 additions & 4 deletions plugin/tegg/test/app/extend/application.unittest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('test/app/extend/application.unittest.test.ts', () => {
});

it('should work', async function() {
const ctx = await app.mockModuleContext();
const traceId = await app.module.multiModuleService.traceService.getTraceId();
assert(traceId);
await app.destroyModuleContext(ctx);
await app.mockModuleContextScope(async () => {
const traceId = await app.module.multiModuleService.traceService.getTraceId();
assert(traceId);
});
});
});
13 changes: 6 additions & 7 deletions plugin/tegg/test/app/extend/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ describe('test/app/extend/context.test.ts', () => {

describe('getEggObject', () => {
it('should work', async () => {
const ctx = await app.mockModuleContext();
const appService = await ctx.getEggObject(AppService);
assert(appService instanceof AppService);
await app.mockModuleContextScope(async ctx => {
const appService = await ctx.getEggObject(AppService);
assert(appService instanceof AppService);

const persistenceService = await ctx.getEggObject(PersistenceService);
assert(persistenceService instanceof PersistenceService);

await app.destroyModuleContext(ctx);
const persistenceService = await ctx.getEggObject(PersistenceService);
assert(persistenceService instanceof PersistenceService);
});
});
});
});
6 changes: 3 additions & 3 deletions plugin/tegg/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application, Context } from 'egg';
import { Context } from 'egg';
import '@eggjs/tegg-config';
import { ModuleHandler } from '../lib/ModuleHandler';
import { EggPrototypeCreatorFactory } from '@eggjs/tegg-metadata';
Expand Down Expand Up @@ -48,11 +48,11 @@ declare module 'egg' {
eggPrototypeLifecycleUtil: typeof EggPrototypeLifecycleUtil;
eggContextLifecycleUtil: typeof EggContextLifecycleUtil;
eggObjectLifecycleUtil: typeof EggObjectLifecycleUtil;
teggContextStorage(): AsyncLocalStorage<EggContext>

teggContext: EggContext;
moduleHandler: ModuleHandler;

mockModuleContext(data?: any): Promise<Context>;
mockModuleContextScope<R=any>(fn: (ctx: Context) => Promise<R>, data?: any): Promise<R>;
destroyModuleContext(context: Context): Promise<void>;
// 兼容现有 module 的定义
module: EggModule & EggApplicationModule;
Expand Down