Skip to content

Commit f3d4ce5

Browse files
committed
test: Add tests for AsyncSemaphore and DispatchTimeInterval.
[Human-Directed AI Assistance]
1 parent 0cfc4e3 commit f3d4ce5

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Testing
2+
3+
@testable import DispatchAsync
4+
5+
@Test
6+
func asyncSemaphoreWaitSignal() async throws {
7+
let semaphore = AsyncSemaphore(value: 1)
8+
9+
// First wait should succeed immediately and bring the count to 0
10+
await semaphore.wait()
11+
12+
// Launch a task that tries to wait – it should be suspended until we signal
13+
var didEnterCriticalSection = false
14+
let waiter = Task {
15+
await semaphore.wait()
16+
didEnterCriticalSection = true
17+
await semaphore.signal()
18+
}
19+
20+
// Allow the task a brief moment to start and (hopefully) suspend
21+
try? await Task.sleep(nanoseconds: 1_000)
22+
23+
#expect(!didEnterCriticalSection) // should still be waiting
24+
25+
// Now release the semaphore – the waiter should proceed
26+
await semaphore.signal()
27+
28+
// Give the waiter a chance to run
29+
try? await Task.sleep(nanoseconds: 1_000)
30+
31+
#expect(didEnterCriticalSection) // waiter must have run
32+
33+
_ = await waiter.value
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Testing
2+
3+
@testable import DispatchAsync
4+
5+
@Test
6+
func dispatchTimeIntervalEquality() throws {
7+
// 1 second == 1_000 milliseconds
8+
#expect(DispatchTimeInterval.seconds(1) == .milliseconds(1_000))
9+
10+
// 1 second != 2 seconds
11+
#expect(DispatchTimeInterval.seconds(1) != .seconds(2))
12+
13+
// 2_000 micro-seconds == 1 milliseconds
14+
#expect(DispatchTimeInterval.microseconds(2_000) == .milliseconds(2))
15+
16+
// 1 micro-seconds == 1_000 nanoseconds
17+
#expect(DispatchTimeInterval.microseconds(1) == .nanoseconds(1_000))
18+
19+
// `.never` is only equal to `.never`
20+
#expect(DispatchTimeInterval.never == .never)
21+
#expect(DispatchTimeInterval.never != .seconds(0))
22+
#expect(DispatchTimeInterval.never != .seconds(Int.max))
23+
}

0 commit comments

Comments
 (0)