@@ -4,20 +4,45 @@ 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
+ {
17
+ return try await withCheckedThrowingContinuation { continuation in
18
+ msgHandler = continuation
19
+ Task {
20
+ await closure ( )
21
+ }
22
+ }
23
+ }
24
+
25
+ private var rpcHandler : CheckedContinuation < RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > , Error > ?
14
26
override func handleRPC( _ req: RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage > ) {
15
27
rpcHandler? . resume ( returning: req)
16
28
}
29
+
30
+ /// Runs the given closure asynchronously and return the next non-RPC message received
31
+ func expectRPC( with closure:
32
+ @escaping @Sendable ( ) async -> Void ) async throws ->
33
+ RPCRequest < Vpn_TunnelMessage , Vpn_ManagerMessage >
34
+ {
35
+ return try await withCheckedThrowingContinuation { continuation in
36
+ rpcHandler = continuation
37
+ Task {
38
+ await closure ( )
39
+ }
40
+ }
41
+ }
17
42
}
18
43
19
44
@Suite ( . timeLimit( . minutes( 1 ) ) )
20
- struct SpeakerTests {
45
+ struct SpeakerTests: Sendable {
21
46
let pipeMT = Pipe ( )
22
47
let pipeTM = Pipe ( )
23
48
let uut : TestTunnel
@@ -56,14 +81,11 @@ struct SpeakerTests {
56
81
@Test func handleSingleMessage( ) async throws {
57
82
async let readDone : ( ) = try uut. readLoop ( )
58
83
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
- }
84
+ let got = try await uut. expectMessage {
85
+ var s = Vpn_ManagerMessage ( )
86
+ s. start = Vpn_StartRequest ( )
87
+ await #expect( throws: Never . self) {
88
+ try await sender. send ( s)
67
89
}
68
90
}
69
91
#expect( got. msg == . start( Vpn_StartRequest ( ) ) )
@@ -74,16 +96,13 @@ struct SpeakerTests {
74
96
@Test func handleRPC( ) async throws {
75
97
async let readDone : ( ) = try uut. readLoop ( )
76
98
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
- }
99
+ let got = try await uut. expectRPC {
100
+ var s = Vpn_ManagerMessage ( )
101
+ s. start = Vpn_StartRequest ( )
102
+ s. rpc = Vpn_RPC ( )
103
+ s. rpc. msgID = 33
104
+ await #expect( throws: Never . self) {
105
+ try await sender. send ( s)
87
106
}
88
107
}
89
108
#expect( got. msg. msg == . start( Vpn_StartRequest ( ) ) )
0 commit comments