Skip to content

Commit

Permalink
test: Move common test functions to common.rs
Browse files Browse the repository at this point in the history
- is_port_bound
- wait_for_function_to_pass
  • Loading branch information
AiyionPrime authored and philipp-caspers committed Jul 8, 2024
1 parent 48067ad commit 68b9f5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
46 changes: 46 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use socket2::{Domain, Socket, Type};
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use std::{io, thread};

pub fn is_port_bound(port: u16) -> bool {
let socket = match Socket::new(Domain::IPV4, Type::STREAM, None) {
Ok(sock) => sock,
Err(_) => return false,
};

// Set SO_REUSEADDR to allow for the server to bind while this function is testing
if socket.set_reuse_address(true).is_err() {
return false;
}

let addr = SocketAddr::from(([127, 0, 0, 1], port));

// Attempt to bind the socket
match socket.bind(&addr.into()) {
Ok(_) => false, // Successfully bound, so port was not bound
Err(ref e) if e.kind() == io::ErrorKind::AddrInUse => true, // Port is already bound
Err(_) => false, // Other errors are treated as port not bound
}
}

pub fn wait_for_function_to_pass<F>(function: F, timeout_ms: u64) -> Result<(), io::Error>
where
F: Fn() -> bool,
{
let start_time = Instant::now();
let timeout_duration = Duration::from_millis(timeout_ms);
let check_interval = Duration::from_millis(100);

while Instant::now().duration_since(start_time) < timeout_duration {
if function() {
return Ok(());
}
thread::sleep(check_interval);
}

Err(io::Error::new(
io::ErrorKind::TimedOut,
"Expectation fn()==True timed out",
))
}
50 changes: 3 additions & 47 deletions tests/test_helper_opc_ua_publisher.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
use socket2::{Domain, Socket, Type};
use std::io;
use std::net::SocketAddr;
use std::thread;
use std::time::{Duration, Instant};

mod common;
mod helpers;

fn is_port_bound(port: u16) -> bool {
let socket = match Socket::new(Domain::IPV4, Type::STREAM, None) {
Ok(sock) => sock,
Err(_) => return false,
};

// Set SO_REUSEADDR to allow for the server to bind while this function is testing
if socket.set_reuse_address(true).is_err() {
return false;
}

let addr = SocketAddr::from(([127, 0, 0, 1], port));

// Attempt to bind the socket
match socket.bind(&addr.into()) {
Ok(_) => false, // Successfully bound, so port was not bound
Err(ref e) if e.kind() == io::ErrorKind::AddrInUse => true, // Port is already bound
Err(_) => false, // Other errors are treated as port not bound
}
}

fn wait_for_function_to_pass<F>(function: F, timeout_ms: u64) -> Result<(), io::Error>
where
F: Fn() -> bool,
{
let start_time = Instant::now();
let timeout_duration = Duration::from_millis(timeout_ms);
let check_interval = Duration::from_millis(100);

while Instant::now().duration_since(start_time) < timeout_duration {
if function() {
return Ok(());
}
thread::sleep(check_interval);
}

Err(io::Error::new(
io::ErrorKind::TimedOut,
"Expectation fn()==True timed out",
))
}
use common::is_port_bound;
use common::wait_for_function_to_pass;

#[tokio::test]
async fn test_simple_server_binds_port() {
Expand Down

0 comments on commit 68b9f5c

Please sign in to comment.