Skip to content

Commit fc87488

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 66c214b commit fc87488

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
@@ -111,16 +111,39 @@ impl Writeable for TestCustomMessage {
111111
}
112112

113113
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,
115120
}
116121

117122
impl TestCustomMessageHandler {
118123
fn new() -> Self {
119-
Self { expected_messages: Mutex::new(VecDeque::new()) }
124+
Self { expectations: Mutex::new(VecDeque::new()) }
120125
}
121126

122127
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")
124147
}
125148
}
126149

@@ -131,25 +154,32 @@ impl Drop for TestCustomMessageHandler {
131154
return;
132155
}
133156
}
134-
assert!(self.expected_messages.lock().unwrap().is_empty());
157+
assert!(self.expectations.lock().unwrap().is_empty());
135158
}
136159
}
137160

138161
impl CustomOnionMessageHandler for TestCustomMessageHandler {
139162
type CustomMessage = TestCustomMessage;
140163
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,
148170
};
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,
153183
}
154184
}
155185
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)