Skip to content
Merged
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
57 changes: 39 additions & 18 deletions tests/connection_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,46 @@ enum Priority {
Low,
}

/// Push frames in the given priority order and return the expected output
/// sequence when fairness is disabled.
async fn queue_frames(
order: &[Priority],
handle: &wireframe::push::PushHandle<u8>,
high_count: usize,
) -> Vec<u8> {
let mut next_high = 1u8;
let mut next_low = u8::try_from(high_count).expect("too many high frames") + 1;

let mut highs = Vec::new();
let mut lows = Vec::new();
Comment on lines +91 to +100
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider adding tests for empty input and mixed single-element cases.

Adding tests for empty input and single-element cases will help verify the scheduler's behavior in these edge scenarios.

Suggested implementation:

async fn queue_frames(
    order: &[Priority],
    handle: &wireframe::push::PushHandle<u8>,
    high_count: usize,
) -> Vec<u8> {
    let mut next_high = 1u8;
    let mut next_low = u8::try_from(high_count).expect("too many high frames") + 1;

    let mut highs = Vec::new();
    let mut lows = Vec::new();

}

// Test for empty input
#[tokio::test]
async fn test_queue_frames_empty_input() {
    let (handle, mut output) = wireframe::push::PushHandle::new();
    let result = queue_frames(&[], &handle, 0).await;
    assert!(result.is_empty(), "Expected empty output for empty input");
}

// Test for single high-priority frame
#[tokio::test]
async fn test_queue_frames_single_high() {
    let (handle, mut output) = wireframe::push::PushHandle::new();
    let result = queue_frames(&[Priority::High], &handle, 1).await;
    assert_eq!(result, vec![1u8], "Expected output to contain only the high-priority frame");
}

// Test for single low-priority frame
#[tokio::test]
async fn test_queue_frames_single_low() {
    let (handle, mut output) = wireframe::push::PushHandle::new();
    let result = queue_frames(&[Priority::Low], &handle, 0).await;
    assert_eq!(result, vec![1u8], "Expected output to contain only the low-priority frame");
}

// Test for mixed single-element case (one high, one low)
#[tokio::test]
async fn test_queue_frames_mixed_single_elements() {
    let (handle, mut output) = wireframe::push::PushHandle::new();
    let result = queue_frames(&[Priority::High, Priority::Low], &handle, 1).await;
    assert_eq!(result, vec![1u8, 2u8], "Expected output to contain high then low frame");
}
  • Ensure that wireframe::push::PushHandle::new() is available and returns a handle suitable for testing.
  • If the queue_frames function or the test module uses a different async runtime, adjust the test attribute accordingly.
  • If the Priority enum is not in scope, import it at the top of the file.


for priority in order {
match priority {
Priority::High => {
let msg = format!("failed to push high-priority frame {next_high}");
handle.push_high_priority(next_high).await.expect(&msg);
highs.push(next_high);
next_high += 1;
}
Priority::Low => {
let msg = format!("failed to push low-priority frame {next_low}");
handle.push_low_priority(next_low).await.expect(&msg);
lows.push(next_low);
next_low += 1;
}
}
}

highs.into_iter().chain(lows.into_iter()).collect()
}

#[rstest]
#[case(vec![Priority::High, Priority::High, Priority::High, Priority::Low, Priority::Low])]
#[case(vec![Priority::Low, Priority::Low, Priority::High, Priority::High, Priority::High])]
#[case(vec![Priority::High; 3])]
#[case(vec![Priority::Low; 3])]
#[tokio::test]
async fn fairness_disabled_processes_all_high_first(
async fn processes_all_priorities_in_order(
#[case] order: Vec<Priority>,
queues: (PushQueues<u8>, wireframe::push::PushHandle<u8>),
shutdown_token: CancellationToken,
Expand All @@ -101,29 +136,15 @@ async fn fairness_disabled_processes_all_high_first(
time_slice: None,
};

let mut highs = 1..=3;
let mut lows = 4..=5;
for priority in order {
match priority {
Priority::High => {
let n = highs.next().expect("ran out of high-priority frames");
let msg = format!("failed to push high-priority frame {n}");
handle.push_high_priority(n).await.expect(&msg);
}
Priority::Low => {
let n = lows.next().expect("ran out of low-priority frames");
let msg = format!("failed to push low-priority frame {n}");
handle.push_low_priority(n).await.expect(&msg);
}
}
}
let high_count = order.iter().filter(|p| matches!(p, Priority::High)).count();
let expected = queue_frames(&order, &handle, high_count).await;

let mut actor: ConnectionActor<_, ()> =
ConnectionActor::new(queues, handle, None, shutdown_token);
actor.set_fairness(fairness);
let mut out = Vec::new();
actor.run(&mut out).await.expect("actor run failed");
assert_eq!(out, vec![1, 2, 3, 4, 5]);
assert_eq!(out, expected);
}

#[rstest]
Expand Down