Skip to content

Commit ad42d72

Browse files
committed
test: add async event tests
1 parent dc3090c commit ad42d72

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import XCTest
2+
@testable import AsyncObjects
3+
4+
class AsyncEventTests: XCTestCase {
5+
6+
func checkWait(
7+
for event: AsyncEvent,
8+
signalIn interval: UInt64 = UInt64(5E9),
9+
durationInSeconds seconds: Int = 0
10+
) async throws {
11+
Task.detached {
12+
try await Task.sleep(nanoseconds: interval)
13+
await event.signal()
14+
}
15+
await checkExecInterval(
16+
for: { await event.wait() },
17+
durationInSeconds: seconds
18+
)
19+
}
20+
21+
func testEventWait() async throws {
22+
let event = AsyncEvent(signaledInitially: false)
23+
try await checkWait(for: event, durationInSeconds: 5)
24+
}
25+
26+
func testEventLockAndWait() async throws {
27+
let event = AsyncEvent()
28+
await event.reset()
29+
try await checkWait(for: event, durationInSeconds: 5)
30+
}
31+
32+
func testReleasedEventWait() async throws {
33+
let event = AsyncEvent()
34+
try await checkWait(for: event)
35+
}
36+
37+
func testEventWaitWithTimeout() async throws {
38+
let event = AsyncEvent(signaledInitially: false)
39+
var result: TaskTimeoutResult = .success
40+
await checkExecInterval(
41+
for: {
42+
result = await event.wait(forNanoseconds: UInt64(4E9))
43+
},
44+
durationInSeconds: 4
45+
)
46+
XCTAssertEqual(result, .timedOut)
47+
}
48+
49+
func testEventWaitSuccessWithoutTimeout() async throws {
50+
let event = AsyncEvent(signaledInitially: false)
51+
var result: TaskTimeoutResult = .timedOut
52+
Task.detached {
53+
try await Task.sleep(nanoseconds: UInt64(5E9))
54+
await event.signal()
55+
}
56+
await checkExecInterval(
57+
for: {
58+
result = await event.wait(forNanoseconds: UInt64(10E9))
59+
},
60+
durationInSeconds: 5
61+
)
62+
XCTAssertEqual(result, .success)
63+
}
64+
65+
func testReleasedEventWaitSuccessWithoutTimeout() async throws {
66+
let event = AsyncEvent()
67+
var result: TaskTimeoutResult = .timedOut
68+
await checkExecInterval(
69+
for: {
70+
result = await event.wait(forNanoseconds: UInt64(10E9))
71+
},
72+
durationInSeconds: 0
73+
)
74+
XCTAssertEqual(result, .success)
75+
}
76+
}

0 commit comments

Comments
 (0)