flowr 1.0.0

Runners for compiled 'flow' programs
Documentation
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
pub mod test {
    use std::sync::{Arc, Mutex};

    use portpicker::pick_unused_port;

    use crate::gui::client_connection::{discover_service, ClientConnection};
    use crate::gui::client_message::ClientMessage;
    use crate::gui::coordinator_connection::{
        enable_service_discovery, CoordinatorConnection, WAIT,
    };
    use crate::gui::coordinator_message::CoordinatorMessage;

    pub fn wait_for_then_send(
        wait_for_message: CoordinatorMessage,
        then_send: ClientMessage,
    ) -> Arc<Mutex<CoordinatorConnection>> {
        let test_port = pick_unused_port().expect("No ports free");
        let service_name = format!("test-{test_port}");
        let server_connection = Arc::new(Mutex::new(
            CoordinatorConnection::new(&service_name, test_port)
                .expect("Could not create server connection"),
        ));
        let _mdns = enable_service_discovery(&service_name, test_port)
            .expect("Could not enable service discovery");

        let connection = server_connection
            .lock()
            .expect("Could not get access to server connection");

        let server_address = discover_service(&service_name).expect("Could not discover service");
        let client_connection =
            ClientConnection::new(&server_address).expect("Could not create ClientConnection");

        client_connection
            .send(ClientMessage::Ack)
            .expect("Could not send initial 'Ack' message");

        // background thread that acts as a client that waits for the "wait_for_message" to be sent
        // to it from the server, and once received it replies with the "then_send" message to the server
        std::thread::spawn(move || loop {
            match client_connection.receive::<CoordinatorMessage>() {
                Ok(received_message) => {
                    if std::mem::discriminant(&received_message)
                        == std::mem::discriminant(&wait_for_message)
                    {
                        client_connection
                            .send(then_send)
                            .expect("Could not send ClientMessage");

                        return;
                    }
                }
                _ => panic!("Error receiving ServerMessage"),
            }
        });

        // Get the initial Ack sent from client to open the connection
        connection
            .receive::<ClientMessage>(WAIT)
            .expect("Could not receive initial Ack message from client");

        server_connection.clone()
    }
}