Skip to content

Commit 29510e1

Browse files
TheBlueMattarik-so
authored andcommitted
Do a real handshake to set up a network in peer_handler tests
1 parent 2454ad7 commit 29510e1

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,15 +1124,28 @@ mod tests {
11241124

11251125
use rand::{thread_rng, Rng};
11261126

1127-
use std::sync::{Arc};
1127+
use std::sync::{Arc, Mutex};
11281128

1129-
#[derive(PartialEq, Eq, Clone, Hash)]
1129+
#[derive(Clone)]
11301130
struct FileDescriptor {
11311131
fd: u16,
1132+
outbound_data: Arc<Mutex<Vec<u8>>>,
1133+
}
1134+
impl PartialEq for FileDescriptor {
1135+
fn eq(&self, other: &Self) -> bool {
1136+
self.fd == other.fd
1137+
}
1138+
}
1139+
impl Eq for FileDescriptor { }
1140+
impl std::hash::Hash for FileDescriptor {
1141+
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
1142+
self.fd.hash(hasher)
1143+
}
11321144
}
11331145

11341146
impl SocketDescriptor for FileDescriptor {
11351147
fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
1148+
self.outbound_data.lock().unwrap().extend_from_slice(data);
11361149
data.len()
11371150
}
11381151

@@ -1173,10 +1186,15 @@ mod tests {
11731186

11741187
fn establish_connection<'a>(peer_a: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler>, peer_b: &PeerManager<FileDescriptor, &'a test_utils::TestChannelMessageHandler>) {
11751188
let secp_ctx = Secp256k1::new();
1176-
let their_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
1177-
let fd = FileDescriptor { fd: 1};
1178-
peer_a.new_inbound_connection(fd.clone()).unwrap();
1179-
peer_a.peers.lock().unwrap().node_id_to_descriptor.insert(their_id, fd.clone());
1189+
let a_id = PublicKey::from_secret_key(&secp_ctx, &peer_a.our_node_secret);
1190+
//let b_id = PublicKey::from_secret_key(&secp_ctx, &peer_b.our_node_secret);
1191+
let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
1192+
let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
1193+
let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone()).unwrap();
1194+
peer_a.new_inbound_connection(fd_a.clone()).unwrap();
1195+
assert_eq!(peer_a.read_event(&mut fd_a, initial_data).unwrap(), false);
1196+
assert_eq!(peer_b.read_event(&mut fd_b, fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
1197+
assert_eq!(peer_a.read_event(&mut fd_a, fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
11801198
}
11811199

11821200
#[test]

0 commit comments

Comments
 (0)