Skip to content

Commit 31a80cd

Browse files
authored
fix tests helpers: parse rpc server addr (#552)
1 parent dad4ca0 commit 31a80cd

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

tests/common.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use staking_miner::opt::Chain;
22
use std::{
33
io::{BufRead, BufReader, Read},
4+
net::SocketAddr,
45
ops::{Deref, DerefMut},
56
process::{self, Child, ChildStderr, ChildStdout},
67
};
@@ -21,20 +22,30 @@ pub fn find_ws_url_from_output(read: impl Read + Send) -> (String, String) {
2122

2223
let ws_url = BufReader::new(read)
2324
.lines()
25+
.take(50)
2426
.find_map(|line| {
2527
let line =
2628
line.expect("failed to obtain next line from stdout for WS address discovery");
2729
log::info!("{}", line);
2830

2931
data.push_str(&line);
3032

31-
// does the line contain our port (we expect this specific output from substrate).
32-
let sock_addr = match line.split_once("Running JSON-RPC WS server: addr=") {
33-
None => return None,
34-
Some((_, after)) => after.split_once(",").unwrap().0,
35-
};
33+
// Read socketaddr from output "Running JSON-RPC server: addr=127.0.0.1:9944, allowed origins=["*"]"
34+
let line_end = line
35+
.rsplit_once("Running JSON-RPC WS server: addr=")
36+
// newest message (jsonrpsee merging http and ws servers):
37+
.or_else(|| line.rsplit_once("Running JSON-RPC server: addr="))
38+
.map(|(_, line)| line)?;
3639

37-
Some(format!("ws://{}", sock_addr))
40+
// get the socketaddr only.
41+
let addr_str = line_end.split_once(",").unwrap().0;
42+
43+
// expect a valid sockaddr.
44+
let addr: SocketAddr = addr_str
45+
.parse()
46+
.unwrap_or_else(|_| panic!("valid SocketAddr expected but got '{addr_str}'"));
47+
48+
Some(format!("ws://{}", addr))
3849
})
3950
.expect("We should get a WebSocket address");
4051

0 commit comments

Comments
 (0)