1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use futures::StreamExt;
use log::*;
use socket_flow::error::Error;
use socket_flow::handshake::connect_async;

const AGENT: &str = "socket-flow";

async fn run_test(case: u32) -> Result<(), Error> {
    info!("Running test case {}", case);
    let case_url = &format!("ws://127.0.0.1:9001/runCase?case={}&agent={}", case, AGENT);
    let mut connection = connect_async(case_url).await?;
    while let Some(msg) = connection.next().await {
        let msg = msg?;
        connection.send_message(msg).await?;
    }

    Ok(())
}

async fn update_reports() -> Result<(), Error> {
    info!("updating reports");
    let mut connection = connect_async(&format!(
        "ws://127.0.0.1:9001/updateReports?agent={}",
        AGENT
    ))
    .await?;
    info!("closing connection");
    connection.close_connection().await?;
    Ok(())
}

async fn get_case_count() -> Result<u32, Error> {
    let mut connection = connect_async("ws://localhost:9001/getCaseCount").await?;

    // Receive a single message
    let msg = connection.next().await.unwrap()?;
    connection.close_connection().await?;

    let text_message = msg.as_text()?;
    Ok(text_message
        .parse::<u32>()
        .expect("couldn't convert test case to number"))
}

#[tokio::main]
async fn main() {
    env_logger::init();

    let total = get_case_count().await.expect("Error getting case count");

    for case in 1..=total {
        if let Err(e) = run_test(case).await {
            error!("Testcase {} failed: {}", case, e)
        }
    }

    update_reports().await.expect("Error updating reports");
}