Skip to content

Commit

Permalink
Tidy dummy event setting code in announcer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed May 12, 2024
1 parent 30532c5 commit 0cf7318
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 87 deletions.
16 changes: 14 additions & 2 deletions client/src/announcer/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ use serde_json::Value;
use std::collections::HashMap;
use tokio::time::{Duration, Instant};

fn fixup_events_for_test_comparison(events: &mut [Event]) {
for event in events {
fn set_and_patch_dummy_events(
server: &mut DummyScheduleServer,
mut events: Vec<Event>,
) -> Vec<Event> {
// Load the events into the dummy/test server.
server.set_events(events.clone());

// Add "type" to extra fields to ensure equality checking works as expected.
// In theory this should not be needed, I assume there is some funkyness going on with the fact
// this field is renamed by serde.
for event in events.iter_mut() {
event.extra = HashMap::from([("type".to_string(), Value::String("talk".to_string()))]);
}

// Return the patched events for comparisons.
events
}
12 changes: 8 additions & 4 deletions client/src/announcer/test/t02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ async fn t02_schedule_is_refreshed_on_requested_schedule() {
let mut dummy_server = DummyScheduleServer::new(8002).await;

let now = Utc::now();
dummy_server.set_events(vec![Event::dummy(
0,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)]);

set_and_patch_dummy_events(
&mut dummy_server,
vec![Event::dummy(
0,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)],
);

let client = Client::new(dummy_server.url());

Expand Down
23 changes: 15 additions & 8 deletions client/src/announcer/test/t03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ async fn t03_changes_to_the_schedule_are_noticed() {
let mut dummy_server = DummyScheduleServer::new(8003).await;

let now = Utc::now();
dummy_server.set_events(vec![Event::dummy(
0,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)]);

set_and_patch_dummy_events(
&mut dummy_server,
vec![Event::dummy(
0,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)],
);

let client = Client::new(dummy_server.url());

Expand All @@ -23,10 +27,13 @@ async fn t03_changes_to_the_schedule_are_noticed() {
.await
.unwrap();

dummy_server.set_events(vec![Event::dummy(
1,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)]);
set_and_patch_dummy_events(
&mut dummy_server,
vec![Event::dummy(
1,
(now + ChronoDuration::try_minutes(1).unwrap()).into(),
)],
);

crate::assert_future_in!(
announcer.poll(),
Expand Down
16 changes: 8 additions & 8 deletions client/src/announcer/test/t04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ async fn t04_basic_event_notification() {
let mut dummy_server = DummyScheduleServer::new(8004).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand Down
18 changes: 9 additions & 9 deletions client/src/announcer/test/t05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ async fn t05_event_notification_with_multiple_identical_start_times() {
let mut dummy_server = DummyScheduleServer::new(8005).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(3, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(3, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand Down
24 changes: 10 additions & 14 deletions client/src/announcer/test/t06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ async fn t06_basic_event_notification_unsorted() {
let mut dummy_server = DummyScheduleServer::new(8006).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
];

dummy_server.set_events(vec![
events[1].clone(),
events[0].clone(),
events[2].clone(),
]);

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(1, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand All @@ -36,13 +32,13 @@ async fn t06_basic_event_notification_unsorted() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(events[1].clone())
);

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(2),
AnnouncerPollResult::Event(events[1].clone())
AnnouncerPollResult::Event(events[0].clone())
);

crate::assert_future_in!(
Expand Down
16 changes: 8 additions & 8 deletions client/src/announcer/test/t07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ async fn t07_event_notification_with_schedule_update() {
let mut dummy_server = DummyScheduleServer::new(8007).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand Down
33 changes: 16 additions & 17 deletions client/src/announcer/test/t08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ async fn t08_future_changes_take_effect() {
let mut dummy_server = DummyScheduleServer::new(8008).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand Down Expand Up @@ -53,15 +53,14 @@ async fn t08_future_changes_take_effect() {
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(9).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(9).unwrap()).into()),
],
);

crate::assert_future_in!(
announcer.poll(),
Expand Down
33 changes: 16 additions & 17 deletions client/src/announcer/test/t09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ async fn t09_changes_in_past_have_no_effect() {
let mut dummy_server = DummyScheduleServer::new(8009).await;

let now = Utc::now();
let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(1).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
],
);

let client = Client::new(dummy_server.url());

Expand Down Expand Up @@ -53,15 +53,14 @@ async fn t09_changes_in_past_have_no_effect() {
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

let mut events = vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
];

dummy_server.set_events(events.clone());

fixup_events_for_test_comparison(&mut events);
let events = set_and_patch_dummy_events(
&mut dummy_server,
vec![
Event::dummy(0, (now + ChronoDuration::try_seconds(2).unwrap()).into()),
Event::dummy(1, (now + ChronoDuration::try_seconds(3).unwrap()).into()),
Event::dummy(2, (now + ChronoDuration::try_seconds(7).unwrap()).into()),
],
);

crate::assert_future_in!(
announcer.poll(),
Expand Down

0 comments on commit 0cf7318

Please sign in to comment.