@@ -4,20 +4,43 @@ import Testing
4
4
5
5
/// A concrete, test class for the abstract Speaker, which overrides the handlers to send things to
6
6
/// continuations we set in the test.
7
- class TestTunnel : Speaker < Vpn_TunnelMessage , Vpn_ManagerMessage > {
8
- var msgHandler : CheckedContinuation < Vpn_ManagerMessage , Error > ?
7
+ class TestTunnel : Speaker < Vpn_TunnelMessage , Vpn_ManagerMessage > , @ unchecked Sendable {
8
+ private var msgHandler : CheckedContinuation < Vpn_ManagerMessage , Error > ?
9
9
override func handleMessage( _ msg: Vpn_ManagerMessage ) {
10
10
msgHandler? . resume ( returning: msg)
11
11
}
12
12
13
- var rpcHandler : CheckedContinuation < RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > , Error > ?
13
+ /// Runs the given closure asynchronously and returns the next non-RPC message received.
14
+ func expectMessage( with closure:
15
+ @escaping @Sendable ( ) async -> Void ) async throws -> Vpn_ManagerMessage {
16
+ return try await withCheckedThrowingContinuation { continuation in
17
+ msgHandler = continuation
18
+ Task {
19
+ await closure ( )
20
+ }
21
+ }
22
+ }
23
+
24
+ private var rpcHandler : CheckedContinuation < RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > , Error > ?
14
25
override func handleRPC( _ req: RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > ) {
15
26
rpcHandler? . resume ( returning: req)
16
27
}
28
+
29
+ /// Runs the given closure asynchronously and return the next non-RPC message received
30
+ func expectRPC( with closure:
31
+ @escaping @Sendable ( ) async -> Void ) async throws ->
32
+ RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > {
33
+ return try await withCheckedThrowingContinuation { continuation in
34
+ rpcHandler = continuation
35
+ Task {
36
+ await closure ( )
37
+ }
38
+ }
39
+ }
17
40
}
18
41
19
42
@Suite ( . timeLimit( . minutes( 1 ) ) )
20
- struct SpeakerTests {
43
+ struct SpeakerTests: Sendable {
21
44
let pipeMT = Pipe ( )
22
45
let pipeTM = Pipe ( )
23
46
let uut : TestTunnel
@@ -56,14 +79,11 @@ struct SpeakerTests {
56
79
@Test func handleSingleMessage( ) async throws {
57
80
async let readDone : ( ) = try uut. readLoop ( )
58
81
59
- let got = try await withCheckedThrowingContinuation { continuation in
60
- uut. msgHandler = continuation
61
- Task {
62
- var s = Vpn_ManagerMessage ( )
63
- s. start = Vpn_StartRequest ( )
64
- await #expect( throws: Never . self) {
65
- try await sender. send ( s)
66
- }
82
+ let got = try await uut. expectMessage {
83
+ var s = Vpn_ManagerMessage ( )
84
+ s. start = Vpn_StartRequest ( )
85
+ await #expect( throws: Never . self) {
86
+ try await sender. send ( s)
67
87
}
68
88
}
69
89
#expect( got. msg == . start( Vpn_StartRequest ( ) ) )
@@ -74,16 +94,13 @@ struct SpeakerTests {
74
94
@Test func handleRPC( ) async throws {
75
95
async let readDone : ( ) = try uut. readLoop ( )
76
96
77
- let got = try await withCheckedThrowingContinuation { continuation in
78
- uut. rpcHandler = continuation
79
- Task {
80
- var s = Vpn_ManagerMessage ( )
81
- s. start = Vpn_StartRequest ( )
82
- s. rpc = Vpn_RPC ( )
83
- s. rpc. msgID = 33
84
- await #expect( throws: Never . self) {
85
- try await sender. send ( s)
86
- }
97
+ let got = try await uut. expectRPC {
98
+ var s = Vpn_ManagerMessage ( )
99
+ s. start = Vpn_StartRequest ( )
100
+ s. rpc = Vpn_RPC ( )
101
+ s. rpc. msgID = 33
102
+ await #expect( throws: Never . self) {
103
+ try await sender. send ( s)
87
104
}
88
105
}
89
106
#expect( got. msg. msg == . start( Vpn_StartRequest ( ) ) )
0 commit comments