Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle settled transfers #45

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ jobs:
- name: Upload to Codecov
if: matrix.version == '1.75.0' && (github.ref == 'refs/heads/master' || github.event_name == 'pull_request')
continue-on-error: true
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v4
with:
file: cobertura.xml
token: ${{ secrets.CODECOV_TOKEN }}
files: cobertura.xml
fail_ci_if_error: true

- name: Install cargo-cache
continue-on-error: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ guide/build/
*.pid
*.sock
*~
.DS_Store

# These are backup files generated by rustfmt
**/*.rs.bk
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [2.1.3] - 2024-04-11

* Handle settled transfers

## [2.1.2] - 2024-03-17

* Set transfer handle
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories = ["network-programming"]
keywords = ["AMQP", "IoT", "messaging"]
license = "MIT OR Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config"]
edition = "2018"
edition = "2021"

[workspace]
members = [
Expand All @@ -24,7 +24,7 @@ default = []
frame-trace = []

[dependencies]
ntex = "1.0"
ntex = "1"
ntex-util = "1.0.1"
ntex-amqp-codec = "0.9"

Expand All @@ -37,7 +37,7 @@ uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
env_logger = "0.11"
ntex = { version = "1.0", features = ["tokio"] }
ntex = { version = "1", features = ["tokio"] }

[patch.crates-io]
ntex-amqp = { path = "." }
Expand Down
10 changes: 9 additions & 1 deletion src/delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl Delivery {
}

pub async fn wait(&self) -> Result<Option<DeliveryState>, AmqpProtocolError> {
if self.flags.get().contains(Flags::LOCAL_SETTLED) {
return Ok(None);
}

let rx = if let Some(inner) = self
.session
.inner
Expand Down Expand Up @@ -300,7 +304,11 @@ impl DeliveryBuilder {
{
Err(AmqpProtocolError::BodyTooLarge)
} else {
let id = self.sender.get_mut().send(self.data, self.tag).await?;
let id = self
.sender
.get_mut()
.send(self.data, self.tag, self.settled)
.await?;

Ok(Delivery {
id,
Expand Down
10 changes: 6 additions & 4 deletions src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::VecDeque, convert::TryFrom, fmt, future::Future};

Check warning on line 1 in src/session.rs

View workflow job for this annotation

GitHub Actions / clippy

the item `TryFrom` is imported redundantly

warning: the item `TryFrom` is imported redundantly --> src/session.rs:1:34 | 1 | use std::{collections::VecDeque, convert::TryFrom, fmt, future::Future}; | ^^^^^^^^^^^^^^^^ --> /rustc/8b2459c1f21187f9792d99310171a15e64feb9cf/library/std/src/prelude/mod.rs:148:13 | = note: the item `TryFrom` is already defined here

use ntex::channel::{condition, oneshot, pool};
use ntex::util::{ByteString, Bytes, Either, HashMap, PoolRef, Ready};
Expand Down Expand Up @@ -1147,7 +1147,7 @@
} else {
None
},
incoming_window: std::u32::MAX,

Check warning on line 1150 in src/session.rs

View workflow job for this annotation

GitHub Actions / clippy

usage of a legacy numeric constant

warning: usage of a legacy numeric constant --> src/session.rs:1150:34 | 1150 | incoming_window: std::u32::MAX, | ^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants help: use the associated constant instead | 1150 | incoming_window: u32::MAX, | ~~~~~~~~
next_outgoing_id: self.next_outgoing_id,
outgoing_window: self.remote_incoming_window,
handle: None,
Expand All @@ -1174,7 +1174,7 @@
} else {
None
},
incoming_window: std::u32::MAX,

Check warning on line 1177 in src/session.rs

View workflow job for this annotation

GitHub Actions / clippy

usage of a legacy numeric constant

warning: usage of a legacy numeric constant --> src/session.rs:1177:30 | 1177 | incoming_window: std::u32::MAX, | ^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants help: use the associated constant instead | 1177 | incoming_window: u32::MAX, | ~~~~~~~~
next_outgoing_id: self.next_outgoing_id,
outgoing_window: self.remote_incoming_window,
handle: Some(handle),
Expand Down Expand Up @@ -1253,14 +1253,15 @@
transfer.0.handle = link_handle;
transfer.0.body = Some(TransferBody::Data(chunk));
transfer.0.more = true;
transfer.0.settled = Some(settled);
transfer.0.state = tr_settled;
transfer.0.batchable = true;
transfer.0.delivery_id = Some(delivery_id);
transfer.0.delivery_tag = Some(tag.clone());
transfer.0.message_format = message_format;

if !settled {
if settled {
transfer.0.settled = Some(true);

Check warning on line 1263 in src/session.rs

View check run for this annotation

Codecov / codecov/patch

src/session.rs#L1262-L1263

Added lines #L1262 - L1263 were not covered by tests
} else {
self.unsettled_snd_deliveries
.insert(delivery_id, DeliveryInner::new());
}
Expand Down Expand Up @@ -1302,13 +1303,14 @@
let mut transfer = Transfer(Default::default());
transfer.0.handle = link_handle;
transfer.0.body = Some(body);
transfer.0.settled = Some(settled);
transfer.0.state = tr_settled;
transfer.0.delivery_id = Some(delivery_id);
transfer.0.delivery_tag = Some(tag);
transfer.0.message_format = message_format;

if !settled {
if settled {
transfer.0.settled = Some(true);
} else {
self.unsettled_snd_deliveries
.insert(delivery_id, DeliveryInner::new());
}
Expand Down
3 changes: 2 additions & 1 deletion src/sndlink.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::VecDeque, convert::TryFrom, future::Future};

Check warning on line 1 in src/sndlink.rs

View workflow job for this annotation

GitHub Actions / clippy

the item `TryFrom` is imported redundantly

warning: the item `TryFrom` is imported redundantly --> src/sndlink.rs:1:34 | 1 | use std::{collections::VecDeque, convert::TryFrom, future::Future}; | ^^^^^^^^^^^^^^^^ --> /rustc/8b2459c1f21187f9792d99310171a15e64feb9cf/library/std/src/prelude/mod.rs:148:13 | = note: the item `TryFrom` is already defined here

use ntex::channel::{condition, oneshot, pool};
use ntex::util::{BufMut, ByteString, Bytes, Either, PoolRef, Ready};
Expand Down Expand Up @@ -320,6 +320,7 @@
&mut self,
body: T,
tag: Option<Bytes>,
settled: bool,
) -> Result<DeliveryNumber, AmqpProtocolError> {
if let Some(ref err) = self.error {
Err(err.clone())
Expand Down Expand Up @@ -353,7 +354,7 @@
self.session
.inner
.get_mut()
.send_transfer(self.id as u32, tag, body, false, self.max_message_size)
.send_transfer(self.id as u32, tag, body, settled, self.max_message_size)
.await
}
}
Expand Down
44 changes: 33 additions & 11 deletions tests/test_server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{convert::TryFrom, sync::Arc, sync::Mutex};
use std::convert::TryFrom;
use std::sync::{atomic::AtomicUsize, atomic::Ordering, Arc, Mutex};

use ntex::server::test_server;
use ntex::service::{boxed, boxed::BoxService, fn_factory_with_config, fn_service};
Expand All @@ -9,17 +10,30 @@ use ntex_amqp::{
};

async fn server(
link: types::Link<()>,
_link: types::Link<()>,
) -> Result<BoxService<types::Transfer, types::Outcome, LinkError>, LinkError> {
println!("OPEN LINK: {:?}", link);
Ok(boxed::service(fn_service(|_req| {
Ready::Ok(types::Outcome::Accept)
})))
}

async fn server_count(
count: Arc<AtomicUsize>,
) -> Result<BoxService<types::Transfer, types::Outcome, LinkError>, LinkError> {
Ok(boxed::service(fn_service(move |_req| {
let val = count.load(Ordering::Relaxed);
count.store(val + 1, Ordering::Release);
Ready::Ok(types::Outcome::Accept)
})))
}

#[ntex::test]
async fn test_simple() -> std::io::Result<()> {
let srv = test_server(|| {
let count = Arc::new(AtomicUsize::new(0));

let count2 = count.clone();
let srv = test_server(move || {
let count = count2.clone();
server::Server::build(|con: server::Handshake| async move {
match con {
server::Handshake::Amqp(con) => {
Expand All @@ -31,7 +45,10 @@ async fn test_simple() -> std::io::Result<()> {
})
.finish(
server::Router::<()>::new()
.service("test", fn_factory_with_config(server))
.service(
"test",
fn_factory_with_config(move |_: types::Link<()>| server_count(count.clone())),
)
.finish(),
)
});
Expand Down Expand Up @@ -60,6 +77,17 @@ async fn test_simple() -> std::io::Result<()> {
let st = delivery.wait().await.unwrap().unwrap();
assert_eq!(st, protocol::DeliveryState::Accepted(protocol::Accepted {}));

let delivery = link
.delivery(Bytes::from(b"test".as_ref()))
.settled()
.send()
.await
.unwrap();
let st = delivery.wait().await.unwrap();
assert_eq!(st, None);
sleep(Millis(250)).await;

assert_eq!(count.load(Ordering::Relaxed), 2);
Ok(())
}

Expand Down Expand Up @@ -194,8 +222,6 @@ async fn test_session_end() -> std::io::Result<()> {

#[ntex::test]
async fn test_link_detach() -> std::io::Result<()> {
let _ = env_logger::try_init();

let srv = test_server(move || {
server::Server::build(|con: server::Handshake| async move {
match con {
Expand Down Expand Up @@ -267,8 +293,6 @@ async fn test_link_detach() -> std::io::Result<()> {

#[ntex::test]
async fn test_link_detach_on_session_end() -> std::io::Result<()> {
let _ = env_logger::try_init();

let srv = test_server(move || {
server::Server::build(|con: server::Handshake| async move {
match con {
Expand Down Expand Up @@ -322,8 +346,6 @@ async fn test_link_detach_on_session_end() -> std::io::Result<()> {

#[ntex::test]
async fn test_link_detach_on_disconnect() -> std::io::Result<()> {
let _ = env_logger::try_init();

let srv = test_server(move || {
server::Server::build(|con: server::Handshake| async move {
match con {
Expand Down
Loading