Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Fixed tests failure in StreamReader
Browse files Browse the repository at this point in the history
caused by parallel-runned tests opening two simultaneuos TCP sockets on the same port
  • Loading branch information
dr-orlovsky committed Feb 4, 2019
1 parent 50bb68d commit ff85da8
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/network/stream_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ mod test {
}
}

fn serve_tcp(pieces: Vec<Vec<u8>>) {
let listener = TcpListener::bind("127.0.0.1:34254").unwrap();
fn serve_tcp(port: u16, pieces: Vec<Vec<u8>>) {
let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap();
for ostream in listener.incoming() {
let mut ostream = ostream.unwrap();

Expand All @@ -286,12 +286,13 @@ mod test {

#[test]
fn read_multipartmsg_test() {
let handle = thread::spawn(|| {
serve_tcp(vec![MSG_VERSION[..24].to_vec(), MSG_VERSION[24..].to_vec()]);
let port: u16 = 34254;
let handle = thread::spawn(move || {
serve_tcp(port, vec![MSG_VERSION[..24].to_vec(), MSG_VERSION[24..].to_vec()]);
});

thread::sleep(Duration::from_secs(1));
let mut istream = TcpStream::connect("127.0.0.1:34254").unwrap();
let mut istream = TcpStream::connect(format!("127.0.0.1:{}", port)).unwrap();
let messages = StreamReader::new(&mut istream, None).read_messages().unwrap();
assert_eq!(messages.len(), 1);

Expand All @@ -303,8 +304,9 @@ mod test {

#[test]
fn read_sequencemsg_test() {
let handle = thread::spawn(|| {
serve_tcp(vec![
let port: u16 = 34255;
let handle = thread::spawn(move || {
serve_tcp(port, vec![
// Real-world Bitcoin core communication case for /Satoshi:0.17.1/
MSG_VERSION[..23].to_vec(), MSG_VERSION[23..].to_vec(),
MSG_VERACK.to_vec(),
Expand All @@ -313,7 +315,7 @@ mod test {
});

thread::sleep(Duration::from_secs(1));
let mut istream = TcpStream::connect("127.0.0.1:34254").unwrap();
let mut istream = TcpStream::connect(format!("127.0.0.1:{}", port)).unwrap();

let messages = StreamReader::new(&mut istream, None).read_messages().unwrap();
assert_eq!(messages.len(), 1);
Expand Down

0 comments on commit ff85da8

Please sign in to comment.