-
Notifications
You must be signed in to change notification settings - Fork 0
Add loom-based concurrency tests for push queues #360
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
leynos
merged 5 commits into
main
from
codex/implement-loom-concurrency-testing-with-unit-tests
Sep 17, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2cae82e
Add loom-based concurrency tests for push queues
leynos c972378
Broaden loom coverage for push queues
leynos 20b9d57
Document loom probe usage
leynos 5dedb91
Annotate loom command block as plaintext
leynos 4bce3c9
Rephrase loom probe documentation
leynos 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
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 |
|---|---|---|
| @@ -1,76 +1,171 @@ | ||
| #![cfg(feature = "advanced-tests")] | ||
| //! Concurrency tests for push delivery using loom. | ||
| #![cfg(all(feature = "advanced-tests", loom))] | ||
| //! Concurrency tests for push queues using loom. | ||
| //! | ||
| //! These tests model concurrent push execution to validate fairness and | ||
| //! correct shutdown behaviour under various interleavings. | ||
| //! These tests exercise the `PushHandle` shared state without Tokio. `loom` | ||
| //! explores interleavings to ensure DLQ accounting and queue-full errors remain | ||
| //! deterministic under concurrent producers. | ||
|
|
||
| use loom::model; | ||
| use tokio::runtime::Builder; | ||
| use tokio_util::sync::CancellationToken; | ||
| use wireframe::{ | ||
| connection::ConnectionActor, | ||
| push::PushQueues, | ||
| }; | ||
| use loom::{model, thread}; | ||
| use rstest::rstest; | ||
| use tokio::sync::mpsc; | ||
| use wireframe::push::{PushError, PushPolicy, PushPriority, PushQueues}; | ||
|
|
||
| #[rstest] | ||
| #[case(PushPriority::High)] | ||
| #[case(PushPriority::Low)] | ||
| fn concurrent_drops_reset_dlq_counter(#[case] priority: PushPriority) { | ||
| model(move || { | ||
| let (dlq_tx, mut dlq_rx) = mpsc::channel(4); | ||
| let (queues, handle) = PushQueues::<u8>::builder() | ||
| .high_capacity(1) | ||
| .low_capacity(1) | ||
| .dlq(Some(dlq_tx)) | ||
| .dlq_log_every_n(2) | ||
| .unlimited() | ||
| .build() | ||
| .expect("failed to build PushQueues"); | ||
| let _queues = queues; | ||
|
|
||
| handle | ||
| .try_push(0, priority, PushPolicy::ReturnErrorIfFull) | ||
| .expect("initial push should succeed"); | ||
|
|
||
| let probe = handle.probe(); | ||
| let h1 = handle.clone(); | ||
| let h2 = handle.clone(); | ||
|
|
||
| let t1_priority = priority; | ||
| let t2_priority = priority; | ||
|
|
||
| let t1 = thread::spawn(move || { | ||
| h1.try_push(1, t1_priority, PushPolicy::WarnAndDropIfFull) | ||
| .expect("first drop should succeed"); | ||
| }); | ||
| let t2 = thread::spawn(move || { | ||
| h2.try_push(2, t2_priority, PushPolicy::WarnAndDropIfFull) | ||
| .expect("second drop should succeed"); | ||
| }); | ||
|
|
||
| t1.join().expect("first drop thread panicked"); | ||
|
leynos marked this conversation as resolved.
|
||
| t2.join().expect("second drop thread panicked"); | ||
|
|
||
| assert_eq!( | ||
| probe.dlq_drop_count(), | ||
| 0, | ||
| "counter should reset after reaching the logging threshold" | ||
| ); | ||
|
|
||
| let mut drops = Vec::new(); | ||
| while let Ok(frame) = dlq_rx.try_recv() { | ||
| drops.push(frame); | ||
| } | ||
| drops.sort_unstable(); | ||
| assert_eq!(drops, vec![1, 2]); | ||
|
leynos marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn concurrent_push_delivery() { | ||
| fn concurrent_queue_full_errors_are_reported() { | ||
| model(|| { | ||
| let rt = Builder::new_current_thread() | ||
| .enable_all() | ||
| let (queues, handle) = PushQueues::<u8>::builder() | ||
| .high_capacity(1) | ||
| .low_capacity(1) | ||
| .unlimited() | ||
| .build() | ||
| .expect("failed to build tokio runtime"); | ||
|
|
||
| rt.block_on(async { | ||
| let (queues, handle) = PushQueues::<u8>::builder() | ||
| .high_capacity(1) | ||
| .low_capacity(1) | ||
| .unlimited() | ||
| .build() | ||
| .expect("failed to build PushQueues"); | ||
| let token = CancellationToken::new(); | ||
|
|
||
| let out = loom::sync::Arc::new(loom::sync::Mutex::new(Vec::new())); | ||
| let out_clone = out.clone(); | ||
| let mut actor: ConnectionActor<_, ()> = | ||
| ConnectionActor::new(queues, handle.clone(), None, token.clone()); | ||
|
|
||
| let actor_task = tokio::spawn(async move { | ||
| let mut buf = Vec::new(); | ||
| actor | ||
| .run(&mut buf) | ||
| .await | ||
| .expect("connection actor failed to run"); | ||
| out_clone | ||
| .lock() | ||
| .expect("mutex poisoned") | ||
| .extend(buf); | ||
| }); | ||
|
|
||
| let h1 = handle.clone(); | ||
| let t1 = tokio::spawn(async move { | ||
| h1 | ||
| .push_high_priority(1u8) | ||
| .await | ||
| .expect("failed to push high priority frame"); | ||
| }); | ||
|
|
||
| let h2 = handle.clone(); | ||
| let t2 = tokio::spawn(async move { | ||
| h2 | ||
| .push_low_priority(2u8) | ||
| .await | ||
| .expect("failed to push low priority frame"); | ||
| }); | ||
|
|
||
| t1.await.expect("high priority task join failed"); | ||
| t2.await.expect("low priority task join failed"); | ||
| token.cancel(); | ||
| actor_task.await.expect("actor task join failed"); | ||
|
|
||
| let buf = out.lock().expect("mutex poisoned"); | ||
| assert!(buf.contains(&1)); | ||
| assert!(buf.contains(&2)); | ||
| .expect("failed to build PushQueues"); | ||
| let _queues = queues; | ||
|
|
||
| handle | ||
| .try_push(0, PushPriority::High, PushPolicy::ReturnErrorIfFull) | ||
| .expect("initial push should succeed"); | ||
|
|
||
| let h1 = handle.clone(); | ||
| let h2 = handle.clone(); | ||
| let h3 = handle.clone(); | ||
|
|
||
| let t1 = thread::spawn(move || { | ||
| let res = h1.try_push(1, PushPriority::High, PushPolicy::ReturnErrorIfFull); | ||
| assert!( | ||
| matches!(res, Err(PushError::QueueFull)), | ||
| "expected queue full error for first producer" | ||
| ); | ||
| }); | ||
| let t2 = thread::spawn(move || { | ||
| let res = h2.try_push(2, PushPriority::High, PushPolicy::ReturnErrorIfFull); | ||
| assert!( | ||
| matches!(res, Err(PushError::QueueFull)), | ||
| "expected queue full error for second producer" | ||
| ); | ||
| }); | ||
| let t3 = thread::spawn(move || { | ||
| let res = h3.try_push(3, PushPriority::High, PushPolicy::ReturnErrorIfFull); | ||
| assert!( | ||
| matches!(res, Err(PushError::QueueFull)), | ||
| "expected queue full error for third producer" | ||
| ); | ||
| }); | ||
|
|
||
| t1.join().expect("first producer thread panicked"); | ||
| t2.join().expect("second producer thread panicked"); | ||
| t3.join().expect("third producer thread panicked"); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dlq_probe_ignores_absent_channel() { | ||
| model(|| { | ||
| let (queues, handle) = PushQueues::<u8>::builder() | ||
| .high_capacity(1) | ||
| .low_capacity(1) | ||
| .dlq(None) | ||
| .dlq_log_every_n(2) | ||
| .unlimited() | ||
| .build() | ||
| .expect("failed to build PushQueues"); | ||
| let _queues = queues; | ||
|
|
||
| handle | ||
| .try_push(0, PushPriority::High, PushPolicy::ReturnErrorIfFull) | ||
| .expect("initial push should succeed"); | ||
|
|
||
| let probe = handle.probe(); | ||
|
|
||
| handle | ||
| .try_push(1, PushPriority::High, PushPolicy::WarnAndDropIfFull) | ||
| .expect("drop should succeed even without a DLQ"); | ||
|
|
||
| assert_eq!( | ||
| probe.dlq_drop_count(), | ||
| 0, | ||
| "counter remains zero when DLQ is disabled" | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dlq_probe_reports_zero_when_dlq_idle() { | ||
| model(|| { | ||
| let (dlq_tx, mut dlq_rx) = mpsc::channel(2); | ||
| let (queues, handle) = PushQueues::<u8>::builder() | ||
| .high_capacity(1) | ||
| .low_capacity(1) | ||
| .dlq(Some(dlq_tx)) | ||
| .dlq_log_every_n(2) | ||
| .unlimited() | ||
| .build() | ||
| .expect("failed to build PushQueues"); | ||
| let _queues = queues; | ||
|
|
||
| let probe = handle.probe(); | ||
|
|
||
| assert_eq!( | ||
| probe.dlq_drop_count(), | ||
| 0, | ||
| "counter remains zero before any drops" | ||
| ); | ||
| assert!( | ||
| dlq_rx.try_recv().is_err(), | ||
| "DLQ should be empty before any drops" | ||
| ); | ||
| }); | ||
| } | ||
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.