-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use lapin::{ | ||
message::DeliveryResult, options::*, publisher_confirm::Confirmation, types::FieldTable, | ||
BasicProperties, Connection, ConnectionProperties, | ||
}; | ||
use tracing::info; | ||
|
||
fn main() { | ||
if std::env::var("RUST_LOG").is_err() { | ||
std::env::set_var("RUST_LOG", "info"); | ||
} | ||
|
||
tracing_subscriber::fmt::init(); | ||
|
||
let addr = std::env::var("AMQP_ADDR").unwrap_or_else(|_| "amqp://127.0.0.1:5672/%2f".into()); | ||
|
||
async_global_executor::block_on(async { | ||
let conn = Connection::connect(&addr, ConnectionProperties::default()) | ||
.await | ||
.expect("connection error"); | ||
|
||
info!("CONNECTED"); | ||
|
||
{ | ||
let channel1 = conn.create_channel().await.expect("create_channel"); | ||
let channel2 = conn.create_channel().await.expect("create_channel"); | ||
channel1 | ||
.queue_declare( | ||
"recover-test", | ||
QueueDeclareOptions::default(), | ||
FieldTable::default(), | ||
) | ||
.await | ||
.expect("queue_declare"); | ||
|
||
info!("will consume"); | ||
let channel = channel2.clone(); | ||
channel2 | ||
.basic_consume( | ||
"recover-test", | ||
"my_consumer", | ||
BasicConsumeOptions::default(), | ||
FieldTable::default(), | ||
) | ||
.await | ||
.expect("basic_consume") | ||
.set_delegate(move |delivery: DeliveryResult| { | ||
let channel = channel.clone(); | ||
async move { | ||
info!(message=?delivery, "received message"); | ||
if let Ok(Some(delivery)) = delivery { | ||
delivery | ||
.ack(BasicAckOptions::default()) | ||
.await | ||
.expect("basic_ack"); | ||
if &delivery.data[..] == b"after" { | ||
channel | ||
.basic_cancel("my_consumer", BasicCancelOptions::default()) | ||
.await | ||
.expect("basic_cancel"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
info!("will publish"); | ||
let confirm = channel1 | ||
.basic_publish( | ||
"", | ||
"recover-test", | ||
BasicPublishOptions::default(), | ||
b"before", | ||
BasicProperties::default(), | ||
) | ||
.await | ||
.expect("basic_publish") | ||
.await | ||
.expect("publisher-confirms"); | ||
assert_eq!(confirm, Confirmation::NotRequested); | ||
|
||
info!("before fail"); | ||
assert!(channel1 | ||
.queue_declare( | ||
"fake queue", | ||
QueueDeclareOptions { | ||
passive: true, | ||
..QueueDeclareOptions::default() | ||
}, | ||
FieldTable::default(), | ||
) | ||
.await | ||
.is_err()); | ||
info!("after fail"); | ||
|
||
std::thread::sleep_ms(100); | ||
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, stable)
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, stable)
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, nightly)
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, nightly)
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, 1.74.0)
Check warning on line 94 in examples/t.rs GitHub Actions / build_and_test (ubuntu-latest, beta)
|
||
|
||
info!("publish after"); | ||
let confirm = channel1 | ||
.basic_publish( | ||
"", | ||
"recover-test", | ||
BasicPublishOptions::default(), | ||
b"after", | ||
BasicProperties::default(), | ||
) | ||
.await | ||
.expect("basic_publish") | ||
.await | ||
.expect("publisher-confirms"); | ||
assert_eq!(confirm, Confirmation::NotRequested); | ||
} | ||
|
||
conn.run().expect("conn.run"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters