use std::sync::Arc;
use std::time::{Duration, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use super::engine::Kv;
use crate::{Options, Store, TestClock};
pub(crate) const SECOND: Duration = Duration::from_secs(1);
pub(crate) const MINUTE: Duration = Duration::from_secs(60);
pub(crate) const HOUR: Duration = Duration::from_secs(60 * 60);
pub(crate) const DAY: Duration = Duration::from_secs(24 * 60 * 60);
pub(crate) struct Fixture {
pub(crate) store: Store,
pub(crate) clock: Arc<TestClock>,
pub(crate) dir: tempfile::TempDir,
}
pub(crate) fn fixture() -> Fixture {
let dir = tempfile::tempdir().unwrap();
let clock = Arc::new(TestClock::new(UNIX_EPOCH + Duration::from_secs(1_800_000_000)));
let store = open(&dir, &clock);
Fixture { dir, store, clock }
}
fn open(dir: &tempfile::TempDir, clock: &Arc<TestClock>) -> Store {
let options = Options { clock: Some(clock.clone()), background: false, ..Options::default() };
Store::open(dir.path(), options).unwrap()
}
impl Fixture {
pub(crate) fn reopen(self) -> Fixture {
self.store.close().unwrap();
let store = open(&self.dir, &self.clock);
Fixture { store, ..self }
}
pub(crate) fn rows(&self, table: &str) -> i64 {
let kv = Kv::of(&self.store).unwrap();
let sql = format!("select count(*) from {table}");
kv.file().read(|connection| Ok(connection.query_row(&sql, [], |row| row.get(0)).unwrap())).unwrap()
}
pub(crate) fn commits(&self) -> u64 {
Kv::of(&self.store).unwrap().file().commits()
}
#[cfg(feature = "async")]
pub(crate) fn hold_writer(&self) -> WriterHeld {
let kv = Kv::of(&self.store).unwrap();
let (release, held_until) = std::sync::mpsc::channel::<()>();
let (holding, held) = std::sync::mpsc::channel();
let holder = std::thread::spawn(move || {
let _writer = kv.file().hold_writer();
holding.send(()).unwrap();
let _ = held_until.recv_timeout(Duration::from_secs(10));
});
held.recv().unwrap();
WriterHeld { release: Some(release), holder: Some(holder) }
}
}
#[cfg(feature = "async")]
pub(crate) struct WriterHeld {
release: Option<std::sync::mpsc::Sender<()>>,
holder: Option<std::thread::JoinHandle<()>>,
}
#[cfg(feature = "async")]
impl Drop for WriterHeld {
fn drop(&mut self) {
drop(self.release.take());
if let Some(holder) = self.holder.take() {
holder.join().unwrap();
}
}
}
#[cfg(feature = "async")]
pub(crate) fn eventually(what: &str, mut holds: impl FnMut() -> bool) {
let deadline = std::time::Instant::now() + Duration::from_secs(10);
while !holds() {
assert!(std::time::Instant::now() < deadline, "{what}: not within ten seconds");
std::thread::sleep(Duration::from_millis(1));
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub(crate) struct Session {
pub(crate) user: i64,
pub(crate) device: String,
}
pub(crate) fn session(user: i64) -> Session {
Session { user, device: "phone".to_owned() }
}