Skip to content

Commit a7f6c97

Browse files
committed
add test
1 parent 60a2e64 commit a7f6c97

3 files changed

Lines changed: 161 additions & 3 deletions

File tree

Package@swift-5.9.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "AsyncTimer",
8+
platforms: [
9+
.macOS(.v10_15),
10+
.iOS(.v13),
11+
.tvOS(.v13),
12+
.watchOS(.v6),
13+
.macCatalyst(.v13),
14+
.visionOS(.v1),
15+
],
16+
17+
products: [
18+
// Products define the executables and libraries a package produces, making them visible to other packages.
19+
.library(
20+
name: "AsyncTimer",
21+
targets: ["AsyncTimer"]
22+
),
23+
],
24+
targets: [
25+
// Targets are the basic building blocks of a package, defining a module or a test suite.
26+
// Targets can depend on other targets in this package and products from dependencies.
27+
.target(
28+
name: "AsyncTimer"),
29+
.testTarget(
30+
name: "AsyncTimerTests",
31+
dependencies: ["AsyncTimer"]
32+
),
33+
],
34+
swiftLanguageVersions: [.v5]
35+
)

Sources/AsyncTimer/AsyncTimer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public final actor AsyncTimer {
9595
}
9696
} catch is CancellationError {
9797
await cancelHandler?()
98+
return
9899
} catch {}
99100
}
100101
}
Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,128 @@
1-
import Testing
21
@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")
3123

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+
}
6128
}

0 commit comments

Comments
 (0)