Skip to main content

rusteron_archive/
testing.rs

1use crate::IntoCString;
2use crate::{Aeron, AeronArchive, AeronArchiveAsyncConnect, AeronArchiveContext, AeronCError, AeronContext};
3use log::info;
4use log::{error, warn};
5use regex::Regex;
6use std::backtrace::Backtrace;
7use std::ffi::CString;
8use std::path::Path;
9use std::process::{Child, Command, ExitStatus, Stdio};
10use std::sync::{Arc, Mutex, Weak};
11use std::thread::sleep;
12use std::time::{Duration, Instant};
13use std::{fs, io, panic, process};
14
15/// Check whether the test is running under Valgrind, used to scale Rust-side
16/// timeouts that would otherwise fire before their C counterpart finishes.
17pub(crate) fn running_under_valgrind() -> bool {
18    std::env::var_os("RUSTERON_VALGRIND").is_some()
19}
20
21/// Return a `Duration` scaled by 3× when running under Valgrind: the same
22/// multiplier the client crate uses for driver / liveness timeouts.
23pub(crate) fn valgrind_timeout(base_secs: u64) -> Duration {
24    if running_under_valgrind() {
25        Duration::from_secs(base_secs.saturating_mul(3))
26    } else {
27        Duration::from_secs(base_secs)
28    }
29}
30
31static ARCHIVE_PROCESSES: Mutex<Vec<Weak<Mutex<Child>>>> = Mutex::new(Vec::new());
32
33pub struct EmbeddedArchiveMediaDriverProcess {
34    child: Arc<Mutex<Child>>,
35    owns_driver: bool,
36    pub aeron_dir: CString,
37    pub archive_dir: CString,
38    pub control_request_channel: String,
39    pub control_response_channel: String,
40    pub recording_events_channel: String,
41}
42
43impl EmbeddedArchiveMediaDriverProcess {
44    /// Builds missing Aeron jars with Gradle and starts an Archive Media Driver.
45    ///
46    /// Uses the Gradle wrapper in `aeron` under `CARGO_MANIFEST_DIR`. The build
47    /// writes jars to `aeron/aeron-all/build/libs`. Java must be on `PATH`.
48    /// The returned guard stops the process and removes its directories on drop.
49    ///
50    /// # Parameters
51    /// - `aeron_dir`: Media Driver directory for IPC and client communication.
52    /// - `archive_dir`: Directory for recordings and archive metadata.
53    /// - `control_request_channel`: URI for archive control requests.
54    /// - `control_response_channel`: URI for archive control responses.
55    /// - `recording_events_channel`: URI for recording event notifications.
56    ///
57    /// # Errors
58    /// Returns an error if the Gradle build or process startup fails.
59    ///
60    /// # Example
61    /// ```
62    /// use rusteron_archive::testing::EmbeddedArchiveMediaDriverProcess;
63    /// let driver = EmbeddedArchiveMediaDriverProcess::build_and_start(
64    ///     "/tmp/aeron-dir",
65    ///     "/tmp/archive-dir",
66    ///     "aeron:udp?endpoint=localhost:8010",
67    ///     "aeron:udp?endpoint=localhost:8011",
68    ///     "aeron:udp?endpoint=localhost:8012",
69    /// ).expect("Failed to build and start Aeron Archive Media Driver");
70    /// ```
71    ///
72    pub fn build_and_start(
73        aeron_dir: &str,
74        archive_dir: &str,
75        control_request_channel: &str,
76        control_response_channel: &str,
77        recording_events_channel: &str,
78    ) -> io::Result<Self> {
79        let path = std::path::MAIN_SEPARATOR;
80        let gradle = if cfg!(target_os = "windows") {
81            &format!("{}{path}aeron{path}gradlew.bat", env!("CARGO_MANIFEST_DIR"),)
82        } else {
83            "./gradlew"
84        };
85        let dir = format!("{}{path}aeron", env!("CARGO_MANIFEST_DIR"),);
86        info!("running {} in {}", gradle, dir);
87
88        if !Path::new(&format!(
89            "{}{path}aeron{path}aeron-all{path}build{path}libs",
90            env!("CARGO_MANIFEST_DIR")
91        ))
92        .exists()
93        {
94            Command::new(&gradle)
95                .current_dir(dir)
96                .args([":aeron-samples:jar", ":aeron-archive:jar", ":aeron-all:build"])
97                .stdout(Stdio::inherit())
98                .stderr(Stdio::inherit())
99                .spawn()?
100                .wait()?;
101        }
102
103        return Self::start(
104            &aeron_dir,
105            archive_dir,
106            control_request_channel,
107            control_response_channel,
108            recording_events_channel,
109        );
110    }
111
112    pub fn run_aeron_stats(&self) -> std::io::Result<Child> {
113        let main_dir = env!("CARGO_MANIFEST_DIR");
114        let dir = format!("{}/{}", main_dir, &self.aeron_dir.to_str().unwrap());
115        info!("running 'just aeron-stat {}'", dir);
116        Command::new("just")
117            .args(["aeron-stat", dir.as_str()])
118            .stdout(Stdio::inherit())
119            .stderr(Stdio::inherit())
120            .spawn()
121    }
122
123    /// An archive context on `aeron` with this archive's control request, control response
124    /// and recording events channels.
125    ///
126    /// # Errors
127    ///
128    /// Returns the Aeron error if the context cannot be created or rejects a setting.
129    pub fn archive_context(&self, aeron: &Aeron) -> Result<AeronArchiveContext, AeronCError> {
130        let context = AeronArchiveContext::new()?;
131        context.set_aeron(aeron)?;
132        context.set_control_request_channel(&self.control_request_channel.as_str().into_c_string())?;
133        context.set_control_response_channel(&self.control_response_channel.as_str().into_c_string())?;
134        context.set_recording_events_channel(&self.recording_events_channel.as_str().into_c_string())?;
135        Ok(context)
136    }
137
138    pub fn archive_connect(&self) -> Result<(AeronArchive, Aeron), io::Error> {
139        let start = Instant::now();
140        let deadline = valgrind_timeout(30);
141        while start.elapsed() < deadline {
142            if let Ok(aeron_context) = AeronContext::new() {
143                aeron_context.set_dir(&self.aeron_dir).expect("invalid dir");
144                aeron_context
145                    .set_client_name(&CString::new("unit_test_client")?)
146                    .expect("invalid client name");
147                if let Ok(aeron) = Aeron::new(&aeron_context) {
148                    if aeron.start().is_ok() {
149                        if let Ok(archive_context) = self.archive_context(&aeron) {
150                            if let Ok(connect) = AeronArchiveAsyncConnect::new_with_aeron(&archive_context, &aeron) {
151                                if let Ok(archive) = connect.poll_blocking(valgrind_timeout(10)) {
152                                    let i = archive.get_archive_id();
153                                    assert!(i > 0);
154                                    info!("aeron archive media driver is up [connected with archive id {i}]");
155                                    sleep(Duration::from_millis(100));
156                                    return Ok((archive, aeron));
157                                };
158                            }
159                        }
160                        error!("aeron error: {}", Aeron::errmsg());
161                    }
162                }
163            }
164            info!("waiting for aeron to start up, retrying...");
165        }
166
167        assert!(start.elapsed() < deadline, "failed to start up aeron media driver");
168
169        return Err(std::io::Error::other("unable to start up aeron media driver client"));
170    }
171
172    /// Starts an Archive Media Driver using the existing Aeron jars.
173    ///
174    /// Removes and recreates `aeron_dir` and `archive_dir` before launching Java.
175    /// Requires jars under `aeron/aeron-all/build/libs` in `CARGO_MANIFEST_DIR`.
176    /// The returned guard stops the process and removes its directories on drop.
177    /// See [`Self::build_and_start`] for the channel parameters.
178    ///
179    /// # Errors
180    /// Returns an error if directory setup, jar discovery or Java startup fails.
181    ///
182    /// # Example
183    /// ```
184    /// use rusteron_archive::testing::EmbeddedArchiveMediaDriverProcess;
185    /// let driver = EmbeddedArchiveMediaDriverProcess::start(
186    ///     "/tmp/aeron-dir",
187    ///     "/tmp/archive-dir",
188    ///     "aeron:udp?endpoint=localhost:8010",
189    ///     "aeron:udp?endpoint=localhost:8011",
190    ///     "aeron:udp?endpoint=localhost:8012",
191    /// ).expect("Failed to start Aeron Archive Media Driver");
192    /// ```
193    ///
194    pub fn start(
195        aeron_dir: &str,
196        archive_dir: &str,
197        control_request_channel: &str,
198        control_response_channel: &str,
199        recording_events_channel: &str,
200    ) -> io::Result<Self> {
201        Self::start_process(
202            aeron_dir,
203            archive_dir,
204            control_request_channel,
205            control_response_channel,
206            recording_events_channel,
207            None,
208        )
209    }
210
211    /// Start only an Archive, preserving the existing shared Media Driver directory.
212    #[cfg(test)]
213    pub(crate) fn start_on_existing_driver(
214        &self,
215        archive_dir: &str,
216        control_request_channel: &str,
217        control_response_channel: &str,
218        recording_events_channel: &str,
219        archive_id: i64,
220    ) -> io::Result<Self> {
221        Self::start_process(
222            self.aeron_dir.to_str().unwrap(),
223            archive_dir,
224            control_request_channel,
225            control_response_channel,
226            recording_events_channel,
227            Some(archive_id),
228        )
229    }
230
231    fn start_process(
232        aeron_dir: &str,
233        archive_dir: &str,
234        control_request_channel: &str,
235        control_response_channel: &str,
236        recording_events_channel: &str,
237        existing_driver_archive_id: Option<i64>,
238    ) -> io::Result<Self> {
239        if existing_driver_archive_id.is_none() {
240            Self::clean_directory(aeron_dir)?;
241        }
242        Self::clean_directory(archive_dir)?;
243
244        // Create the directories before launching Java.
245        fs::create_dir_all(aeron_dir)?;
246        fs::create_dir_all(archive_dir)?;
247
248        let binding = fs::read_dir(format!("{}/aeron/aeron-all/build/libs", env!("CARGO_MANIFEST_DIR")))?
249            .filter(|f| f.is_ok())
250            .map(|f| f.unwrap())
251            .filter(|f| f.file_name().to_string_lossy().to_string().ends_with(".jar"))
252            .next()
253            .unwrap()
254            .path();
255        let mut jar_path = binding.to_str().unwrap();
256        assert!(fs::exists(jar_path).unwrap_or_default());
257        let mut args = vec![];
258        let separator = if cfg!(target_os = "windows") { ";" } else { ":" };
259
260        let combined_jars = format!(
261            "{}{separator}{}",
262            jar_path,
263            jar_path.replace("aeron-all", "aeron-archive")
264        );
265        jar_path = &combined_jars;
266
267        args.push("--add-opens".to_string());
268        args.push("java.base/jdk.internal.misc=ALL-UNNAMED".to_string());
269        args.push("-cp".to_string());
270        args.push(jar_path.to_string());
271        args.push(format!("-Daeron.dir={}", aeron_dir));
272        args.push(format!("-Daeron.archive.dir={}", archive_dir));
273        args.push("-Daeron.spies.simulate.connection=true".to_string());
274        args.push("-Daeron.event.log=all".to_string());
275        args.push("-Daeron.event.log.disable=FRAME_IN,FRAME_OUT".to_string());
276        args.push("-Daeron.event.archive.log=all".to_string());
277        args.push("-Daeron.event.cluster.log=all".to_string());
278        args.push("-Dagrona.disable.bounds.checks=true".to_string());
279        args.push(format!("-Daeron.archive.control.channel={}", control_request_channel));
280        args.push(format!(
281            "-Daeron.archive.control.response.channel={}",
282            control_response_channel
283        ));
284        args.push(format!(
285            "-Daeron.archive.recording.events.channel={}",
286            recording_events_channel
287        ));
288        args.push("-Daeron.archive.replication.channel=aeron:udp?endpoint=localhost:0".to_string());
289        args.push("-Daeron.client.liveness.timeout=60000000000".to_string());
290        args.push("-Daeron.image.liveness.timeout=60000000000".to_string());
291        args.push("-Daeron.publication.unblock.timeout=65000000000".to_string());
292        if let Some(archive_id) = existing_driver_archive_id {
293            args.push(format!("-Daeron.archive.id={archive_id}"));
294            // The first Archive also listens on the default local control stream.
295            args.push("-Daeron.archive.local.control.stream.id=15402".to_string());
296            args.push("io.aeron.archive.Archive".to_string());
297        } else {
298            args.push("io.aeron.archive.ArchivingMediaDriver".to_string());
299        }
300
301        info!("starting archive media driver [\n\tjava {}\n]", args.join(" "));
302
303        let child = Command::new("java")
304            .args(args)
305            .stdout(Stdio::inherit())
306            .stderr(Stdio::inherit())
307            .spawn()?;
308        let child = Arc::new(Mutex::new(child));
309        let mut processes = ARCHIVE_PROCESSES.lock().expect("archive process registry poisoned");
310        processes.retain(|process| process.strong_count() > 0);
311        processes.push(Arc::downgrade(&child));
312        drop(processes);
313
314        info!(
315            "started archive media driver [{:?}",
316            fs::read_dir(aeron_dir)?.collect::<Vec<_>>()
317        );
318
319        Ok(EmbeddedArchiveMediaDriverProcess {
320            child,
321            owns_driver: existing_driver_archive_id.is_none(),
322            aeron_dir: aeron_dir.into_c_string(),
323            archive_dir: archive_dir.into_c_string(),
324            control_request_channel: control_request_channel.to_string(),
325            control_response_channel: control_response_channel.to_string(),
326            recording_events_channel: recording_events_channel.to_string(),
327        })
328    }
329
330    fn clean_directory(dir: &str) -> io::Result<()> {
331        info!("cleaning directory {}", dir);
332        let path = Path::new(dir);
333        if path.exists() {
334            fs::remove_dir_all(path)?;
335        }
336        Ok(())
337    }
338
339    /// Stops only archive Java children launched by this process, never unrelated JVMs.
340    pub fn kill_all_java_processes() -> io::Result<ExitStatus> {
341        let mut processes = ARCHIVE_PROCESSES.lock().expect("archive process registry poisoned");
342        let mut status = ExitStatus::default();
343        for process in processes.iter().filter_map(Weak::upgrade) {
344            let mut child = process.lock().expect("archive child lock poisoned");
345            if let Some(exited) = child.try_wait()? {
346                status = exited;
347            } else {
348                child.kill()?;
349                status = child.wait()?;
350            }
351        }
352        processes.retain(|process| process.strong_count() > 0);
353        Ok(status)
354    }
355}
356
357// Stop the child before removing its directories.
358impl Drop for EmbeddedArchiveMediaDriverProcess {
359    fn drop(&mut self) {
360        warn!("WARN: stopping aeron archive media driver!!!!");
361        let mut child = self.child.lock().expect("archive child lock poisoned");
362        match child.try_wait() {
363            Ok(Some(_)) => {}
364            Ok(None) => {
365                if let Err(e) = child.kill() {
366                    error!("Failed to kill Java process: {}", e);
367                }
368                if let Err(e) = child.wait() {
369                    error!("Failed to reap Java process: {}", e);
370                }
371            }
372            Err(e) => error!("Failed to check Java process: {}", e),
373        }
374
375        // Clean up directories after the process has terminated
376        if self.owns_driver {
377            if let Err(e) = Self::clean_directory(&self.aeron_dir.to_str().unwrap()) {
378                error!("Failed to clean up Aeron directory: {}", e);
379            }
380        }
381        if let Err(e) = Self::clean_directory(&self.archive_dir.to_str().unwrap()) {
382            error!("Failed to clean up Archive directory: {}", e);
383        }
384    }
385}
386
387/// True if a `java` executable is resolvable on `PATH`.
388///
389/// The embedded archive media driver is a Java process, so the
390/// persistent-subscription tests use this to **skip themselves** when Java
391/// isn't installed, instead of being blanket `#[ignore]`d. With Java present
392/// they run normally under `cargo test`; without it they no-op and pass.
393pub fn java_available() -> bool {
394    Command::new("java")
395        .arg("-version")
396        .stdout(Stdio::null())
397        .stderr(Stdio::null())
398        .status()
399        .is_ok()
400}
401
402/// Place at the top of any test that needs the Java archive. Skips the test
403/// (early-returns `Ok(())`) when `java` is not on `PATH`. Requires the test to
404/// return a `Result<(), _>`.
405#[macro_export]
406macro_rules! skip_unless_java {
407    () => {
408        if !$crate::testing::java_available() {
409            eprintln!("skipping: java not available on PATH");
410            return Ok(());
411        }
412    };
413}
414
415pub fn set_panic_hook() {
416    panic::set_hook(Box::new(|info| {
417        // Get the backtrace
418        let backtrace = Backtrace::force_capture();
419        error!("Stack trace: {backtrace:#?}");
420
421        let backtrace = format!("{:?}", backtrace);
422        // Regular expression to match the function, file, and line
423        let re = Regex::new(r#"fn: "([^"]+)", file: "([^"]+)", line: (\d+)"#).unwrap();
424
425        // Extract and print in IntelliJ format with function
426        for cap in re.captures_iter(&backtrace) {
427            let function = &cap[1];
428            let file = &cap[2];
429            let line = &cap[3];
430            info!("{file}:{line} in {function}");
431        }
432
433        error!("Panic occurred: {:#?}", info);
434
435        if let Some(payload) = info.payload().downcast_ref::<&str>() {
436            error!("Panic message: {}", payload);
437        } else if let Some(payload) = info.payload().downcast_ref::<String>() {
438            error!("Panic message: {}", payload);
439        } else {
440            // If it's not a &str or String, try to print it as Debug
441            error!("Panic with non-standard payload: {:?}", info.payload().type_id());
442        }
443
444        warn!("shutdown");
445
446        process::abort();
447    }))
448}
449
450/// First free UDP port at or above `start` (binds a probe socket to check).
451pub fn find_unused_udp_port(start: u16) -> Option<u16> {
452    (start..65535).find(|p| std::net::UdpSocket::bind(("127.0.0.1", *p)).is_ok())
453}
454
455/// Waits for a recording counter in any archive. Prefer
456/// [`find_counter_id_by_session_in_archive_blocking`] when an Archive client is available.
457pub fn find_counter_id_by_session_blocking(
458    counters_reader: &crate::AeronCountersReader,
459    session_id: i32,
460    wait: Duration,
461) -> Result<i32, crate::AeronCError> {
462    find_counter_id_blocking(wait, || {
463        crate::RecordingPos::find_counter_id_by_session(counters_reader, session_id)
464    })
465}
466
467/// Waits for the recording counter belonging to `archive_id` (Aeron 1.54+).
468/// A session may be recorded by multiple Archives sharing the same Media Driver.
469pub fn find_counter_id_by_session_in_archive_blocking(
470    counters_reader: &crate::AeronCountersReader,
471    session_id: i32,
472    archive_id: i64,
473    wait: Duration,
474) -> Result<i32, crate::AeronCError> {
475    find_counter_id_blocking(wait, || {
476        crate::RecordingPos::find_counter_id_by_session_in_archive(counters_reader, session_id, archive_id)
477    })
478}
479
480fn find_counter_id_blocking(wait: Duration, mut find: impl FnMut() -> i32) -> Result<i32, crate::AeronCError> {
481    let start = Instant::now();
482    loop {
483        let counter_id = find();
484        if counter_id >= 0 {
485            return Ok(counter_id);
486        }
487        if start.elapsed() >= wait {
488            return Err(crate::AeronCError::from_code(-1));
489        }
490        std::thread::sleep(Duration::from_millis(10));
491    }
492}