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

Fix Service::stop #1160

Merged
merged 2 commits into from
Nov 13, 2023
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
17 changes: 11 additions & 6 deletions async-nats/src/service/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ impl Stream for Endpoint {
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
trace!("polling for next request");
match self.shutdown_future.as_mut() {
Some(shutdown) => match shutdown.as_mut().poll(cx) {
if let Some(mut receiver) = self.shutdown.take() {
// Need to initialize `shutdown_future` on first poll
self.shutdown_future = Some(Box::pin(async move { receiver.recv().await }));
}

if let Some(shutdown) = self.shutdown_future.as_mut() {
match shutdown.as_mut().poll(cx) {
Poll::Ready(_result) => {
debug!("got stop broadcast");
self.requests
Expand All @@ -54,16 +59,16 @@ impl Stream for Endpoint {
max: None,
})
.ok();

// Clear future, can't be resumed after completion
self.shutdown_future = None;
}
Poll::Pending => {
trace!("stop broadcast still pending");
}
},
None => {
let mut receiver = self.shutdown.take().unwrap();
self.shutdown_future = Some(Box::pin(async move { receiver.recv().await }));
}
}

trace!("checking for new messages");
match self.requests.poll_next_unpin(cx) {
Poll::Ready(message) => {
Expand Down
18 changes: 18 additions & 0 deletions async-nats/tests/service_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,24 @@ mod service {
}
}

#[tokio::test]
async fn stop() {
let server = nats_server::run_basic_server();
let client = async_nats::connect(server.client_url()).await.unwrap();

let service = client
.service_builder()
.start("service", "1.0.0")
.await
.unwrap();

let mut endpoint = service.endpoint("products").await.unwrap();

service.stop().await.unwrap();
client.publish("products", "data".into()).await.unwrap();
assert!(endpoint.next().await.is_none());
}

#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn cross_clients_tests() {
Expand Down