Skip to content

Commit 2e2054a

Browse files
Merge pull request #8 from code0-tech/7-flow-store-tests
7-flow-store-tests
2 parents 403bd5b + e1f18dd commit 2e2054a

File tree

4 files changed

+372
-44
lines changed

4 files changed

+372
-44
lines changed

Cargo.lock

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lapin = "2.5.0"
1818

1919
[dev-dependencies]
2020
testcontainers = "0.23.2"
21+
serial_test = "3.2.0"
2122

2223
[lib]
2324
doctest = true

src/flow_store/connection.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,90 @@
1-
use std::sync::Arc;
21
use redis::aio::MultiplexedConnection;
3-
use redis::{Client};
2+
use redis::Client;
3+
use std::sync::Arc;
44
use tokio::sync::Mutex;
55

66
pub type FlowStore = Arc<Mutex<Box<MultiplexedConnection>>>;
77

8-
fn build_connection(redis_url: String) -> Client {
8+
pub fn build_connection(redis_url: String) -> Client {
99
match Client::open(redis_url) {
1010
Ok(client) => client,
11-
Err(con_error) => panic!("Cannot create FlowStore (Redis) connection! Reason: {}", con_error),
11+
Err(con_error) => panic!(
12+
"Cannot create Client (Redis) connection! Reason: {}",
13+
con_error
14+
),
1215
}
1316
}
1417

1518
pub async fn create_flow_store_connection(url: String) -> FlowStore {
16-
let client = match build_connection(url).get_multiplexed_async_connection().await {
19+
let client = match build_connection(url)
20+
.get_multiplexed_async_connection()
21+
.await
22+
{
1723
Ok(connection) => connection,
18-
Err(error) => panic!("Cannot create FlowStore (Redis) connection! Reason: {}", error),
24+
Err(error) => panic!(
25+
"Cannot create FlowStore (Redis) connection! Reason: {}",
26+
error
27+
),
1928
};
20-
29+
2130
Arc::new(Mutex::new(Box::new(client)))
2231
}
2332

24-
2533
#[cfg(test)]
2634
mod tests {
35+
use crate::flow_store::connection::create_flow_store_connection;
36+
use redis::{AsyncCommands, RedisResult};
37+
use serial_test::serial;
2738
use testcontainers::core::IntoContainerPort;
39+
use testcontainers::core::WaitFor;
2840
use testcontainers::runners::AsyncRunner;
2941
use testcontainers::GenericImage;
30-
use testcontainers::core::WaitFor;
31-
use crate::flow_store::connection::build_connection;
3242

3343
macro_rules! redis_container_test {
3444
($test_name:ident, $consumer:expr) => {
35-
3645
#[tokio::test]
46+
#[serial]
3747
async fn $test_name() {
3848
let port: u16 = 6379;
3949
let image_name = "redis";
4050
let wait_message = "Ready to accept connections";
41-
51+
4252
let container = GenericImage::new(image_name, "latest")
4353
.with_exposed_port(port.tcp())
4454
.with_wait_for(WaitFor::message_on_stdout(wait_message))
4555
.start()
4656
.await
4757
.unwrap();
48-
58+
4959
let host = container.get_host().await.unwrap();
5060
let host_port = container.get_host_port_ipv4(port).await.unwrap();
5161
let url = format!("redis://{host}:{host_port}");
52-
62+
5363
$consumer(url).await;
64+
65+
let _ = container.stop().await;
5466
}
5567
};
5668
}
57-
58-
redis_container_test!(test_redis_startup, (|url: String| async move {
59-
println!("Redis server started correctly on: {}", url);
60-
}));
61-
62-
redis_container_test!(test_redis_connection, (|url: String| async move {
63-
let result = build_connection(url).get_connection();
64-
assert!(result.is_ok());
65-
}));
69+
70+
redis_container_test!(
71+
test_redis_startup,
72+
(|url: String| async move {
73+
println!("Redis server started correctly on: {}", url);
74+
})
75+
);
76+
77+
redis_container_test!(
78+
test_redis_ping,
79+
(|url: String| async move {
80+
println!("Redis server started correctly on: {}", url.clone());
81+
82+
let flow_store = create_flow_store_connection(url.clone()).await;
83+
let mut con = flow_store.lock().await;
84+
85+
let ping_res: RedisResult<String> = con.ping().await;
86+
assert!(ping_res.is_ok());
87+
assert_eq!(ping_res.unwrap(), "PONG");
88+
})
89+
);
6690
}

0 commit comments

Comments
 (0)