@@ -26,6 +26,8 @@ import DemoActorSayImpl from '../actor/DemoActorSayImpl';
26
26
import DemoActorSayInterface from '../actor/DemoActorSayInterface' ;
27
27
import DemoActorTimerImpl from '../actor/DemoActorTimerImpl' ;
28
28
import DemoActorTimerInterface from '../actor/DemoActorTimerInterface' ;
29
+ import DemoActorTimerTtlImpl from '../actor/DemoActorTimerTtlImpl' ;
30
+ import DemoActorReminderTtlImpl from '../actor/DemoActorReminderTtlImpl' ;
29
31
30
32
const serverHost = "127.0.0.1" ;
31
33
const serverPort = "50001" ;
@@ -171,6 +173,42 @@ describe('http/actors', () => {
171
173
const res4 = await actor . getCounter ( ) ;
172
174
expect ( res4 ) . toEqual ( 300 ) ;
173
175
} , 10000 ) ;
176
+
177
+
178
+ it ( 'should apply the ttl when it is set (expected execution time > 5s)' , async ( ) => {
179
+ const builder = new ActorProxyBuilder < DemoActorTimerInterface > ( DemoActorTimerTtlImpl , client ) ;
180
+ const actor = builder . build ( ActorId . createRandomId ( ) ) ;
181
+
182
+ // Activate our actor
183
+ await actor . init ( ) ;
184
+
185
+ const res0 = await actor . getCounter ( ) ;
186
+ expect ( res0 ) . toEqual ( 0 ) ;
187
+
188
+ // Now we wait for dueTime (2s)
189
+ await ( new Promise ( resolve => setTimeout ( resolve , 500 ) ) ) ;
190
+
191
+ // After that the timer callback will be called
192
+ // In our case, the callback increments the count attribute
193
+ // the count attribute is +100 due to the passed state
194
+ const res1 = await actor . getCounter ( ) ;
195
+ expect ( res1 ) . toEqual ( 100 ) ;
196
+
197
+ // Every 1 second the timer gets called again, so the count attribute should change
198
+ // we check this twice to ensure correct calling
199
+ await ( new Promise ( resolve => setTimeout ( resolve , 500 ) ) ) ;
200
+ const res2 = await actor . getCounter ( ) ;
201
+ expect ( res2 ) . toEqual ( 200 ) ;
202
+
203
+ await ( new Promise ( resolve => setTimeout ( resolve , 500 ) ) ) ;
204
+ try {
205
+ const res3 = await actor . getCounter ( ) ;
206
+ } catch ( e ) {
207
+ //@ts -ignore
208
+ console . log ( e . message )
209
+ }
210
+ // expect(res3).toEqual(300);
211
+ } , 10000 ) ;
174
212
} ) ;
175
213
176
214
describe ( 'reminders' , ( ) => {
@@ -228,5 +266,36 @@ describe('http/actors', () => {
228
266
// Unregister the reminder
229
267
await actor . removeReminder ( ) ;
230
268
} ) ;
269
+
270
+ it ( 'should apply the ttl when it is set to a reminder' , async ( ) => {
271
+ const builder = new ActorProxyBuilder < DemoActorReminderInterface > ( DemoActorReminderTtlImpl , client ) ;
272
+ const actor = builder . build ( ActorId . createRandomId ( ) ) ;
273
+
274
+ // Activate our actor
275
+ // this will initialize the reminder to be called
276
+ await actor . init ( ) ;
277
+
278
+ const res0 = await actor . getCounter ( ) ;
279
+ expect ( res0 ) . toEqual ( 0 ) ;
280
+
281
+ // Now we wait for dueTime (2s)
282
+ await NodeJSUtil . sleep ( 2000 ) ;
283
+
284
+ // After that the reminder callback will be called
285
+ // In our case, the callback increments the count attribute
286
+ const res1 = await actor . getCounter ( ) ;
287
+ expect ( res1 ) . toEqual ( 123 ) ;
288
+
289
+ await actor . removeReminder ( ) ;
290
+
291
+ await ( new Promise ( resolve => setTimeout ( resolve , 500 ) ) ) ;
292
+ try {
293
+ const res2 = await actor . getCounter ( ) ;
294
+ } catch ( e ) {
295
+ //@ts -ignore
296
+ console . log ( e . message )
297
+ }
298
+ // expect(res2).toEqual(123);
299
+ } ) ;
231
300
} ) ;
232
301
} ) ;
0 commit comments