1
use std::time::Duration;
2

            
3
use tokio::sync::{OwnedSemaphorePermit, mpsc};
4

            
5
use super::*;
6

            
7
mod close_tests;
8
mod inflight_tests;
9
mod limit_tests;
10
mod refusal_tests;
11
mod reply_tests;
12
mod stream_tests;
13

            
14
struct Rig {
15
    mux: Mux,
16
    replies: mpsc::Receiver<String>,
17
    refusals: mpsc::Sender<String>,
18
}
19

            
20
/// A reply in time, or `None` once the channel ended: no test waits for ever.
21
110
async fn recv(replies: &mut mpsc::Receiver<String>) -> Option<String> {
22
110
    tokio::time::timeout(Duration::from_secs(30), replies.recv())
23
110
        .await
24
110
        .expect("a reply in time")
25
110
}
26

            
27
26
fn rig() -> Rig {
28
26
    rig_with(ScriptCtx::new(uuid::Uuid::nil()))
29
26
}
30

            
31
30
fn rig_with(ctx: ScriptCtx) -> Rig {
32
30
    let (mux, replies) = Mux::new(&Handle::current(), ctx, Session::new).expect("Mux::new");
33
30
    let refusals = mux.replies();
34
30
    Rig {
35
30
        mux,
36
30
        replies,
37
30
        refusals,
38
30
    }
39
30
}
40

            
41
impl Rig {
42
80
    async fn next(&mut self) -> String {
43
80
        recv(&mut self.replies)
44
80
            .await
45
80
            .expect("the reply channel is open")
46
80
    }
47

            
48
    /// Sends the frame as a client of the channel would: a refusal goes to the replies.
49
213
    fn frame(&mut self, frame: &str) {
50
213
        if let Some(refusal) = self.mux.frame(frame) {
51
7
            self.refusals.try_send(refusal).expect("room for a refusal");
52
206
        }
53
213
    }
54

            
55
9
    async fn hold_permit(&self) -> OwnedSemaphorePermit {
56
9
        Arc::clone(&self.mux.permits)
57
9
            .acquire_owned()
58
9
            .await
59
9
            .expect("permit")
60
9
    }
61

            
62
21
    async fn ask(&mut self, frame: &str) -> String {
63
21
        self.frame(frame);
64
21
        self.next().await
65
21
    }
66
}