Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
tests: add helper program to send node events to fake control socket
Lars Wirzenius committed 2 years ago
commit c3eda6c3f1ec37fda3fba6857985813f1dfecc00
parent bb24029ff50edd864b359eb05dc2fde2f4f2f950
1 file changed +71 -0
added src/bin/synthetic-events.rs
@@ -0,0 +1,71 @@
+
// Read node events, expressed as JSON, from files, and output them as
+
// single-line JSON objects to a Unix domain socket.
+
//
+
// This is a helper tool for testing of the CI broker as a whole.
+
//
+
// This program forks and runs itself in the background.
+

+
use std::{
+
    env::current_exe,
+
    fs::{read, remove_file},
+
    io::Write,
+
    os::unix::net::UnixListener,
+
    path::PathBuf,
+
    process::{Command, Stdio},
+
    thread::sleep,
+
    time::Duration,
+
};
+

+
use clap::Parser;
+

+
use radicle::node::Event;
+

+
fn main() -> anyhow::Result<()> {
+
    let args = Args::parse();
+
    let exe = current_exe()?;
+

+
    if args.daemon {
+
        let mut events = vec![];
+
        for filename in args.events.iter() {
+
            let data = read(filename)?;
+
            let e: Event = serde_json::from_slice(&data)?;
+
            let mut e = serde_json::to_string(&e)?;
+
            e.push('\n');
+
            events.push(e);
+
        }
+

+
        let listener = UnixListener::bind(&args.socket)?;
+

+
        if let Some(stream) = listener.incoming().next() {
+
            let mut stream = stream?;
+
            for e in events.iter() {
+
                stream.write_all(e.as_bytes())?;
+
            }
+
        }
+
    } else {
+
        if args.socket.exists() {
+
            remove_file(&args.socket)?;
+
        }
+
        Command::new(exe)
+
            .arg("--daemon")
+
            .arg(&args.socket)
+
            .args(&args.events)
+
            .stdin(Stdio::null())
+
            .stdout(Stdio::null())
+
            .stderr(Stdio::null())
+
            .spawn()?;
+
        while !args.socket.exists() {
+
            sleep(Duration::from_millis(100));
+
        }
+
    }
+

+
    Ok(())
+
}
+

+
#[derive(Debug, Parser)]
+
struct Args {
+
    #[clap(long)]
+
    daemon: bool,
+
    socket: PathBuf,
+
    events: Vec<PathBuf>,
+
}