Skip to content

Commit

Permalink
Merge pull request #243 from claddyy/testdns
Browse files Browse the repository at this point in the history
Add the dns test
  • Loading branch information
Shourya742 authored Nov 12, 2024
2 parents 47fb73d + 2f9f0aa commit 972585c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/market/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn start_address_writer_thread(
let address_file = directory.data_dir.join("addresses.dat");

let interval = if cfg!(feature = "integration-test") {
60 // 1 minute for tests
3 // 3 seconds for tests
} else {
600 // 10 minutes for production
};
Expand Down
115 changes: 115 additions & 0 deletions tests/dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}

0 comments on commit 972585c

Please sign in to comment.