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