Skip to content

Commit 5ce6b1a

Browse files
authored
test: add async signal tests for events (#17)
* test: add async signal tests for events * wip: improve test cases
1 parent 3c3c61b commit 5ce6b1a

15 files changed

+33
-45
lines changed

Sources/AsyncObjects/AsyncCountdownEvent.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
import OrderedCollections
84

Sources/AsyncObjects/AsyncEvent.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
/// An object that controls execution of tasks depending on the signal state.
84
///

Sources/AsyncObjects/AsyncSemaphore.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
import OrderedCollections
84

Sources/AsyncObjects/Continuation/ContinuableCollection.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
/// A type that manages a collection of continuations with an associated key.
84
///

Sources/AsyncObjects/Continuation/ContinuableCollectionActor.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
#if swift(>=5.7)
21
import Foundation
32

3+
#if swift(>=5.7)
44
/// An actor type that manages a collection of continuations with an associated key.
55
///
66
/// On `Swift 5.7` and above [actor isolation bug with protocol conformance](https://forums.swift.org/t/actor-isolation-is-broken-by-protocol-conformance/57040)
77
/// is fixed, and hence original protocol can be used without any issue.
88
typealias ContinuableCollectionActor = ContinuableCollection
99
#else
10-
@preconcurrency import Foundation
11-
1210
/// An actor type that manages a collection of continuations with an associated key.
1311
///
1412
/// This is to avoid [actor isolation bug with protocol conformance on older `Swift` versions](https://forums.swift.org/t/actor-isolation-is-broken-by-protocol-conformance/57040).

Sources/AsyncObjects/Continuation/TrackedContinuation.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
/// A mechanism to interface between synchronous and asynchronous code,
84
/// with tracking state data.

Sources/AsyncObjects/Future.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
/// An object that eventually produces a single value and then finishes or fails.
84
///

Sources/AsyncObjects/TaskQueue.swift

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#if swift(>=5.7)
21
import Foundation
3-
#else
4-
@preconcurrency import Foundation
5-
#endif
62

73
import OrderedCollections
84

Tests/AsyncObjectsTests/AsyncCountdownEventTests.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ class AsyncCountdownEventTests: XCTestCase {
1717
try await event.wait(forSeconds: 5)
1818
}
1919

20+
func testWithIncrementSignalAfterSomeWait() async throws {
21+
let event = AsyncCountdownEvent()
22+
event.increment(by: 10)
23+
try await waitUntil(event, timeout: 5) { $0.currentCount == 10 }
24+
Task {
25+
try await Task.sleep(seconds: 1)
26+
await event.signal(concurrent: 10)
27+
}
28+
try await event.wait(forSeconds: 10)
29+
}
30+
2031
func testWithOverIncrement() async throws {
2132
let event = AsyncCountdownEvent()
2233
event.increment(by: 10)
@@ -78,7 +89,7 @@ class AsyncCountdownEventTests: XCTestCase {
7889
event.signal()
7990
try await event.wait(forSeconds: 5)
8091
self.addTeardownBlock { [weak event] in
81-
try await waitUntil(event, timeout: 5) { $0.assertReleased() }
92+
try await waitUntil(event, timeout: 10) { $0.assertReleased() }
8293
}
8394
}
8495

Tests/AsyncObjectsTests/AsyncEventTests.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ class AsyncEventTests: XCTestCase {
1010
try await event.wait(forSeconds: 3)
1111
}
1212

13+
func testSignalAfterSomeWait() async throws {
14+
let event = AsyncEvent(signaledInitially: false)
15+
Task {
16+
try await Task.sleep(seconds: 1)
17+
event.signal()
18+
}
19+
try await event.wait(forSeconds: 10)
20+
}
21+
1322
func testResetSignal() async throws {
1423
let event = AsyncEvent()
1524
event.reset()
@@ -29,7 +38,7 @@ class AsyncEventTests: XCTestCase {
2938
try await event.wait(forSeconds: 3)
3039
await task.value
3140
self.addTeardownBlock { [weak event] in
32-
try await waitUntil(event, timeout: 5) { $0.assertReleased() }
41+
try await waitUntil(event, timeout: 10) { $0.assertReleased() }
3342
}
3443
}
3544

Tests/AsyncObjectsTests/AsyncSemaphoreTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class AsyncSemaphoreTests: XCTestCase {
7474
let semaphore = AsyncSemaphore(value: 1)
7575
try await semaphore.wait(forSeconds: 3)
7676
self.addTeardownBlock { [weak semaphore] in
77-
try await waitUntil(semaphore, timeout: 5) { $0.assertReleased() }
77+
try await waitUntil(semaphore, timeout: 10) { $0.assertReleased() }
7878
}
7979
}
8080
}

Tests/AsyncObjectsTests/NonThrowingFutureTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class NonThrowingFutureTests: XCTestCase {
5858
let _ = try await future.wait(forSeconds: 3)
5959
await task.value
6060
self.addTeardownBlock { [weak future] in
61-
try await waitUntil(future, timeout: 5) { $0.assertReleased() }
61+
try await waitUntil(future, timeout: 10) { $0.assertReleased() }
6262
}
6363
}
6464

Tests/AsyncObjectsTests/TaskOperationTests.swift

+6-8
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,14 @@ class TaskOperationTests: XCTestCase {
7474
try await operation.wait(forSeconds: 3)
7575
}
7676

77-
func testFinisheAsyncdWait() async throws {
78-
let operation = TaskOperation { /* Do nothing */ }
79-
operation.signal()
80-
try await operation.wait(forSeconds: 3)
81-
}
82-
8377
func testDeinit() async throws {
8478
let operation = TaskOperation {
8579
try await Task.sleep(seconds: 1)
8680
}
8781
operation.signal()
8882
try await operation.wait(forSeconds: 5)
8983
self.addTeardownBlock { [weak operation] in
90-
try await waitUntil(operation, timeout: 5) { $0.assertReleased() }
84+
try await waitUntil(operation, timeout: 10) { $0.assertReleased() }
9185
}
9286
}
9387

@@ -235,7 +229,7 @@ class TaskOperationCancellationTests: XCTestCase {
235229
operation.cancel()
236230
try await waitUntil(operation, timeout: 3, satisfies: \.isCancelled)
237231
self.addTeardownBlock { [weak operation] in
238-
try await waitUntil(operation, timeout: 5) { $0.assertReleased() }
232+
try await waitUntil(operation, timeout: 10) { $0.assertReleased() }
239233
}
240234
}
241235
}
@@ -265,6 +259,10 @@ class TaskOperationTaskManagementTests: XCTestCase {
265259
XCTAssertFalse(error.localizedDescription.isEmpty)
266260
default: XCTFail("Unexpected operation result")
267261
}
262+
do {
263+
try await operation.wait(forSeconds: 3)
264+
XCTFail("Unexpected task progression")
265+
} catch is DurationTimeoutError {}
268266
}
269267

270268
func testNotStartedCancellationError() async throws {

Tests/AsyncObjectsTests/TaskQueueTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class TaskQueueTests: XCTestCase {
7070
await queue.exec(flags: .barrier) { /* Do nothing */ }
7171
await queue.exec { /* Do nothing */ }
7272
self.addTeardownBlock { [weak queue] in
73-
try await waitUntil(queue, timeout: 5) { $0.assertReleased() }
73+
try await waitUntil(queue, timeout: 10) { $0.assertReleased() }
7474
}
7575
}
7676
}

Tests/AsyncObjectsTests/ThrowingFutureTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ThrowingFutureTests: XCTestCase {
6666
let _ = try await future.wait(forSeconds: 3)
6767
await task.value
6868
self.addTeardownBlock { [weak future] in
69-
try await waitUntil(future, timeout: 5) { $0.assertReleased() }
69+
try await waitUntil(future, timeout: 10) { $0.assertReleased() }
7070
}
7171
}
7272

0 commit comments

Comments
 (0)