Skip to content

Commit

Permalink
nexus: fix tests breaking unless single threaded
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Dec 28, 2023
1 parent efef9a6 commit a1b4c3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
working-directory: ./nexus

- name: cargo test
run: cargo test -- --test-threads=1
run: cargo test
working-directory: ./nexus
env:
RUST_BACKTRACE: 1
Expand Down
20 changes: 14 additions & 6 deletions nexus/server/tests/server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
io::{prelude::*, BufReader, Write},
path::Path,
process::Command,
sync::atomic::{AtomicU16, Ordering},
thread,
time::Duration,
};
Expand Down Expand Up @@ -38,13 +39,20 @@ fn read_queries(filename: impl AsRef<Path>) -> Vec<String> {

struct PeerDBServer {
server: std::process::Child,
port: u16,
}

impl PeerDBServer {
fn new() -> Self {
static PORT_OFFSET_COUNTER: AtomicU16 = AtomicU16::new(0);

let port_offset = PORT_OFFSET_COUNTER.fetch_add(1, Ordering::Relaxed);
let server_port = 9900 + port_offset;
let server_port_str = server_port.to_string();
let console_bind = format!("127.0.0.1:{}", 6669 + port_offset);
let mut server_start = Command::new("cargo");
server_start.envs(std::env::vars());
server_start.args(["run"]);
server_start.envs(std::env::vars().into_iter().chain([("TOKIO_CONSOLE_BIND".into(), console_bind)].iter().cloned()));
server_start.args(["run", "--port", &server_port_str]);
tracing::info!("Starting server...");

let f = File::create("server.log").expect("unable to open server.log");
Expand All @@ -55,12 +63,12 @@ impl PeerDBServer {

thread::sleep(Duration::from_millis(5000));
tracing::info!("peerdb-server Server started");
Self { server: child }
Self { server: child, port: server_port }
}

fn connect_dying(&self) -> Client {
let connection_string = "host=localhost port=9900 password=peerdb user=peerdb";
let mut client_result = Client::connect(connection_string, NoTls);
let connection_string = format!("host=localhost port={} password=peerdb user=peerdb", self.port);
let mut client_result = Client::connect(&connection_string, NoTls);

let mut client_established = false;
let max_attempts = 10;
Expand All @@ -73,7 +81,7 @@ impl PeerDBServer {
Err(_) => {
attempts += 1;
thread::sleep(Duration::from_millis(2000 * attempts));
client_result = Client::connect(connection_string, NoTls);
client_result = Client::connect(&connection_string, NoTls);
}
}
}
Expand Down

0 comments on commit a1b4c3d

Please sign in to comment.