-
Notifications
You must be signed in to change notification settings - Fork 20
[WIP] Be able to stop reconnection loop #35
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
Merged
craftytrickster
merged 6 commits into
craftytrickster:main
from
akuskis:introduce_shutdown_flag
Mar 8, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d0b0331
Introduce 'reconnection_stopped' flag
akuskis 0d4c455
Introduce CancellatioToken to interrupt reconnection loop
akuskis 2c1480a
Collapsing cancel token if statements and reusing test code
craftytrickster 5b14428
Add integration test
craftytrickster 5f83757
Merge pull request #1 from craftytrickster/simplify-pr
akuskis 207a874
Add missed 'must_use'
akuskis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| use futures::stream::StreamExt; | ||
| use std::io::{self, ErrorKind}; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::task::Poll; | ||
| use std::time::Duration; | ||
| use stubborn_io::ReconnectOptions; | ||
| use tokio_util::codec::{Framed, LinesCodec}; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| mod common; | ||
|
|
||
| use common::{DummyCtor, StubbornDummy}; | ||
|
|
||
| fn dummy_ctor(outcomes: Vec<bool>) -> DummyCtor { | ||
| DummyCtor { | ||
| connect_outcomes: Arc::new(Mutex::new(outcomes)), | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| fn options_with_cancel(token: CancellationToken, retries: usize) -> ReconnectOptions { | ||
| ReconnectOptions::new() | ||
| .with_exit_if_first_connect_fails(false) | ||
| .with_retries_generator(move || vec![Duration::from_millis(50); retries]) | ||
| .with_cancel_token(token) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn should_work_without_cancel_token() { | ||
| let ctor = dummy_ctor(vec![true]); | ||
| let options = ReconnectOptions::new(); | ||
|
|
||
| let result = StubbornDummy::connect_with_options(ctor, options).await; | ||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn should_fail_with_pre_cancelled_token() { | ||
| let token = CancellationToken::new(); | ||
| token.cancel(); | ||
|
|
||
| let ctor = dummy_ctor(vec![false, false]); | ||
| let options = options_with_cancel(token, 1); | ||
|
|
||
| let result = StubbornDummy::connect_with_options(ctor, options).await; | ||
| assert!(matches!(result, Err(e) if e.kind() == ErrorKind::Interrupted)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn should_cancel_initial_connection_attempts() { | ||
| let token = CancellationToken::new(); | ||
| let token_clone = token.clone(); | ||
|
|
||
| tokio::spawn(async move { | ||
| tokio::time::sleep(Duration::from_millis(120)).await; | ||
| token_clone.cancel(); | ||
| }); | ||
|
|
||
| let result = StubbornDummy::connect_with_options( | ||
| dummy_ctor(vec![false, false, false, false]), | ||
| options_with_cancel(token, 3), | ||
| ) | ||
| .await; | ||
|
|
||
| assert!(matches!(result, Err(e) if e.kind() == ErrorKind::Interrupted)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn should_cancel_reconnection_attempts() { | ||
| let token = CancellationToken::new(); | ||
| let token_clone = token.clone(); | ||
|
|
||
| let options = ReconnectOptions::new() | ||
| .with_retries_generator(|| vec![Duration::from_millis(50); 4]) | ||
| .with_cancel_token(token); | ||
|
|
||
| let mut ctor = dummy_ctor(vec![true, false, false, false, false]); | ||
| ctor.poll_read_results = Arc::new(Mutex::new(vec![( | ||
| Poll::Ready(Err(io::Error::new(ErrorKind::ConnectionAborted, "fatal"))), | ||
| vec![], | ||
| )])); | ||
|
|
||
| let dummy = StubbornDummy::connect_with_options(ctor, options) | ||
| .await | ||
| .unwrap(); | ||
| let mut framed = Framed::new(dummy, LinesCodec::new()); | ||
|
|
||
| tokio::spawn(async move { | ||
| tokio::time::sleep(Duration::from_millis(120)).await; | ||
| token_clone.cancel(); | ||
| }); | ||
|
|
||
| let result = framed.next().await; | ||
| assert!(result.unwrap().is_err()); | ||
| } |
This file contains hidden or 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,85 @@ | ||
| use std::future::Future; | ||
| use std::io; | ||
| use std::io::ErrorKind; | ||
| use std::pin::Pin; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::task::{Context, Poll}; | ||
| use stubborn_io::tokio::{StubbornIo, UnderlyingIo}; | ||
| use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
|
|
||
| pub type PollReadResults = Arc<Mutex<Vec<(Poll<io::Result<()>>, Vec<u8>)>>>; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct DummyIo { | ||
| pub poll_read_results: PollReadResults, | ||
| } | ||
|
|
||
| #[derive(Default, Clone)] | ||
| pub struct DummyCtor { | ||
| pub connect_outcomes: ConnectOutcomes, | ||
| pub poll_read_results: PollReadResults, | ||
| } | ||
|
|
||
| pub type ConnectOutcomes = Arc<Mutex<Vec<bool>>>; | ||
|
|
||
| impl UnderlyingIo<DummyCtor> for DummyIo { | ||
| fn establish(ctor: DummyCtor) -> Pin<Box<dyn Future<Output = io::Result<Self>> + Send>> { | ||
| let mut connect_attempt_outcome_results = ctor.connect_outcomes.lock().unwrap(); | ||
|
|
||
| let should_succeed = connect_attempt_outcome_results.remove(0); | ||
| if should_succeed { | ||
| let dummy_io = DummyIo { | ||
| poll_read_results: ctor.poll_read_results.clone(), | ||
| }; | ||
|
|
||
| Box::pin(async { Ok(dummy_io) }) | ||
| } else { | ||
| Box::pin(async { Err(io::Error::new(ErrorKind::NotConnected, "So unfortunate")) }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub type StubbornDummy = StubbornIo<DummyIo, DummyCtor>; | ||
|
|
||
| impl AsyncWrite for DummyIo { | ||
| fn poll_write( | ||
| self: Pin<&mut Self>, | ||
| _cx: &mut Context<'_>, | ||
| _buf: &[u8], | ||
| ) -> Poll<io::Result<usize>> { | ||
| unreachable!(); | ||
| } | ||
|
|
||
| fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
| unreachable!(); | ||
| } | ||
|
|
||
| fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
| Poll::Ready(Ok(())) | ||
| } | ||
| } | ||
|
|
||
| impl AsyncRead for DummyIo { | ||
| fn poll_read( | ||
| self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| buf: &mut ReadBuf<'_>, | ||
| ) -> Poll<io::Result<()>> { | ||
| let cloned = self.poll_read_results.clone(); | ||
| let mut poll_read_results = cloned.lock().unwrap(); | ||
|
|
||
| let (result, bytes) = poll_read_results.remove(0); | ||
|
|
||
| if let Poll::Ready(Err(e)) = result { | ||
| if e.kind() == io::ErrorKind::WouldBlock { | ||
| cx.waker().wake_by_ref(); | ||
| Poll::Pending | ||
| } else { | ||
| Poll::Ready(Err(e)) | ||
| } | ||
| } else { | ||
| buf.put_slice(&bytes); | ||
| result | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.