Skip to content

Commit

Permalink
test: Test for subscriber to print value changes to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
AiyionPrime authored and philipp-caspers committed Jul 8, 2024
1 parent 68b9f5c commit 13999c8
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/test_simple_opc_ua_subscriber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
mod common;
mod helpers;

use common::is_port_bound;
use common::wait_for_function_to_pass;
use std::process::Stdio;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command as TokioCommand;
use tokio::time::{timeout, Duration as TokioDuration};

#[tokio::test]
async fn test_simple_subscriber_receives_data_changes() {
// Start the server in the background
helpers::opc_ua_publisher_single_linear::run_rapid_clock().await;

let expected_server_port = 4855;
wait_for_function_to_pass(|| is_port_bound(expected_server_port), 5000)
.expect("Port was not bound within 5 seconds.");

let mut client_process = TokioCommand::new("cargo")
.arg("run")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start client");

let client_stdout = client_process
.stdout
.take()
.expect("Failed to capture client stdout");
let mut reader = BufReader::new(client_stdout).lines();

let mut found_ticks_since_launch_changed_times = 0u32;
let expected_changed_times = 10;

let read_task = tokio::spawn(async move {
while let Some(line) = reader
.next_line()
.await
.expect("Failed to read line from client")
{
println!("Subscriber stdout: {}", line);
if line.contains("Item \"ns=2;s=ticks_since_launch\", Value = Int32(") {
found_ticks_since_launch_changed_times += 1;
if found_ticks_since_launch_changed_times == expected_changed_times {
return found_ticks_since_launch_changed_times;
}
}
}
found_ticks_since_launch_changed_times
});

let timeout_duration = TokioDuration::from_secs(5);
match timeout(timeout_duration, read_task).await {
Ok(result) => {
found_ticks_since_launch_changed_times = result.expect("Failed to join read task");
}
Err(_) => {
eprintln!("Test timed out");
}
}

client_process.kill().await.expect("Failed to kill client");

assert_eq!(
found_ticks_since_launch_changed_times, expected_changed_times,
"Client did not output enough value changes in time."
);
}

0 comments on commit 13999c8

Please sign in to comment.