@@ -4,6 +4,15 @@ use std::time::Duration;
44mod test_utilities;
55use test_utilities:: { cargo_build_canister, pic_base, update} ;
66
7+ /// Advance the time by a given number of seconds.
8+ /// "tick" the IC every second so that it progress and produce a block so that timers get triggered.
9+ fn advance_seconds ( pic : & PocketIc , seconds : u32 ) {
10+ for _ in 0 ..seconds {
11+ pic. advance_time ( Duration :: from_secs ( 1 ) ) ;
12+ pic. tick ( ) ;
13+ }
14+ }
15+
716#[ test]
817fn test_timers ( ) {
918 let wasm = cargo_build_canister ( "timers" ) ;
@@ -86,13 +95,6 @@ fn test_scheduling_many_timers() {
8695 assert_eq ! ( timers_to_schedule, executed_timers) ;
8796}
8897
89- fn advance_seconds ( pic : & PocketIc , seconds : u32 ) {
90- for _ in 0 ..seconds {
91- pic. advance_time ( Duration :: from_secs ( 1 ) ) ;
92- pic. tick ( ) ;
93- }
94- }
95-
9698#[ test]
9799fn test_set_global_timers ( ) {
98100 let wasm = cargo_build_canister ( "timers" ) ;
@@ -153,3 +155,25 @@ fn test_async_timers() {
153155 3
154156 ) ;
155157}
158+
159+ #[ test]
160+ fn test_periodic_timers_repeat_when_task_make_calls ( ) {
161+ let wasm = cargo_build_canister ( "timers" ) ;
162+ let pic = pic_base ( ) . build ( ) ;
163+
164+ let canister_id = pic. create_canister ( ) ;
165+ pic. add_cycles ( canister_id, 2_000_000_000_000 ) ;
166+ pic. install_canister ( canister_id, wasm, vec ! [ ] , None ) ;
167+
168+ update :: < ( ) , ( ) > ( & pic, canister_id, "start_repeating_async" , ( ) ) . unwrap ( ) ;
169+ // start_repeating_async sets a repeating timer with a 1s interval
170+ // advance time by 3 seconds should trigger 3 repeats
171+ advance_seconds ( & pic, 3 ) ;
172+ update :: < ( ) , ( ) > ( & pic, canister_id, "stop_repeating" , ( ) ) . unwrap ( ) ;
173+
174+ let ( events, ) : ( Vec < String > , ) = query_candid ( & pic, canister_id, "get_events" , ( ) ) . unwrap ( ) ;
175+ assert_eq ! (
176+ events[ ..] ,
177+ [ "method repeat" , "method repeat" , "method repeat" ]
178+ ) ;
179+ }
0 commit comments