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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Subscription functionality for the Tendermint RPC mock client.

use crate::client::subscription::{SubscriptionDriverCmd, SubscriptionRouter};
use crate::client::sync::{unbounded, ChannelRx, ChannelTx};
use crate::event::Event;
use crate::query::Query;
use crate::{Error, Result, Subscription, SubscriptionClient, SubscriptionId};
use async_trait::async_trait;
use tokio::task::JoinHandle;

/// A mock client that facilitates [`Event`] subscription.
///
/// Creating a `MockSubscriptionClient` will immediately spawn an asynchronous
/// driver task that handles routing of incoming [`Event`]s. The
/// `MockSubscriptionClient` then effectively becomes a handle to the
/// asynchronous driver.
///
/// [`Event`]: event/struct.Event.html
#[derive(Debug)]
pub struct MockSubscriptionClient {
    driver_hdl: JoinHandle<Result<()>>,
    event_tx: ChannelTx<Event>,
    cmd_tx: ChannelTx<SubscriptionDriverCmd>,
}

#[async_trait]
impl SubscriptionClient for MockSubscriptionClient {
    async fn subscribe(&mut self, query: Query) -> Result<Subscription> {
        let (event_tx, event_rx) = unbounded();
        let (result_tx, mut result_rx) = unbounded();
        let id = SubscriptionId::default();
        self.send_cmd(SubscriptionDriverCmd::Subscribe {
            id: id.clone(),
            query: query.clone(),
            event_tx,
            result_tx,
        })
        .await?;
        result_rx.recv().await.ok_or_else(|| {
            Error::client_internal_error(
                "failed to receive subscription confirmation from mock client driver",
            )
        })??;

        Ok(Subscription::new(id, query, event_rx, self.cmd_tx.clone()))
    }
}

impl MockSubscriptionClient {
    /// Publish the given event to all subscribers whose queries match that of
    /// the event.
    pub async fn publish(&mut self, ev: Event) -> Result<()> {
        self.event_tx.send(ev).await
    }

    async fn send_cmd(&mut self, cmd: SubscriptionDriverCmd) -> Result<()> {
        self.cmd_tx.send(cmd).await
    }

    /// Attempt to gracefully close this client.
    pub async fn close(mut self) -> Result<()> {
        self.send_cmd(SubscriptionDriverCmd::Terminate).await?;
        self.driver_hdl.await.map_err(|e| {
            Error::client_internal_error(format!(
                "failed to terminate mock client driver task: {}",
                e
            ))
        })?
    }
}

impl Default for MockSubscriptionClient {
    fn default() -> Self {
        let (event_tx, event_rx) = unbounded();
        let (cmd_tx, cmd_rx) = unbounded();
        let driver = MockSubscriptionClientDriver::new(event_rx, cmd_rx);
        let driver_hdl = tokio::spawn(async move { driver.run().await });
        Self {
            driver_hdl,
            event_tx,
            cmd_tx,
        }
    }
}

#[derive(Debug)]
struct MockSubscriptionClientDriver {
    event_rx: ChannelRx<Event>,
    cmd_rx: ChannelRx<SubscriptionDriverCmd>,
    router: SubscriptionRouter,
}

impl MockSubscriptionClientDriver {
    fn new(event_rx: ChannelRx<Event>, cmd_rx: ChannelRx<SubscriptionDriverCmd>) -> Self {
        Self {
            event_rx,
            cmd_rx,
            router: SubscriptionRouter::default(),
        }
    }

    async fn run(mut self) -> Result<()> {
        loop {
            tokio::select! {
                Some(ev) = self.event_rx.recv() => self.router.publish(ev).await,
                Some(cmd) = self.cmd_rx.recv() => match cmd {
                    SubscriptionDriverCmd::Subscribe {
                        id,
                        query,
                        event_tx,
                        result_tx,
                    } => self.subscribe(id, query, event_tx, result_tx).await?,
                    SubscriptionDriverCmd::Unsubscribe {
                        id,
                        query,
                        result_tx,
                    } => self.unsubscribe(id, query, result_tx).await?,
                    SubscriptionDriverCmd::Terminate => return Ok(()),
                },
            }
        }
    }

    async fn subscribe(
        &mut self,
        id: SubscriptionId,
        query: impl ToString,
        event_tx: ChannelTx<Result<Event>>,
        mut result_tx: ChannelTx<Result<()>>,
    ) -> Result<()> {
        self.router.add(&id, query.to_string(), event_tx);
        result_tx.send(Ok(())).await
    }

    async fn unsubscribe(
        &mut self,
        id: SubscriptionId,
        query: impl ToString,
        mut result_tx: ChannelTx<Result<()>>,
    ) -> Result<()> {
        self.router.remove(&id, query.to_string());
        result_tx.send(Ok(())).await
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::query::EventType;
    use crate::Response;
    use futures::StreamExt;
    use std::path::PathBuf;
    use tokio::fs;

    async fn read_json_fixture(name: &str) -> String {
        fs::read_to_string(PathBuf::from("./tests/support/").join(name.to_owned() + ".json"))
            .await
            .unwrap()
    }

    async fn read_event(name: &str) -> Event {
        Event::from_string(&read_json_fixture(name).await).unwrap()
    }

    fn take_from_subs_and_terminate(
        mut subs: Subscription,
        count: usize,
    ) -> JoinHandle<Vec<Result<Event>>> {
        tokio::spawn(async move {
            let mut res = Vec::new();
            while let Some(res_ev) = subs.next().await {
                res.push(res_ev);
                if res.len() >= count {
                    break;
                }
            }
            subs.terminate().await.unwrap();
            res
        })
    }

    #[tokio::test]
    async fn mock_subscription_client() {
        let mut client = MockSubscriptionClient::default();
        let event1 = read_event("event_new_block_1").await;
        let event2 = read_event("event_new_block_2").await;
        let event3 = read_event("event_new_block_3").await;
        let events = vec![event1, event2, event3];

        let subs1 = client.subscribe(EventType::NewBlock.into()).await.unwrap();
        let subs2 = client.subscribe(EventType::NewBlock.into()).await.unwrap();
        assert_ne!(subs1.id, subs2.id);

        let subs1_events = take_from_subs_and_terminate(subs1, 3);
        let subs2_events = take_from_subs_and_terminate(subs2, 3);
        for ev in &events {
            client.publish(ev.clone()).await.unwrap();
        }

        let subs1_events = subs1_events.await.unwrap();
        let subs2_events = subs2_events.await.unwrap();

        assert_eq!(3, subs1_events.len());
        assert_eq!(3, subs2_events.len());

        for i in 0..3 {
            assert!(events[i].eq(subs1_events[i].as_ref().unwrap()));
        }

        client.close().await.unwrap();
    }
}