|
| 1 | +use std::net::SocketAddr; |
| 2 | +use tokio::net::TcpListener; |
| 3 | +use tokio::task::JoinHandle; |
| 4 | +use mini_redis::{client, server}; |
| 5 | + |
| 6 | +/// A basic "hello world" style test. A server instance is started in a |
| 7 | +/// background task. A client instance is then established and set and get |
| 8 | +/// commands are sent to the server. The response is then evaluated |
| 9 | +#[tokio::test] |
| 10 | +async fn key_value_get_set() { |
| 11 | + let (addr, _) = start_server().await; |
| 12 | + |
| 13 | + let mut client = client::connect(addr).await.unwrap(); |
| 14 | + client.set("hello", "world".into()).await.unwrap(); |
| 15 | + |
| 16 | + let value = client.get("hello").await.unwrap().unwrap(); |
| 17 | + assert_eq!(b"world", &value[..]) |
| 18 | +} |
| 19 | + |
| 20 | +/// similar to the "hello world" style test, But this time |
| 21 | +/// a single channel subscription will be tested instead |
| 22 | +#[tokio::test] |
| 23 | +async fn receive_message_subscribed_channel() { |
| 24 | + let (addr, _) = start_server().await; |
| 25 | + |
| 26 | + let client = client::connect(addr.clone()).await.unwrap(); |
| 27 | + let mut subscriber = client.subscribe(vec!["hello".into()]).await.unwrap(); |
| 28 | + |
| 29 | + tokio::spawn(async move { |
| 30 | + let mut client = client::connect(addr).await.unwrap(); |
| 31 | + client.publish("hello", "world".into()).await.unwrap() |
| 32 | + }); |
| 33 | + |
| 34 | + let message = subscriber.next_message().await.unwrap(); |
| 35 | + assert_eq!("hello", &message.channel); |
| 36 | + assert_eq!(b"world", &message.content[..]) |
| 37 | +} |
| 38 | + |
| 39 | +/// test that a client gets messages from multiple subscribed channels |
| 40 | +#[tokio::test] |
| 41 | +async fn receive_message_multiple_subscribed_channels() { |
| 42 | + let (addr, _) = start_server().await; |
| 43 | + |
| 44 | + let client = client::connect(addr.clone()).await.unwrap(); |
| 45 | + let mut subscriber = client.subscribe(vec!["hello".into(), "world".into()]).await.unwrap(); |
| 46 | + |
| 47 | + tokio::spawn(async move { |
| 48 | + let mut client = client::connect(addr).await.unwrap(); |
| 49 | + client.publish("hello", "world".into()).await.unwrap() |
| 50 | + }); |
| 51 | + |
| 52 | + let message1 = subscriber.next_message().await.unwrap(); |
| 53 | + assert_eq!("hello", &message1.channel); |
| 54 | + assert_eq!(b"world", &message1.content[..]); |
| 55 | + |
| 56 | + tokio::spawn(async move { |
| 57 | + let mut client = client::connect(addr).await.unwrap(); |
| 58 | + client.publish("world", "howdy?".into()).await.unwrap() |
| 59 | + }); |
| 60 | + |
| 61 | + |
| 62 | + let message2 = subscriber.next_message().await.unwrap(); |
| 63 | + assert_eq!("world", &message2.channel); |
| 64 | + assert_eq!(b"howdy?", &message2.content[..]) |
| 65 | +} |
| 66 | + |
| 67 | +/// test that a client accurately removes its own subscribed chanel list |
| 68 | +/// when unbscribing to all subscribed channels by submitting an empty vec |
| 69 | +#[tokio::test] |
| 70 | +async fn unsubscribes_from_channels() { |
| 71 | + let (addr, _) = start_server().await; |
| 72 | + |
| 73 | + let client = client::connect(addr.clone()).await.unwrap(); |
| 74 | + let mut subscriber = client.subscribe(vec!["hello".into(), "world".into()]).await.unwrap(); |
| 75 | + |
| 76 | + subscriber.unsubscribe(vec![]).await.unwrap(); |
| 77 | + assert_eq!(subscriber.get_subscribed().len(), 0); |
| 78 | +} |
| 79 | + |
| 80 | +async fn start_server() -> (SocketAddr, JoinHandle<mini_redis::Result<()>>) { |
| 81 | + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 82 | + let addr = listener.local_addr().unwrap(); |
| 83 | + |
| 84 | + let handle = tokio::spawn(async move { |
| 85 | + server::run(listener, tokio::signal::ctrl_c()).await |
| 86 | + }); |
| 87 | + |
| 88 | + (addr, handle) |
| 89 | +} |
| 90 | + |
0 commit comments