11import assert from 'assert' ;
22import path from 'path' ;
3- import mm from 'egg-mock' ;
3+ import mm , { MockApplication } from 'egg-mock' ;
44import { TimerUtil } from '@eggjs/tegg-common-util' ;
55import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService' ;
66import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger' ;
77
88describe ( 'test/eventbus.test.ts' , ( ) => {
9- let app ;
10- let ctx ;
9+ let app : MockApplication ;
1110
1211 afterEach ( async ( ) => {
1312 mm . restore ( ) ;
@@ -30,85 +29,86 @@ describe('test/eventbus.test.ts', () => {
3029 } ) ;
3130
3231 it ( 'msg should work' , async ( ) => {
33- ctx = await app . mockModuleContext ( ) ;
34- const helloService = await ctx . getEggObject ( HelloService ) ;
35- let msg : string | undefined ;
36- // helloLogger is in child context
37- mm ( HelloLogger . prototype , 'handle' , m => {
38- msg = m ;
32+ await app . mockModuleContextScope ( async ctx => {
33+ const helloService = await ctx . getEggObject ( HelloService ) ;
34+ let msg : string | undefined ;
35+ // helloLogger is in child context
36+ mm ( HelloLogger . prototype , 'handle' , m => {
37+ msg = m ;
38+ } ) ;
39+ const eventWaiter = await app . getEventWaiter ( ) ;
40+ const helloEvent = eventWaiter . await ( 'helloEgg' ) ;
41+ helloService . hello ( ) ;
42+ await helloEvent ;
43+ assert ( msg === '01' ) ;
3944 } ) ;
40- const eventWaiter = await app . getEventWaiter ( ) ;
41- const helloEvent = eventWaiter . await ( 'helloEgg' ) ;
42- helloService . hello ( ) ;
43- await helloEvent ;
44- assert ( msg === '01' ) ;
4545 } ) ;
4646
4747 it ( 'cork/uncork should work' , async ( ) => {
48- ctx = await app . mockModuleContext ( ) ;
49-
50- const helloService = await ctx . getEggObject ( HelloService ) ;
51- let helloTime = 0 ;
52- // helloLogger is in child context
53- mm ( HelloLogger . prototype , 'handle' , ( ) => {
54- helloTime = Date . now ( ) ;
48+ await app . mockModuleContextScope ( async ctx => {
49+ const helloService = await ctx . getEggObject ( HelloService ) ;
50+ let helloTime = 0 ;
51+ // helloLogger is in child context
52+ mm ( HelloLogger . prototype , 'handle' , ( ) => {
53+ helloTime = Date . now ( ) ;
54+ } ) ;
55+ helloService . cork ( ) ;
56+ const triggerTime = Date . now ( ) ;
57+ helloService . hello ( ) ;
58+
59+ await TimerUtil . sleep ( 100 ) ;
60+ helloService . uncork ( ) ;
61+
62+ const eventWaiter = await app . getEventWaiter ( ) ;
63+ const helloEvent = eventWaiter . await ( 'helloEgg' ) ;
64+ await helloEvent ;
65+ assert ( helloTime >= triggerTime + 100 ) ;
5566 } ) ;
56- helloService . cork ( ) ;
57- const triggerTime = Date . now ( ) ;
58- helloService . hello ( ) ;
59-
60- await TimerUtil . sleep ( 100 ) ;
61- helloService . uncork ( ) ;
62-
63- const eventWaiter = await app . getEventWaiter ( ) ;
64- const helloEvent = eventWaiter . await ( 'helloEgg' ) ;
65- await helloEvent ;
66- assert ( helloTime >= triggerTime + 100 ) ;
6767 } ) ;
6868
6969 it ( 'can call cork/uncork multi times' , async ( ) => {
70- ctx = await app . mockModuleContext ( ) ;
71-
72- const helloService = await ctx . getEggObject ( HelloService ) ;
73- const eventWaiter = await app . getEventWaiter ( ) ;
74-
75- let helloCalled = 0 ;
76- // helloLogger is in child context
77- mm ( HelloLogger . prototype , 'handle' , ( ) => {
78- helloCalled ++ ;
70+ await app . mockModuleContextScope ( async ctx => {
71+ const helloService = await ctx . getEggObject ( HelloService ) ;
72+ const eventWaiter = await app . getEventWaiter ( ) ;
73+
74+ let helloCalled = 0 ;
75+ // helloLogger is in child context
76+ mm ( HelloLogger . prototype , 'handle' , ( ) => {
77+ helloCalled ++ ;
78+ } ) ;
79+ helloService . cork ( ) ;
80+ helloService . hello ( ) ;
81+ helloService . uncork ( ) ;
82+ await eventWaiter . await ( 'helloEgg' ) ;
83+
84+ helloService . cork ( ) ;
85+ helloService . hello ( ) ;
86+ helloService . uncork ( ) ;
87+ await eventWaiter . await ( 'helloEgg' ) ;
88+
89+ assert ( helloCalled === 2 ) ;
7990 } ) ;
80- helloService . cork ( ) ;
81- helloService . hello ( ) ;
82- helloService . uncork ( ) ;
83- await eventWaiter . await ( 'helloEgg' ) ;
84-
85- helloService . cork ( ) ;
86- helloService . hello ( ) ;
87- helloService . uncork ( ) ;
88- await eventWaiter . await ( 'helloEgg' ) ;
89-
90- assert ( helloCalled === 2 ) ;
9191 } ) ;
9292
9393 it ( 'reentry cork/uncork should work' , async ( ) => {
94- ctx = await app . mockModuleContext ( ) ;
95-
96- const helloService = await ctx . getEggObject ( HelloService ) ;
97- const eventWaiter = await app . getEventWaiter ( ) ;
98-
99- let helloCalled = 0 ;
100- // helloLogger is in child context
101- mm ( HelloLogger . prototype , 'handle' , ( ) => {
102- helloCalled ++ ;
94+ await app . mockModuleContextScope ( async ctx => {
95+ const helloService = await ctx . getEggObject ( HelloService ) ;
96+ const eventWaiter = await app . getEventWaiter ( ) ;
97+
98+ let helloCalled = 0 ;
99+ // helloLogger is in child context
100+ mm ( HelloLogger . prototype , 'handle' , ( ) => {
101+ helloCalled ++ ;
102+ } ) ;
103+ helloService . cork ( ) ;
104+ helloService . cork ( ) ;
105+ helloService . hello ( ) ;
106+ helloService . uncork ( ) ;
107+ helloService . uncork ( ) ;
108+ await eventWaiter . await ( 'helloEgg' ) ;
109+
110+ assert ( helloCalled === 1 ) ;
103111 } ) ;
104- helloService . cork ( ) ;
105- helloService . cork ( ) ;
106- helloService . hello ( ) ;
107- helloService . uncork ( ) ;
108- helloService . uncork ( ) ;
109- await eventWaiter . await ( 'helloEgg' ) ;
110-
111- assert ( helloCalled === 1 ) ;
112112 } ) ;
113113
114114 it ( 'concurrent cork/uncork should work' , async ( ) => {
0 commit comments