@@ -112,16 +112,39 @@ impl Writeable for TestCustomMessage {
112
112
}
113
113
114
114
struct TestCustomMessageHandler {
115
- expected_messages : Mutex < VecDeque < TestCustomMessage > > ,
115
+ expectations : Mutex < VecDeque < OnHandleCustomMessage > > ,
116
+ }
117
+
118
+ struct OnHandleCustomMessage {
119
+ expect : TestCustomMessage ,
120
+ include_reply_path : bool ,
116
121
}
117
122
118
123
impl TestCustomMessageHandler {
119
124
fn new ( ) -> Self {
120
- Self { expected_messages : Mutex :: new ( VecDeque :: new ( ) ) }
125
+ Self { expectations : Mutex :: new ( VecDeque :: new ( ) ) }
121
126
}
122
127
123
128
fn expect_message ( & self , message : TestCustomMessage ) {
124
- self . expected_messages . lock ( ) . unwrap ( ) . push_back ( message) ;
129
+ self . expectations . lock ( ) . unwrap ( ) . push_back (
130
+ OnHandleCustomMessage {
131
+ expect : message,
132
+ include_reply_path : false ,
133
+ }
134
+ ) ;
135
+ }
136
+
137
+ fn expect_message_and_response ( & self , message : TestCustomMessage ) {
138
+ self . expectations . lock ( ) . unwrap ( ) . push_back (
139
+ OnHandleCustomMessage {
140
+ expect : message,
141
+ include_reply_path : true ,
142
+ }
143
+ ) ;
144
+ }
145
+
146
+ fn get_next_expectation ( & self ) -> OnHandleCustomMessage {
147
+ self . expectations . lock ( ) . unwrap ( ) . pop_front ( ) . expect ( "No expectations remaining" )
125
148
}
126
149
}
127
150
@@ -132,25 +155,32 @@ impl Drop for TestCustomMessageHandler {
132
155
return ;
133
156
}
134
157
}
135
- assert ! ( self . expected_messages . lock( ) . unwrap( ) . is_empty( ) ) ;
158
+ assert ! ( self . expectations . lock( ) . unwrap( ) . is_empty( ) ) ;
136
159
}
137
160
}
138
161
139
162
impl CustomOnionMessageHandler for TestCustomMessageHandler {
140
163
type CustomMessage = TestCustomMessage ;
141
164
fn handle_custom_message ( & self , msg : Self :: CustomMessage , responder : Option < Responder > ) -> ResponseInstruction < Self :: CustomMessage > {
142
- match self . expected_messages . lock ( ) . unwrap ( ) . pop_front ( ) {
143
- Some ( expected_msg) => assert_eq ! ( expected_msg, msg) ,
144
- None => panic ! ( "Unexpected message: {:?}" , msg) ,
145
- }
146
- let response_option = match msg {
147
- TestCustomMessage :: Ping => Some ( TestCustomMessage :: Pong ) ,
148
- TestCustomMessage :: Pong => None ,
165
+ let expectation = self . get_next_expectation ( ) ;
166
+ assert_eq ! ( msg, expectation. expect) ;
167
+
168
+ let response = match msg {
169
+ TestCustomMessage :: Ping => TestCustomMessage :: Pong ,
170
+ TestCustomMessage :: Pong => TestCustomMessage :: Ping ,
149
171
} ;
150
- if let ( Some ( response) , Some ( responder) ) = ( response_option, responder) {
151
- responder. respond ( response)
152
- } else {
153
- ResponseInstruction :: NoResponse
172
+
173
+ // Sanity check: expecting to include reply path when responder is absent should panic.
174
+ if expectation. include_reply_path && responder. is_none ( ) {
175
+ panic ! ( "Expected to include a reply_path, but the responder was absent." )
176
+ }
177
+
178
+ match responder {
179
+ Some ( responder) if expectation. include_reply_path => {
180
+ responder. respond_with_reply_path ( response)
181
+ } ,
182
+ Some ( responder) => responder. respond ( response) ,
183
+ None => ResponseInstruction :: NoResponse ,
154
184
}
155
185
}
156
186
fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , DecodeError > where Self : Sized {
0 commit comments