Skip to content

Commit

Permalink
Move announcer tests to individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed May 12, 2024
1 parent 53285a1 commit 30532c5
Show file tree
Hide file tree
Showing 10 changed files with 506 additions and 489 deletions.
489 changes: 0 additions & 489 deletions client/src/announcer/test.rs

This file was deleted.

20 changes: 20 additions & 0 deletions client/src/announcer/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod t02;
mod t03;
mod t04;
mod t05;
mod t06;
mod t07;
mod t08;
mod t09;

use super::*;
use crate::testing::DummyScheduleServer;
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 {
event.extra = HashMap::from([("type".to_string(), Value::String("talk".to_string()))]);
}
}
33 changes: 33 additions & 0 deletions client/src/announcer/test/t02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::*;

#[tokio::test]
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(),
)]);

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

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(3))
.build()
.unwrap(),
client,
)
.await
.unwrap();

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

dummy_server.stop().await;
}
38 changes: 38 additions & 0 deletions client/src/announcer/test/t03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use super::*;

#[tokio::test]
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(),
)]);

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

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(3))
.build()
.unwrap(),
client,
)
.await
.unwrap();

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

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(3),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::Changes)
);

dummy_server.stop().await;
}
51 changes: 51 additions & 0 deletions client/src/announcer/test/t04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::*;

#[tokio::test]
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 client = Client::new(dummy_server.url());

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(600))
.event_start_offset(ChronoDuration::zero())
.build()
.unwrap(),
client,
)
.await
.unwrap();

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

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

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

dummy_server.stop().await;
}
58 changes: 58 additions & 0 deletions client/src/announcer/test/t05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use super::*;

#[tokio::test]
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 client = Client::new(dummy_server.url());

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(600))
.event_start_offset(ChronoDuration::zero())
.build()
.unwrap(),
client,
)
.await
.unwrap();

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

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

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

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

dummy_server.stop().await;
}
55 changes: 55 additions & 0 deletions client/src/announcer/test/t06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use super::*;

#[tokio::test]
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 client = Client::new(dummy_server.url());

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(600))
.event_start_offset(ChronoDuration::zero())
.build()
.unwrap(),
client,
)
.await
.unwrap();

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

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

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

dummy_server.stop().await;
}
75 changes: 75 additions & 0 deletions client/src/announcer/test/t07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use super::*;

#[tokio::test]
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 client = Client::new(dummy_server.url());

let now_i = Instant::now();
let mut announcer = Announcer::new(
AnnouncerSettingsBuilder::default()
.schedule_refresh(Duration::from_secs(2))
.event_start_offset(ChronoDuration::zero())
.build()
.unwrap(),
client,
)
.await
.unwrap();

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

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(2),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

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

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(4),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(6),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

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

crate::assert_future_in!(
announcer.poll(),
now_i + Duration::from_secs(8),
AnnouncerPollResult::ScheduleRefreshed(AnnouncerScheduleChanges::NoChanges)
);

dummy_server.stop().await;
}
Loading

0 comments on commit 30532c5

Please sign in to comment.