forked from bitcoin-teleport/teleport-transactions
-
Notifications
You must be signed in to change notification settings - Fork 47
/
dns.rs
115 lines (98 loc) · 3.23 KB
/
dns.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::{
io::{BufRead, BufReader, Write},
net::TcpStream,
process::{Child, Command},
sync::{
mpsc,
mpsc::{Receiver, Sender},
},
thread,
time::Duration,
};
fn start_server() -> (Child, Receiver<String>) {
let (log_sender, log_receiver): (Sender<String>, Receiver<String>) = mpsc::channel();
let mut directoryd_process = Command::new("./target/debug/directoryd")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
let stdout = directoryd_process.stdout.take().unwrap();
thread::spawn(move || {
let reader = BufReader::new(stdout);
reader.lines().map_while(Result::ok).for_each(|line| {
log_sender.send(line).unwrap_or_else(|e| {
println!("Failed to send log: {}", e);
});
});
});
(directoryd_process, log_receiver)
}
fn wait_for_server_start(log_receiver: &Receiver<String>) {
let mut server_started = false;
while let Ok(log_message) = log_receiver.recv_timeout(Duration::from_secs(5)) {
if log_message.contains("RPC socket binding successful") {
server_started = true;
break;
}
}
assert!(
server_started,
"Server did not start within the expected time"
);
}
fn send_addresses(addresses: &[&str]) {
for address in addresses {
let mut stream = TcpStream::connect(("127.0.0.1", 8080)).unwrap();
let request = format!("POST {}\n", address);
stream.write_all(request.as_bytes()).unwrap();
}
}
fn verify_addresses(addresses: &[&str]) {
let output = Command::new("./target/debug/directory-cli")
.arg("list-addresses")
.output()
.unwrap();
let addresses_output = String::from_utf8(output.stdout).unwrap();
assert!(
output.stderr.is_empty(),
"Error: {:?}",
String::from_utf8(output.stderr).unwrap()
);
for address in addresses {
assert!(
addresses_output.contains(&address.to_string()),
"Address {} not found",
address
);
}
}
#[test]
fn test_dns() {
let (mut process, receiver) = start_server();
wait_for_server_start(&receiver);
let initial_addresses = vec!["127.0.0.1:8080", "127.0.0.1:8081", "127.0.0.1:8082"];
send_addresses(&initial_addresses);
thread::sleep(Duration::from_secs(10));
verify_addresses(&initial_addresses);
// Persistence check
process.kill().expect("Failed to kill directoryd process");
process.wait().unwrap();
let (mut process, receiver) = start_server();
wait_for_server_start(&receiver);
let additional_addresses = vec!["127.0.0.1:8083", "127.0.0.1:8084"];
send_addresses(&additional_addresses);
thread::sleep(Duration::from_secs(10));
process.kill().expect("Failed to kill directoryd process");
process.wait().unwrap();
let (mut process, receiver) = start_server();
wait_for_server_start(&receiver);
let all_addresses = vec![
"127.0.0.1:8080",
"127.0.0.1:8081",
"127.0.0.1:8082",
"127.0.0.1:8083",
"127.0.0.1:8084",
];
verify_addresses(&all_addresses);
process.kill().expect("Failed to kill directoryd process");
process.wait().unwrap();
}