Skip to main content

syncpond_protocol/
lib.rs

1pub mod protocol;
2
3pub use protocol::{
4    Command,
5    Response,
6    format_command,
7    format_response,
8    parse_command,
9    parse_response,
10    connect_with_auth,
11    connect_with_retry,
12    start_long_lived_tcp,
13    Client,
14    send_command,
15};
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use serde_json::json;
21
22    #[test]
23    fn parse_and_format_room_create() {
24        let cmd = parse_command("ROOM.CREATE").unwrap();
25        assert_eq!(cmd, Command::RoomCreate);
26        let line = format_command(&cmd);
27        assert_eq!(line, "ROOM.CREATE");
28    }
29
30    #[test]
31    fn parse_set_with_json() {
32        let line = "SET 1 public foo {\"a\": 123, \"s\": \"x\"}";
33        let cmd = parse_command(line).unwrap();
34        match cmd {
35            Command::Set { room_id, container, key, value } => {
36                assert_eq!(room_id, 1);
37                assert_eq!(container, "public");
38                assert_eq!(key, "foo");
39                assert_eq!(value, json!({"a": 123, "s": "x"}));
40            }
41            _ => panic!("unexpected command variant"),
42        }
43    }
44
45    #[test]
46    fn parse_and_format_persist_commands() {
47        let s = "PERSIST.SET 1 public foo";
48        let cmd = parse_command(s).unwrap();
49        assert_eq!(cmd, Command::PersistSet { room_id: 1, container: "public".into(), key: "foo".into() });
50        assert_eq!(format_command(&cmd), "PERSIST.SET 1 public foo");
51
52        let s2 = "PERSIST.UNSET 1 public foo";
53        let cmd2 = parse_command(s2).unwrap();
54        assert_eq!(cmd2, Command::PersistUnset { room_id: 1, container: "public".into(), key: "foo".into() });
55        assert_eq!(format_command(&cmd2), "PERSIST.UNSET 1 public foo");
56
57        let s3 = "PERSIST.GET 1 public foo";
58        let cmd3 = parse_command(s3).unwrap();
59        assert_eq!(cmd3, Command::PersistGet { room_id: 1, container: "public".into(), key: "foo".into() });
60        assert_eq!(format_command(&cmd3), "PERSIST.GET 1 public foo");
61    }
62
63    #[test]
64    fn parse_and_format_save_load_commands() {
65        let s = "SAVE 1";
66        let cmd = parse_command(s).unwrap();
67        assert_eq!(cmd, Command::Save { room_id: 1 });
68        assert_eq!(format_command(&cmd), "SAVE 1");
69
70        let s2 = "LOAD 1";
71        let cmd2 = parse_command(s2).unwrap();
72        assert_eq!(cmd2, Command::Load { room_id: 1 });
73        assert_eq!(format_command(&cmd2), "LOAD 1");
74    }
75
76    #[test]
77    fn response_roundtrip() {
78        let r = Response::Ok(Some("{\"a\":1}".to_string()));
79        let s = format_response(&r);
80        assert_eq!(s, "OK {\"a\":1}");
81        let parsed = parse_response(&s).unwrap();
82        assert_eq!(parsed, r);
83    }
84
85    #[tokio::test]
86    async fn connect_and_auth_and_receive() {
87        use tokio::net::TcpListener;
88        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
89        use tokio::time::Duration;
90
91        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
92        let addr = listener.local_addr().unwrap().to_string();
93        let api_key = "test-api-key-xyz";
94
95        let (mut rx, _tx) = start_long_lived_tcp(addr.clone(), api_key.to_string());
96
97        // Accept the incoming connection from our background client and validate auth.
98        let accept = tokio::spawn(async move {
99            let (mut socket, _) = listener.accept().await.unwrap();
100            let mut reader = BufReader::new(&mut socket);
101            let mut line = String::new();
102            reader.read_line(&mut line).await.unwrap();
103            assert_eq!(line.trim_end(), api_key);
104            socket.write_all(b"HELLO\n").await.unwrap();
105            socket.flush().await.unwrap();
106        });
107
108        let got = tokio::time::timeout(Duration::from_secs(2), rx.recv())
109            .await
110            .expect("timeout waiting for line")
111            .expect("channel closed");
112        assert_eq!(got, "HELLO");
113
114        accept.await.unwrap();
115    }
116
117    #[tokio::test]
118    async fn client_wrapper_send_and_shutdown() {
119        use tokio::net::TcpListener;
120        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
121        use tokio::time::Duration;
122
123        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
124        let addr = listener.local_addr().unwrap().to_string();
125        let api_key = "test-api-key-xyz".to_string();
126
127        // Start the client wrapper which spawns a background task.
128        let client = Client::start(addr.clone(), api_key.clone());
129
130        // Accept and validate auth + command from client in a background task.
131        let accept = tokio::spawn(async move {
132            let (mut socket, _) = listener.accept().await.unwrap();
133            let mut reader = BufReader::new(&mut socket);
134            let mut line = String::new();
135            // auth
136            reader.read_line(&mut line).await.unwrap();
137            assert_eq!(line.trim_end(), api_key);
138
139            // read command sent by client
140            let mut cmd_line = String::new();
141            reader.read_line(&mut cmd_line).await.unwrap();
142            assert_eq!(cmd_line.trim_end(), "ROOM.CREATE");
143        });
144
145        // Send a command from the client wrapper.
146        client.send(Command::RoomCreate).await.unwrap();
147
148        // Wait for the server task to accept the connection and receive the command,
149        // then shut down the client.
150        accept.await.unwrap();
151        client.shutdown().await.unwrap();
152    }
153}