@@ -22,10 +22,12 @@ import DemoActorCounterImpl from "../../actor/DemoActorCounterImpl";
22
22
import DemoActorCounterInterface from "../../actor/DemoActorCounterInterface" ;
23
23
import DemoActorReminderImpl from "../../actor/DemoActorReminderImpl" ;
24
24
import DemoActorReminder2Impl from "../../actor/DemoActorReminder2Impl" ;
25
+ import DemoActorReminderOnceImpl from "../../actor/DemoActorReminderOnceImpl" ;
25
26
import DemoActorReminderInterface from "../../actor/DemoActorReminderInterface" ;
26
27
import DemoActorSayImpl from "../../actor/DemoActorSayImpl" ;
27
28
import DemoActorSayInterface from "../../actor/DemoActorSayInterface" ;
28
29
import DemoActorTimerImpl from "../../actor/DemoActorTimerImpl" ;
30
+ import DemoActorTimerOnceImpl from "../../actor/DemoActorTimerOnceImpl" ;
29
31
import DemoActorTimerInterface from "../../actor/DemoActorTimerInterface" ;
30
32
import DemoActorTimerTtlImpl from "../../actor/DemoActorTimerTtlImpl" ;
31
33
import DemoActorReminderTtlImpl from "../../actor/DemoActorReminderTtlImpl" ;
@@ -82,7 +84,9 @@ describe("http/actors", () => {
82
84
await server . actor . registerActor ( DemoActorSayImpl ) ;
83
85
await server . actor . registerActor ( DemoActorReminderImpl ) ;
84
86
await server . actor . registerActor ( DemoActorReminder2Impl ) ;
87
+ await server . actor . registerActor ( DemoActorReminderOnceImpl ) ;
85
88
await server . actor . registerActor ( DemoActorTimerImpl ) ;
89
+ await server . actor . registerActor ( DemoActorTimerOnceImpl ) ;
86
90
await server . actor . registerActor ( DemoActorActivateImpl ) ;
87
91
await server . actor . registerActor ( DemoActorTimerTtlImpl ) ;
88
92
await server . actor . registerActor ( DemoActorReminderTtlImpl ) ;
@@ -292,6 +296,32 @@ describe("http/actors", () => {
292
296
const res4 = await actor . getCounter ( ) ;
293
297
expect ( res4 ) . toEqual ( 200 ) ;
294
298
} , 10000 ) ;
299
+
300
+ it ( "should only fire once when period is not set to a timer" , async ( ) => {
301
+ const builder = new ActorProxyBuilder < DemoActorTimerInterface > ( DemoActorTimerOnceImpl , client ) ;
302
+ const actor = builder . build ( ActorId . createRandomId ( ) ) ;
303
+
304
+ // Activate our actor
305
+ await actor . init ( ) ;
306
+
307
+ const res0 = await actor . getCounter ( ) ;
308
+ expect ( res0 ) . toEqual ( 0 ) ;
309
+
310
+ // Now we wait for dueTime (2s)
311
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
312
+
313
+ // After that the timer callback will be called
314
+ // In our case, the callback increments the count attribute
315
+ // the count attribute is +100 due to the passed state
316
+ const res1 = await actor . getCounter ( ) ;
317
+ expect ( res1 ) . toEqual ( 100 ) ;
318
+
319
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
320
+
321
+ // Make sure the counter didn't change
322
+ const res2 = await actor . getCounter ( ) ;
323
+ expect ( res2 ) . toEqual ( 100 ) ;
324
+ } , 5000 ) ;
295
325
} ) ;
296
326
297
327
describe ( "reminders" , ( ) => {
@@ -379,5 +409,31 @@ describe("http/actors", () => {
379
409
const res2 = await actor . getCounter ( ) ;
380
410
expect ( res2 ) . toEqual ( 123 ) ;
381
411
} ) ;
412
+
413
+ it ( "should only fire once when period is not set to a reminder" , async ( ) => {
414
+ const builder = new ActorProxyBuilder < DemoActorReminderInterface > ( DemoActorReminderOnceImpl , client ) ;
415
+ const actor = builder . build ( ActorId . createRandomId ( ) ) ;
416
+
417
+ // Activate our actor
418
+ // this will initialize the reminder to be called
419
+ await actor . init ( ) ;
420
+
421
+ const res0 = await actor . getCounter ( ) ;
422
+ expect ( res0 ) . toEqual ( 0 ) ;
423
+
424
+ // Now we wait for dueTime (1.5s)
425
+ await NodeJSUtil . sleep ( 1500 ) ;
426
+
427
+ // After that the reminder callback will be called
428
+ // In our case, the callback increments the count attribute
429
+ const res1 = await actor . getCounter ( ) ;
430
+ expect ( res1 ) . toEqual ( 100 ) ;
431
+
432
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
433
+
434
+ // Make sure the counter didn't change
435
+ const res2 = await actor . getCounter ( ) ;
436
+ expect ( res2 ) . toEqual ( 100 ) ;
437
+ } , 5000 ) ;
382
438
} ) ;
383
439
} ) ;
0 commit comments