Skip to content

Commit

Permalink
fix: after call mockModuleContext, hasMockModuleContext should be true (
Browse files Browse the repository at this point in the history
#134)

* fix: after call mockModuleContext, hasMockModuleContext should be true

* f
  • Loading branch information
popomore committed Jul 13, 2023
1 parent 6039fbc commit 88b3caa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 70 deletions.
138 changes: 69 additions & 69 deletions plugin/eventbus/test/eventbus.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import assert from 'assert';
import path from 'path';
import mm from 'egg-mock';
import mm, { MockApplication } from 'egg-mock';
import { TimerUtil } from '@eggjs/tegg-common-util';
import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService';
import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger';

describe('test/eventbus.test.ts', () => {
let app;
let ctx;
let app: MockApplication;

afterEach(async () => {
mm.restore();
Expand All @@ -30,85 +29,86 @@ describe('test/eventbus.test.ts', () => {
});

it('msg should work', async () => {
ctx = await app.mockModuleContext();
const helloService = await ctx.getEggObject(HelloService);
let msg: string | undefined;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', m => {
msg = m;
await app.mockModuleContextScope(async ctx => {
const helloService = await ctx.getEggObject(HelloService);
let msg: string | undefined;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', m => {
msg = m;
});
const eventWaiter = await app.getEventWaiter();
const helloEvent = eventWaiter.await('helloEgg');
helloService.hello();
await helloEvent;
assert(msg === '01');
});
const eventWaiter = await app.getEventWaiter();
const helloEvent = eventWaiter.await('helloEgg');
helloService.hello();
await helloEvent;
assert(msg === '01');
});

it('cork/uncork should work', async () => {
ctx = await app.mockModuleContext();

const helloService = await ctx.getEggObject(HelloService);
let helloTime = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloTime = Date.now();
await app.mockModuleContextScope(async ctx => {
const helloService = await ctx.getEggObject(HelloService);
let helloTime = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloTime = Date.now();
});
helloService.cork();
const triggerTime = Date.now();
helloService.hello();

await TimerUtil.sleep(100);
helloService.uncork();

const eventWaiter = await app.getEventWaiter();
const helloEvent = eventWaiter.await('helloEgg');
await helloEvent;
assert(helloTime >= triggerTime + 100);
});
helloService.cork();
const triggerTime = Date.now();
helloService.hello();

await TimerUtil.sleep(100);
helloService.uncork();

const eventWaiter = await app.getEventWaiter();
const helloEvent = eventWaiter.await('helloEgg');
await helloEvent;
assert(helloTime >= triggerTime + 100);
});

it('can call cork/uncork multi times', async () => {
ctx = await app.mockModuleContext();

const helloService = await ctx.getEggObject(HelloService);
const eventWaiter = await app.getEventWaiter();

let helloCalled = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloCalled++;
await app.mockModuleContextScope(async ctx => {
const helloService = await ctx.getEggObject(HelloService);
const eventWaiter = await app.getEventWaiter();

let helloCalled = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloCalled++;
});
helloService.cork();
helloService.hello();
helloService.uncork();
await eventWaiter.await('helloEgg');

helloService.cork();
helloService.hello();
helloService.uncork();
await eventWaiter.await('helloEgg');

assert(helloCalled === 2);
});
helloService.cork();
helloService.hello();
helloService.uncork();
await eventWaiter.await('helloEgg');

helloService.cork();
helloService.hello();
helloService.uncork();
await eventWaiter.await('helloEgg');

assert(helloCalled === 2);
});

it('reentry cork/uncork should work', async () => {
ctx = await app.mockModuleContext();

const helloService = await ctx.getEggObject(HelloService);
const eventWaiter = await app.getEventWaiter();

let helloCalled = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloCalled++;
await app.mockModuleContextScope(async ctx => {
const helloService = await ctx.getEggObject(HelloService);
const eventWaiter = await app.getEventWaiter();

let helloCalled = 0;
// helloLogger is in child context
mm(HelloLogger.prototype, 'handle', () => {
helloCalled++;
});
helloService.cork();
helloService.cork();
helloService.hello();
helloService.uncork();
helloService.uncork();
await eventWaiter.await('helloEgg');

assert(helloCalled === 1);
});
helloService.cork();
helloService.cork();
helloService.hello();
helloService.uncork();
helloService.uncork();
await eventWaiter.await('helloEgg');

assert(helloCalled === 1);
});

it('concurrent cork/uncork should work', async () => {
Expand Down
4 changes: 3 additions & 1 deletion plugin/tegg/app/extend/application.unittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ let hasMockModuleContext = false;

export default {
async mockModuleContext(this: MockApplication, data?: any): Promise<Context> {
this.deprecate('app.mockModuleContext is deprecated, use mockModuleContextScope.');
if (hasMockModuleContext) {
throw new Error('should not call mockModuleContext twice, should use mockModuleContextScope.');
throw new Error('should not call mockModuleContext twice.');
}
const ctx = this.mockContext(data);
const teggCtx = new EggContextImpl(ctx);
Expand All @@ -19,6 +20,7 @@ export default {
if (teggCtx.init) {
await teggCtx.init(lifecycle);
}
hasMockModuleContext = true;
return ctx;
},

Expand Down
12 changes: 12 additions & 0 deletions plugin/tegg/test/app/extend/application.unittest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ describe('test/app/extend/application.unittest.test.ts', () => {
assert(traceId);
});
});

it('should not call mockModuleContext twice', async () => {
const ctx = await app.mockModuleContext();
try {
await assert.rejects(
app.mockModuleContext(),
/should not call mockModuleContext twice./,
);
} finally {
await app.destroyModuleContext(ctx);
}
});
});

0 comments on commit 88b3caa

Please sign in to comment.