|
1 | | -import Testing |
2 | 1 | @testable import AsyncTimer |
| 2 | +import Foundation |
| 3 | +import Testing |
| 4 | + |
| 5 | +// MARK: - AsyncTimerTests |
| 6 | + |
| 7 | +@Suite struct AsyncTimerTests { |
| 8 | + // MARK: - One-time Timer Tests |
| 9 | + |
| 10 | + @MainActor |
| 11 | + @Test func oneTimeTimer() async throws { |
| 12 | + var count = 0 |
| 13 | + let timer = AsyncTimer( |
| 14 | + interval: 0.1, |
| 15 | + repeating: false, |
| 16 | + handler: { |
| 17 | + @MainActor in |
| 18 | + count += 1 |
| 19 | + } |
| 20 | + ) |
| 21 | + |
| 22 | + await timer.start() |
| 23 | + try await Task.sleep(nanoseconds: UInt64(0.2 * 1_000_000_000)) |
| 24 | + |
| 25 | + #expect(count == 1, "One-time timer should execute exactly once") |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - Repeating Timer Tests |
| 29 | + |
| 30 | + @MainActor |
| 31 | + @Test func repeatingTimer() async throws { |
| 32 | + var count = 0 |
| 33 | + let timer = AsyncTimer( |
| 34 | + interval: 0.1, |
| 35 | + repeating: true, |
| 36 | + firesImmediately: false, |
| 37 | + handler: { |
| 38 | + @MainActor in |
| 39 | + count += 1 |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + await timer.start() |
| 44 | + try await Task.sleep(nanoseconds: UInt64(0.35 * 1_000_000_000)) |
| 45 | + await timer.stop() |
| 46 | + |
| 47 | + #expect(count >= 3, "Repeating timer should execute at least 3 times") |
| 48 | + } |
| 49 | + |
| 50 | + // MARK: - Fires Immediately Tests |
| 51 | + |
| 52 | + @MainActor |
| 53 | + @Test func testFiresImmediately() async throws { |
| 54 | + var executionTimes: [TimeInterval] = [] |
| 55 | + let startTime = Date().timeIntervalSince1970 |
| 56 | + |
| 57 | + let timer = AsyncTimer( |
| 58 | + interval: 0.1, |
| 59 | + repeating: true, |
| 60 | + firesImmediately: true, |
| 61 | + handler: { |
| 62 | + @MainActor in |
| 63 | + executionTimes.append(Date().timeIntervalSince1970 - startTime) |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + await timer.start() |
| 68 | + try await Task.sleep(nanoseconds: UInt64(0.15 * 1_000_000_000)) |
| 69 | + await timer.stop() |
| 70 | + |
| 71 | + #expect(!executionTimes.isEmpty, "Timer should execute at least once") |
| 72 | + #expect(executionTimes[0] < 0.01, "First execution should happen almost immediately") |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - Set Interval Tests |
| 76 | + |
| 77 | + @MainActor |
| 78 | + @Test func testSetInterval() async throws { |
| 79 | + var executionTimes: [TimeInterval] = [] |
| 80 | + let startTime = Date().timeIntervalSince1970 |
| 81 | + |
| 82 | + let timer = AsyncTimer( |
| 83 | + interval: 0.2, |
| 84 | + repeating: true, |
| 85 | + handler: { |
| 86 | + @MainActor in |
| 87 | + executionTimes.append(Date().timeIntervalSince1970 - startTime) |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + await timer.start() |
| 92 | + try await Task.sleep(nanoseconds: UInt64(0.3 * 1_000_000_000)) |
| 93 | + await timer.setInterval(0.1) |
| 94 | + try await Task.sleep(nanoseconds: UInt64(0.2 * 1_000_000_000)) |
| 95 | + await timer.stop() |
| 96 | + |
| 97 | + let intervals = zip(executionTimes, executionTimes.dropFirst()) |
| 98 | + .map { $1 - $0 } |
| 99 | + |
| 100 | + #expect(intervals.contains { $0 < 0.15 }, "Intervals should become shorter after setting new interval") |
| 101 | + } |
| 102 | + |
| 103 | + // MARK: - Stop/Restart Tests |
| 104 | + |
| 105 | + @MainActor |
| 106 | + @Test func stopAndRestart() async throws { |
| 107 | + var count = 0 |
| 108 | + let timer = AsyncTimer( |
| 109 | + interval: 0.1, |
| 110 | + repeating: true, |
| 111 | + handler: { |
| 112 | + @MainActor in |
| 113 | + count += 1 |
| 114 | + } |
| 115 | + ) |
| 116 | + |
| 117 | + await timer.start() |
| 118 | + try await Task.sleep(nanoseconds: UInt64(0.15 * 1_000_000_000)) |
| 119 | + await timer.stop() |
| 120 | + let countAfterStop = count |
| 121 | + try await Task.sleep(nanoseconds: UInt64(0.2 * 1_000_000_000)) |
| 122 | + #expect(count == countAfterStop, "Count should not increase after stopping") |
3 | 123 |
|
4 | | -@Test func example() async throws { |
5 | | - // Write your test here and use APIs like `#expect(...)` to check expected conditions. |
| 124 | + await timer.restart() |
| 125 | + try await Task.sleep(nanoseconds: UInt64(0.15 * 1_000_000_000)) |
| 126 | + #expect(count > countAfterStop, "Count should increase after restarting") |
| 127 | + } |
6 | 128 | } |
0 commit comments