Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Implement an integration test for simple-server #357

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ arc-swap = "1.6.0"
[dev-dependencies]
tempdir = "0.3"
serde_json = "1.0"
socket2 = "0.5.6"

# Include console-logging when building tests
opcua = { path = ".", features = ["console-logging"] }
104 changes: 104 additions & 0 deletions lib/tests/simple_server_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use socket2::{Domain, Socket, Type};
use std::io;
use std::net::SocketAddr;
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};

async fn build_sample(sample_dir: &str) {
let mut cargo_command = Command::new("cargo");
cargo_command.arg("build");

if cfg!(feature = "test-vendored-openssl") {
cargo_command.args(&["--features", "test-vendored-openssl"]);
}

let status = cargo_command
.current_dir(sample_dir)
.status()
.expect("Failed to build sample");

assert!(
status.success(),
"Failed to build sample in: {}",
sample_dir
);
}

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",
))
}

#[cfg(target_os = "linux")]
#[tokio::test]
async fn test_simple_server_binds_port() {
build_sample("../samples/simple-server").await;

// Start the server in the background
let mut cargo_command = Command::new("cargo");
cargo_command.arg("run");

if cfg!(feature = "test-vendored-openssl") {
cargo_command.args(&["--features", "test-vendored-openssl"]);
}

let mut server_process = cargo_command
.current_dir("../samples/simple-server/")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start server");

let expected_server_port = 4855;
let res = wait_for_function_to_pass(|| is_port_bound(expected_server_port), 5000);

server_process.kill().expect("Failed to kill server");

match res {
Ok(_) => println!("Port was bound within 5 seconds."),
Err(e) => {
panic!(
"Failed to assert port {} binding within the timeout period: {}",
expected_server_port, e
);
}
}
}
4 changes: 4 additions & 0 deletions samples/simple-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ log = "0.4"
path = "../../lib"
version = "0.13.0" # OPCUARustVersion
features = ["server", "console-logging"]

[features]
default = []
test-vendored-openssl = ["opcua/test-vendored-openssl"]
Loading