|
1 |
| -use std::sync::Arc; |
2 | 1 | use redis::aio::MultiplexedConnection;
|
3 |
| -use redis::{Client}; |
| 2 | +use redis::Client; |
| 3 | +use std::sync::Arc; |
4 | 4 | use tokio::sync::Mutex;
|
5 | 5 |
|
6 | 6 | pub type FlowStore = Arc<Mutex<Box<MultiplexedConnection>>>;
|
7 | 7 |
|
8 |
| -fn build_connection(redis_url: String) -> Client { |
| 8 | +pub fn build_connection(redis_url: String) -> Client { |
9 | 9 | match Client::open(redis_url) {
|
10 | 10 | 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 | + ), |
12 | 15 | }
|
13 | 16 | }
|
14 | 17 |
|
15 | 18 | 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 | + { |
17 | 23 | 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 | + ), |
19 | 28 | };
|
20 |
| - |
| 29 | + |
21 | 30 | Arc::new(Mutex::new(Box::new(client)))
|
22 | 31 | }
|
23 | 32 |
|
24 |
| - |
25 | 33 | #[cfg(test)]
|
26 | 34 | mod tests {
|
| 35 | + use crate::flow_store::connection::create_flow_store_connection; |
| 36 | + use redis::{AsyncCommands, RedisResult}; |
| 37 | + use serial_test::serial; |
27 | 38 | use testcontainers::core::IntoContainerPort;
|
| 39 | + use testcontainers::core::WaitFor; |
28 | 40 | use testcontainers::runners::AsyncRunner;
|
29 | 41 | use testcontainers::GenericImage;
|
30 |
| - use testcontainers::core::WaitFor; |
31 |
| - use crate::flow_store::connection::build_connection; |
32 | 42 |
|
33 | 43 | macro_rules! redis_container_test {
|
34 | 44 | ($test_name:ident, $consumer:expr) => {
|
35 |
| - |
36 | 45 | #[tokio::test]
|
| 46 | + #[serial] |
37 | 47 | async fn $test_name() {
|
38 | 48 | let port: u16 = 6379;
|
39 | 49 | let image_name = "redis";
|
40 | 50 | let wait_message = "Ready to accept connections";
|
41 |
| - |
| 51 | + |
42 | 52 | let container = GenericImage::new(image_name, "latest")
|
43 | 53 | .with_exposed_port(port.tcp())
|
44 | 54 | .with_wait_for(WaitFor::message_on_stdout(wait_message))
|
45 | 55 | .start()
|
46 | 56 | .await
|
47 | 57 | .unwrap();
|
48 |
| - |
| 58 | + |
49 | 59 | let host = container.get_host().await.unwrap();
|
50 | 60 | let host_port = container.get_host_port_ipv4(port).await.unwrap();
|
51 | 61 | let url = format!("redis://{host}:{host_port}");
|
52 |
| - |
| 62 | + |
53 | 63 | $consumer(url).await;
|
| 64 | + |
| 65 | + let _ = container.stop().await; |
54 | 66 | }
|
55 | 67 | };
|
56 | 68 | }
|
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 | + ); |
66 | 90 | }
|
0 commit comments