@@ -125,4 +125,139 @@ import Testing
125125 try await Task . sleep ( nanoseconds: UInt64 ( 0.15 * 1_000_000_000 ) )
126126 #expect( count > countAfterStop, " Count should increase after restarting " )
127127 }
128+
129+ // MARK: - Cancel Handler Tests
130+
131+ @MainActor
132+ @Test func testCancelHandler( ) async throws {
133+ var handlerExecuted = false
134+ var cancelHandlerExecuted = false
135+
136+ let timer = AsyncTimer (
137+ interval: 0.1 ,
138+ repeating: true ,
139+ handler: {
140+ @MainActor in
141+ handlerExecuted = true
142+ } ,
143+ cancelHandler: {
144+ @MainActor in
145+ cancelHandlerExecuted = true
146+ }
147+ )
148+
149+ await timer. start ( )
150+ try await Task . sleep ( nanoseconds: UInt64 ( 0.15 * 1_000_000_000 ) )
151+ await timer. stop ( )
152+ try await Task . sleep ( nanoseconds: UInt64 ( 0.05 * 1_000_000_000 ) ) // Give time for cancel handler to execute
153+
154+ #expect( handlerExecuted, " Timer handler should have executed " )
155+ #expect( cancelHandlerExecuted, " Cancel handler should have executed after stopping the timer " )
156+ }
157+
158+ // MARK: - Task Priority Tests
159+
160+ @MainActor
161+ @Test func taskPriority( ) async throws {
162+ var highPriorityExecutionTime : TimeInterval = 0
163+ var lowPriorityExecutionTime : TimeInterval = 0
164+ let startTime = Date ( ) . timeIntervalSince1970
165+
166+ // Create a high priority timer
167+ let highPriorityTimer = AsyncTimer (
168+ interval: 0.1 ,
169+ priority: . high,
170+ repeating: false ,
171+ handler: {
172+ @MainActor in
173+ // Simulate some work
174+ for _ in 0 ..< 1000000 {
175+ _ = 1 + 1
176+ }
177+ highPriorityExecutionTime = Date ( ) . timeIntervalSince1970 - startTime
178+ }
179+ )
180+
181+ // Create a low priority timer
182+ let lowPriorityTimer = AsyncTimer (
183+ interval: 0.1 ,
184+ priority: . low,
185+ repeating: false ,
186+ handler: {
187+ @MainActor in
188+ // Simulate some work
189+ for _ in 0 ..< 1000000 {
190+ _ = 1 + 1
191+ }
192+ lowPriorityExecutionTime = Date ( ) . timeIntervalSince1970 - startTime
193+ }
194+ )
195+
196+ // Start both timers at the same time
197+ await lowPriorityTimer. start ( )
198+ await highPriorityTimer. start ( )
199+
200+ // Wait for both to complete
201+ try await Task . sleep ( nanoseconds: UInt64 ( 0.3 * 1_000_000_000 ) )
202+
203+ // Note: This test is probabilistic and may not always pass
204+ // In a heavily loaded system, the scheduler might not respect priorities as expected
205+ #expect( highPriorityExecutionTime <= lowPriorityExecutionTime, " High priority timer should execute before or at the same time as low priority timer " )
206+ }
207+
208+ // MARK: - Multiple Timers Test
209+
210+ @MainActor
211+ @Test func multipleTimers( ) async throws {
212+ var counts = [ 0 , 0 , 0 ]
213+
214+ let timer1 = AsyncTimer (
215+ interval: 0.05 ,
216+ repeating: true ,
217+ handler: {
218+ @MainActor in
219+ counts [ 0 ] += 1
220+ }
221+ )
222+
223+ let timer2 = AsyncTimer (
224+ interval: 0.07 ,
225+ repeating: true ,
226+ handler: {
227+ @MainActor in
228+ counts [ 1 ] += 1
229+ }
230+ )
231+
232+ let timer3 = AsyncTimer (
233+ interval: 0.03 ,
234+ repeating: true ,
235+ handler: {
236+ @MainActor in
237+ counts [ 2 ] += 1
238+ }
239+ )
240+
241+ // Start all timers
242+ await timer1. start ( )
243+ await timer2. start ( )
244+ await timer3. start ( )
245+
246+ // Let them run for a while
247+ try await Task . sleep ( nanoseconds: UInt64 ( 0.3 * 1_000_000_000 ) )
248+
249+ // Stop all timers
250+ await timer1. stop ( )
251+ await timer2. stop ( )
252+ await timer3. stop ( )
253+
254+ // Check that all timers executed
255+ #expect( counts [ 0 ] > 0 , " Timer 1 should have executed " )
256+ #expect( counts [ 1 ] > 0 , " Timer 2 should have executed " )
257+ #expect( counts [ 2 ] > 0 , " Timer 3 should have executed " )
258+
259+ // Timer3 should have executed more times than timer1, which should have executed more times than timer2
260+ #expect( counts [ 2 ] > counts [ 0 ] , " Timer with shorter interval should execute more times " )
261+ #expect( counts [ 0 ] > counts [ 1 ] , " Timer with shorter interval should execute more times " )
262+ }
128263}
0 commit comments