+ struct TestScorer {
+ event_expectations: Option<VecDeque<TestResult>>,
+ }
+
+ #[derive(Debug)]
+ enum TestResult {
+ PaymentFailure { path: Vec<RouteHop>, short_channel_id: u64 },
+ PaymentSuccess { path: Vec<RouteHop> },
+ ProbeFailure { path: Vec<RouteHop> },
+ ProbeSuccess { path: Vec<RouteHop> },
+ }
+
+ impl TestScorer {
+ fn new() -> Self {
+ Self { event_expectations: None }
+ }
+
+ fn expect(&mut self, expectation: TestResult) {
+ self.event_expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
+ }
+ }
+
+ impl lightning::util::ser::Writeable for TestScorer {
+ fn write<W: lightning::util::ser::Writer>(&self, _: &mut W) -> Result<(), lightning::io::Error> { Ok(()) }
+ }
+
+ impl Score for TestScorer {
+ fn channel_penalty_msat(
+ &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
+ ) -> u64 { unimplemented!(); }
+
+ fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
+ if let Some(expectations) = &mut self.event_expectations {
+ match expectations.pop_front().unwrap() {
+ TestResult::PaymentFailure { path, short_channel_id } => {
+ assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ assert_eq!(actual_short_channel_id, short_channel_id);
+ },
+ TestResult::PaymentSuccess { path } => {
+ panic!("Unexpected successful payment path: {:?}", path)
+ },
+ TestResult::ProbeFailure { path } => {
+ panic!("Unexpected probe failure: {:?}", path)
+ },
+ TestResult::ProbeSuccess { path } => {
+ panic!("Unexpected probe success: {:?}", path)
+ }
+ }
+ }
+ }
+
+ fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
+ if let Some(expectations) = &mut self.event_expectations {
+ match expectations.pop_front().unwrap() {
+ TestResult::PaymentFailure { path, .. } => {
+ panic!("Unexpected payment path failure: {:?}", path)
+ },
+ TestResult::PaymentSuccess { path } => {
+ assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ },
+ TestResult::ProbeFailure { path } => {
+ panic!("Unexpected probe failure: {:?}", path)
+ },
+ TestResult::ProbeSuccess { path } => {
+ panic!("Unexpected probe success: {:?}", path)
+ }
+ }
+ }
+ }
+
+ fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) {
+ if let Some(expectations) = &mut self.event_expectations {
+ match expectations.pop_front().unwrap() {
+ TestResult::PaymentFailure { path, .. } => {
+ panic!("Unexpected payment path failure: {:?}", path)
+ },
+ TestResult::PaymentSuccess { path } => {
+ panic!("Unexpected payment path success: {:?}", path)
+ },
+ TestResult::ProbeFailure { path } => {
+ assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ },
+ TestResult::ProbeSuccess { path } => {
+ panic!("Unexpected probe success: {:?}", path)
+ }
+ }
+ }
+ }
+ fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
+ if let Some(expectations) = &mut self.event_expectations {
+ match expectations.pop_front().unwrap() {
+ TestResult::PaymentFailure { path, .. } => {
+ panic!("Unexpected payment path failure: {:?}", path)
+ },
+ TestResult::PaymentSuccess { path } => {
+ panic!("Unexpected payment path success: {:?}", path)
+ },
+ TestResult::ProbeFailure { path } => {
+ panic!("Unexpected probe failure: {:?}", path)
+ },
+ TestResult::ProbeSuccess { path } => {
+ assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ }
+ }
+ }
+ }
+ }
+
+ impl Drop for TestScorer {
+ fn drop(&mut self) {
+ if std::thread::panicking() {
+ return;
+ }
+
+ if let Some(event_expectations) = &self.event_expectations {
+ if !event_expectations.is_empty() {
+ panic!("Unsatisfied event expectations: {:?}", event_expectations);
+ }
+ }
+ }
+ }
+