Skip to content

Commit b95cfed

Browse files
committed
Refactor TestCustomMessageHandler
- Introduce a new struct for keeping expectations organized. - Add a boolean field to track whether a response is expected, and hence whether a `reply_path` should be included with the response. - Update Ping and Pong roles for bidirectional communication. - Introduce panic for when there is no responder and we were expecting to include a `reply_path`. - Refactor `handle_custom_message` code.
1 parent 7d5dd6b commit b95cfed

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,39 @@ impl Writeable for TestCustomMessage {
112112
}
113113

114114
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,
116121
}
117122

118123
impl TestCustomMessageHandler {
119124
fn new() -> Self {
120-
Self { expected_messages: Mutex::new(VecDeque::new()) }
125+
Self { expectations: Mutex::new(VecDeque::new()) }
121126
}
122127

123128
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")
125148
}
126149
}
127150

@@ -132,25 +155,32 @@ impl Drop for TestCustomMessageHandler {
132155
return;
133156
}
134157
}
135-
assert!(self.expected_messages.lock().unwrap().is_empty());
158+
assert!(self.expectations.lock().unwrap().is_empty());
136159
}
137160
}
138161

139162
impl CustomOnionMessageHandler for TestCustomMessageHandler {
140163
type CustomMessage = TestCustomMessage;
141164
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,
149171
};
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,
154184
}
155185
}
156186
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

Comments
 (0)