use std::time::Duration;
use super::super::fixture::{DAY, Fixture, fixture};
use super::super::maintain::maintain;
use super::*;
use crate::ErrorKind;
use crate::records::NewEvent;
const HOUR: Duration = Duration::from_secs(60 * 60);
fn segment(fixture: &Fixture, stream: &str, round: usize, events: usize) {
fixture.events(stream).append_all((0..events).map(|n| NewEvent::new(&format!("{round}-{n}")))).unwrap();
fixture.pass(HOUR);
maintain(&fixture.store).unwrap();
}
fn names(batch: &PollBatch) -> Vec<String> {
batch.events.iter().map(|event| event.name.clone()).collect()
}
fn drain(all: &AllEvents, mut cursor: Option<String>, limit: usize) -> (Vec<String>, String) {
let mut given = Vec::new();
loop {
let batch = all.poll_limit(cursor.as_deref(), limit).unwrap();
if batch.events.is_empty() {
return (given, batch.cursor);
}
given.extend(names(&batch));
cursor = Some(batch.cursor);
}
}
fn spelled(names: &[&str]) -> Vec<String> {
names.iter().map(|name| (*name).to_owned()).collect()
}
#[test]
fn a_poll_gives_every_packed_event_once_a_segment_of_one_stream_at_a_time() {
let fixture = fixture();
segment(&fixture, "api", 0, 3);
segment(&fixture, "web", 1, 2);
fixture.events("api").append("waiting", &()).unwrap();
let all = fixture.store.all_events().unwrap();
let first = all.poll(None).unwrap();
assert_eq!((names(&first), first.cursor.as_str(), first.expired), (spelled(&["0-0", "0-1", "0-2"]), "2:0", 0));
assert_eq!(first.events[0].stream, "api");
let second = all.poll(Some(&first.cursor)).unwrap();
assert_eq!(
(names(&second), second.events[0].stream.as_str(), second.cursor.as_str()),
(spelled(&["1-0", "1-1"]), "web", "3:0")
);
let third = all.poll(Some(&second.cursor)).unwrap();
assert_eq!((third.events.len(), third.cursor.as_str()), (0, "3:0"));
assert_eq!(all.poll_limit(None, 2).unwrap().cursor, "1:2");
assert_eq!(drain(&all, None, 2), (spelled(&["0-0", "0-1", "0-2", "1-0", "1-1"]), "3:0".to_owned()));
for (cursor, limit) in [(Some("x"), 10), (Some("1:-1"), 10), (None, 0), (None, 10_001)] {
assert_eq!(all.poll_limit(cursor, limit).unwrap_err().kind(), ErrorKind::Invalid, "{cursor:?} {limit}");
}
}
#[test]
fn a_cursor_keeps_its_place_across_merges_the_middle_of_a_merged_segment_included() {
let fixture = fixture();
for round in 0..3 {
segment(&fixture, "api", round, 4);
}
let all = fixture.store.all_events().unwrap();
let (mut given, mut cursor) = (Vec::new(), None);
for _ in 0..3 {
let batch = all.poll_limit(cursor.as_deref(), 2).unwrap();
given.extend(names(&batch));
cursor = Some(batch.cursor);
}
assert_eq!(cursor.as_deref(), Some("2:2"));
segment(&fixture, "api", 3, 4);
let held: i64 = fixture
.records()
.file
.read(|connection| {
Ok(connection.query_row("select held from segments where id = 1", [], |row| row.get(0)).unwrap())
})
.unwrap();
assert_eq!(held, 16);
let (rest, last) = drain(&all, cursor, 3);
given.extend(rest);
let every: Vec<String> = (0..4).flat_map(|round| (0..4).map(move |n| format!("{round}-{n}"))).collect();
assert_eq!((given, last.as_str()), (every, "5:0"));
}
#[test]
fn a_poll_counts_the_events_keep_removed_before_its_cursor_reached_them() {
let fixture = fixture();
fixture.store.events("api").keep(DAY).open().unwrap();
segment(&fixture, "api", 0, 3);
segment(&fixture, "api", 1, 5);
segment(&fixture, "web", 2, 2);
let all = fixture.store.all_events().unwrap();
let first = all.poll_limit(None, 1).unwrap();
assert_eq!(first.cursor, "1:1");
fixture.pass(DAY);
maintain(&fixture.store).unwrap();
let next = all.poll(Some(&first.cursor)).unwrap();
assert_eq!((names(&next), next.expired), (spelled(&["2-0", "2-1"]), 7));
assert_eq!(all.poll(None).unwrap().expired, 0);
}