Skip to content

Commit

Permalink
Tidy announcer test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed May 12, 2024
1 parent d65f62f commit 93bd5cf
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 113 deletions.
8 changes: 0 additions & 8 deletions client/src/announcer/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,3 @@ mod t09;
use super::*;
use crate::testing::DummyScheduleServer;
use tokio::time::{Duration, Instant};

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

// Return the patched events for comparisons.
events
}
11 changes: 4 additions & 7 deletions client/src/announcer/test/t02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ async fn t02_schedule_is_refreshed_on_requested_schedule() {

let now = Utc::now();

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

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

Expand Down
22 changes: 8 additions & 14 deletions client/src/announcer/test/t03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ async fn t03_changes_to_the_schedule_are_noticed() {

let now = Utc::now();

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

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

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

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

crate::assert_future_in!(
announcer.poll(),
Expand Down
19 changes: 8 additions & 11 deletions client/src/announcer/test/t04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ async fn t04_basic_event_notification() {

let now = Utc::now();

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()),
],
);
dummy_server.set_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()),
]);

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

Expand All @@ -32,19 +29,19 @@ async fn t04_basic_event_notification() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(dummy_server.event(0))
);

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

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

dummy_server.stop().await;
Expand Down
23 changes: 10 additions & 13 deletions client/src/announcer/test/t05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ async fn t05_event_notification_with_multiple_identical_start_times() {

let now = Utc::now();

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()),
],
);
dummy_server.set_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()),
]);

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

Expand All @@ -33,25 +30,25 @@ async fn t05_event_notification_with_multiple_identical_start_times() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(dummy_server.event(0))
);

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

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

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::Event(events[3].clone())
AnnouncerPollResult::Event(dummy_server.event(3))
);

dummy_server.stop().await;
Expand Down
19 changes: 8 additions & 11 deletions client/src/announcer/test/t06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ async fn t06_basic_event_notification_unsorted() {

let now = Utc::now();

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()),
],
);
dummy_server.set_events(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 @@ -32,19 +29,19 @@ async fn t06_basic_event_notification_unsorted() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[1].clone())
AnnouncerPollResult::Event(dummy_server.event(1))
);

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

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

dummy_server.stop().await;
Expand Down
19 changes: 8 additions & 11 deletions client/src/announcer/test/t07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ async fn t07_event_notification_with_schedule_update() {

let now = Utc::now();

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()),
],
);
dummy_server.set_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()),
]);

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

Expand All @@ -32,7 +29,7 @@ async fn t07_event_notification_with_schedule_update() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(dummy_server.event(0))
);

crate::assert_future_in!(
Expand All @@ -44,7 +41,7 @@ async fn t07_event_notification_with_schedule_update() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::Event(events[1].clone())
AnnouncerPollResult::Event(dummy_server.event(1))
);

crate::assert_future_in!(
Expand All @@ -62,7 +59,7 @@ async fn t07_event_notification_with_schedule_update() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(7),
AnnouncerPollResult::Event(events[2].clone())
AnnouncerPollResult::Event(dummy_server.event(2))
);

crate::assert_future_in!(
Expand Down
32 changes: 13 additions & 19 deletions client/src/announcer/test/t08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ async fn t08_future_changes_take_effect() {

let now = Utc::now();

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()),
],
);
dummy_server.set_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()),
]);

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

Expand All @@ -32,7 +29,7 @@ async fn t08_future_changes_take_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(dummy_server.event(0))
);

crate::assert_future_in!(
Expand All @@ -44,7 +41,7 @@ async fn t08_future_changes_take_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::Event(events[1].clone())
AnnouncerPollResult::Event(dummy_server.event(1))
);

crate::assert_future_in!(
Expand All @@ -53,14 +50,11 @@ async fn t08_future_changes_take_effect() {
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

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()),
],
);
dummy_server.set_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()),
]);

crate::assert_future_in!(
announcer.poll(),
Expand All @@ -77,7 +71,7 @@ async fn t08_future_changes_take_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(9),
AnnouncerPollResult::Event(events[2].clone())
AnnouncerPollResult::Event(dummy_server.event(2))
);

crate::assert_future_in!(
Expand Down
32 changes: 13 additions & 19 deletions client/src/announcer/test/t09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ async fn t09_changes_in_past_have_no_effect() {

let now = Utc::now();

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()),
],
);
dummy_server.set_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()),
]);

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

Expand All @@ -32,7 +29,7 @@ async fn t09_changes_in_past_have_no_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(1),
AnnouncerPollResult::Event(events[0].clone())
AnnouncerPollResult::Event(dummy_server.event(0))
);

crate::assert_future_in!(
Expand All @@ -44,7 +41,7 @@ async fn t09_changes_in_past_have_no_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::Event(events[1].clone())
AnnouncerPollResult::Event(dummy_server.event(1))
);

crate::assert_future_in!(
Expand All @@ -53,14 +50,11 @@ async fn t09_changes_in_past_have_no_effect() {
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

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()),
],
);
dummy_server.set_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()),
]);

crate::assert_future_in!(
announcer.poll(),
Expand All @@ -71,7 +65,7 @@ async fn t09_changes_in_past_have_no_effect() {
crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(7),
AnnouncerPollResult::Event(events[2].clone())
AnnouncerPollResult::Event(dummy_server.event(2))
);

crate::assert_future_in!(
Expand Down
4 changes: 4 additions & 0 deletions client/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl DummyScheduleServer {
*self.events.lock().unwrap() = events;
}

pub(crate) fn event(&self, idx: usize) -> Event {
self.events.lock().unwrap()[idx].clone()
}

pub(crate) async fn stop(&mut self) {
if let Some(handle) = self.handle.take() {
handle.abort();
Expand Down

0 comments on commit 93bd5cf

Please sign in to comment.