-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rs
More file actions
59 lines (51 loc) · 1.81 KB
/
connection.rs
File metadata and controls
59 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use lapin::Connection;
pub async fn build_connection(rabbitmq_url: &str) -> Connection {
match Connection::connect(rabbitmq_url, lapin::ConnectionProperties::default()).await {
Ok(env) => env,
Err(error) => {
log::error!(
"Cannot connect to FlowQueue (RabbitMQ) instance! Reason: {:?}",
error
);
panic!("Cannot connect to FlowQueue (RabbitMQ) instance!");
}
}
}
#[cfg(test)]
mod tests {
use crate::flow_queue::connection::build_connection;
use testcontainers::GenericImage;
use testcontainers::core::{IntoContainerPort, WaitFor};
use testcontainers::runners::AsyncRunner;
macro_rules! rabbitmq_container_test {
($test_name:ident, $consumer:expr) => {
#[tokio::test]
async fn $test_name() {
let port: u16 = 5672;
let image_name = "rabbitmq";
let wait_message = "Server startup complete";
let container = GenericImage::new(image_name, "latest")
.with_exposed_port(port.tcp())
.with_wait_for(WaitFor::message_on_stdout(wait_message))
.start()
.await
.unwrap();
let host_port = container.get_host_port_ipv4(port).await.unwrap();
let url = format!("amqp://guest:guest@localhost:{}", host_port);
$consumer(url).await;
}
};
}
rabbitmq_container_test!(
test_rabbitmq_startup,
(|url: String| async move {
println!("RabbitMQ started with the url: {}", url);
})
);
rabbitmq_container_test!(
test_rabbitmq_connection,
(|url: String| async move {
build_connection(&*url).await;
})
);
}