Files
archive/src/tests.rs

39 lines
1.1 KiB
Rust

use std::time::Duration;
use tokio::sync::oneshot;
use crate::{client::Client, config::ServerConfig, server::Server};
#[tokio::test]
async fn expiration() -> Result<(), Box<dyn std::error::Error>> {
let config = ServerConfig::builder().host("127.0.0.1".into()).build();
let mut server = Server::new(&config).await?;
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let server_handle = tokio::spawn(async move { server.run(shutdown_rx).await });
let mut client = client("127.0.0.1:6171").await?;
client
.set("test-key", "test-value".as_bytes(), Some(2))
.await
.unwrap();
assert!(client.has("test-key").await.unwrap());
tokio::time::sleep(Duration::from_secs(1)).await;
assert!(client.has("test-key").await.unwrap());
tokio::time::sleep(Duration::from_secs(2)).await;
assert!(!client.has("test-key").await.unwrap());
shutdown_tx.send(()).unwrap();
server_handle.await??;
Ok(())
}
async fn client<A: tokio::net::ToSocketAddrs>(
addr: A,
) -> Result<Client, Box<dyn std::error::Error>> {
Ok(Client::new(addr).await?)
}