use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::sql::Database;
use crate::{Error, ErrorKind, Options, Store, TestClock, sql};
const ORDERS: &str = "create table orders (id integer primary key, total integer not null) strict";
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Email {
order: i64,
}
struct Fixture {
store: Store,
clock: Arc<TestClock>,
dir: tempfile::TempDir,
}
fn fixture() -> Fixture {
let dir = tempfile::tempdir().unwrap();
let clock = Arc::new(TestClock::new(UNIX_EPOCH + Duration::from_secs(1_791_547_200)));
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 {
fn app(&self) -> Database {
self.store.database("app").migrations([("0001_orders.sql", ORDERS)]).open().unwrap()
}
fn reopen(&mut self) {
self.store.close().unwrap();
self.store = open(&self.dir, &self.clock);
}
}
fn orders(db: &Database) -> i64 {
db.scalar(sql!("select count(*) from orders")).unwrap()
}
#[test]
fn a_bucket_and_a_queue_opened_from_a_database_live_in_its_file() {
let mut f = fixture();
let db = f.app();
db.bucket::<String>("sessions").open().unwrap().set("t-1", &"ann".to_owned()).unwrap();
db.queue::<Email>("emails").open().unwrap().id("order-7").add(&Email { order: 7 }).unwrap();
assert!(!f.dir.path().join("kv.db").exists() && !f.dir.path().join("jobs.db").exists());
let tables: Vec<String> = db
.all(sql!("select name from sqlite_master where name in ('_tinystore_kv_cells', '_tinystore_jobs')"))
.unwrap();
assert_eq!(tables.len(), 2, "{tables:?}");
f.reopen();
let db = f.app();
let sessions = db.bucket::<String>("sessions").open().unwrap();
assert_eq!(sessions.get("t-1").unwrap().as_deref(), Some("ann"));
assert!(db.queue::<Email>("emails").open().unwrap().get("order-7").unwrap().is_some());
assert!(
f.store.bucket::<String>("sessions").open().unwrap().get("t-1").unwrap().is_none(),
"kv.db is another file"
);
}
#[test]
fn an_update_of_a_bucket_kept_in_a_database_reads_again_after_another_write() {
let f = fixture();
let db = f.app();
let carts = db.bucket::<Vec<String>>("carts").open().unwrap();
let (other, mut runs) = (carts.clone(), 0);
let changed = carts.update("ann", |cart| {
runs += 1;
if runs == 1 {
other.set("ann", &vec!["tea".to_owned()]).unwrap();
}
let mut cart = cart.unwrap_or_default();
cart.push("milk".to_owned());
Some(cart)
});
assert_eq!(changed.unwrap(), Some(vec!["tea".to_owned(), "milk".to_owned()]));
assert_eq!(runs, 2, "the other write came before the create, so the change ran again on it");
assert!(!f.dir.path().join("kv.db").exists(), "kept in the database's file");
}
#[test]
fn rows_keys_and_jobs_commit_together_or_not_at_all() {
let f = fixture();
let db = f.app();
let (sessions, emails) =
(db.bucket::<String>("sessions").open().unwrap(), db.queue::<Email>("emails").open().unwrap());
db.tx(|tx| -> crate::Result<()> {
let order: i64 = tx.scalar(sql!("insert into orders (total) values (?) returning id", 70))?;
assert!(tx.with(&emails).id(format!("order-{order}")).add(&Email { order })?);
tx.with(&sessions).set("t-1", &"ann".to_owned())?;
assert!(tx.with(&emails).get("order-1")?.is_some(), "a read inside sees what the transaction wrote");
Ok(())
})
.unwrap();
assert_eq!(orders(&db), 1);
assert!(emails.get("order-1").unwrap().is_some());
assert_eq!(sessions.get("t-1").unwrap().as_deref(), Some("ann"));
let failed = db.tx(|tx| -> crate::Result<()> {
tx.exec(sql!("insert into orders (total) values (?)", 80))?;
tx.with(&emails).id("order-2").add(&Email { order: 2 })?;
tx.with(&sessions).delete("t-1")?;
Err(Error::invalid("the card was declined"))
});
assert_eq!(failed.unwrap_err().kind(), ErrorKind::Invalid);
assert_eq!(orders(&db), 1, "the row rolled back");
assert!(emails.get("order-2").unwrap().is_none(), "and the job");
assert_eq!(sessions.get("t-1").unwrap().as_deref(), Some("ann"), "and the delete");
}
#[test]
fn a_worker_runs_a_job_added_in_a_transaction_once_it_commits() {
let f = fixture();
let db = f.app();
let emails = db.queue::<Email>("emails").open().unwrap();
let sent = Arc::new(Mutex::new(Vec::new()));
let kept = Arc::clone(&sent);
let worker = emails
.work(move |email: Email, _run| -> crate::Result<()> {
kept.lock().unwrap().push(email.order);
Ok(())
})
.unwrap();
thread::sleep(Duration::from_millis(50));
db.tx(|tx| tx.with(&emails).add(&Email { order: 9 })).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while sent.lock().unwrap().is_empty() {
assert!(Instant::now() < deadline, "the worker slept past the commit");
thread::sleep(Duration::from_millis(2));
}
assert_eq!(*sent.lock().unwrap(), [9]);
worker.stop();
}
#[test]
fn a_batch_commits_its_rows_and_its_jobs_together_or_not_at_all() {
let f = fixture();
let db = f.app();
let emails = db.queue::<Email>("emails").open().unwrap();
let done = db
.batch([
sql!("insert into orders (id, total) values (?, ?)", 1, 70).into(),
emails.id("order-1").adding(&Email { order: 1 }),
emails.adding(&Email { order: 1 }),
])
.unwrap();
assert_eq!(done.iter().map(|done| done.changes).collect::<Vec<_>>(), [1, 1, 1]);
assert_eq!((done[0].last_insert_rowid, done[1].last_insert_rowid), (1, 0), "a job has no rowid");
assert_eq!(orders(&db), 1);
assert_eq!(emails.get("order-1").unwrap().unwrap().value, Email { order: 1 });
let taken = db
.batch([
sql!("insert into orders (id, total) values (?, ?)", 2, 80).into(),
emails.id("order-1").adding(&Email { order: 2 }),
])
.unwrap();
assert_eq!(taken.iter().map(|done| done.changes).collect::<Vec<_>>(), [1, 0], "a taken id adds nothing");
assert_eq!(emails.get("order-1").unwrap().unwrap().value, Email { order: 1 });
let failed = db.batch([
emails.id("order-3").adding(&Email { order: 3 }),
sql!("insert into orders (id, total) values (?, ?)", 1, 90).into(),
]);
assert_eq!(failed.unwrap_err().kind(), ErrorKind::Conflict, "the row's key is taken");
assert!(emails.get("order-3").unwrap().is_none(), "the job rolled back with the batch");
assert_eq!(orders(&db), 2);
}
#[test]
fn a_worker_runs_a_job_added_in_a_batch_once_it_commits() {
let f = fixture();
let db = f.app();
let emails = db.queue::<Email>("emails").open().unwrap();
let sent = Arc::new(Mutex::new(Vec::new()));
let kept = Arc::clone(&sent);
let worker = emails
.work(move |email: Email, _run| -> crate::Result<()> {
kept.lock().unwrap().push(email.order);
Ok(())
})
.unwrap();
thread::sleep(Duration::from_millis(50));
db.batch([sql!("insert into orders (total) values (?)", 9).into(), emails.adding(&Email { order: 9 })]).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while sent.lock().unwrap().is_empty() {
assert!(Instant::now() < deadline, "the worker slept past the commit");
thread::sleep(Duration::from_millis(2));
}
assert_eq!(*sent.lock().unwrap(), [9]);
worker.stop();
}
#[test]
fn a_batch_refuses_a_queue_kept_in_another_file_and_an_add_it_cannot_make() {
let f = fixture();
let db = f.app();
let other = f.store.database("other").open().unwrap();
let in_jobs = f.store.queue::<Email>("emails").open().unwrap();
let in_other = other.queue::<Email>("emails").open().unwrap();
let emails = db.queue::<Email>("emails").open().unwrap();
let insert = || sql!("insert into orders (total) values (?)", 1).into();
let error = db.batch([insert(), in_jobs.adding(&Email { order: 1 })]).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid);
let said = "sql app: jobs queue emails: kept in jobs.db, outside this batch of sql app";
assert!(error.to_string().starts_with(said), "{error}");
let error = db.batch([insert(), in_other.adding(&Email { order: 1 })]).unwrap_err();
assert!(error.to_string().contains("kept in sql other, outside this batch of sql app"), "{error}");
let error = db.batch([insert(), emails.id("").adding(&Email { order: 1 })]).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid);
assert!(error.to_string().starts_with("sql app: jobs queue emails: id \"\": an id of 0 bytes"), "{error}");
let error = db.tx(|tx| db.batch([tx.with(&emails).id("in-tx").adding(&Email { order: 1 })])).unwrap_err();
assert!(error.to_string().contains("made through a transaction runs in it, not in a batch"), "{error}");
assert_eq!(orders(&db), 0, "a refused batch writes nothing");
assert!(in_jobs.get("in-tx").unwrap().is_none() && emails.get("in-tx").unwrap().is_none());
}
#[test]
fn a_transaction_refuses_a_handle_kept_in_another_file() {
let f = fixture();
let db = f.app();
let in_kv = f.store.bucket::<String>("sessions").open().unwrap();
let in_db = db.bucket::<String>("sessions").open().unwrap();
let emails = db.queue::<Email>("emails").open().unwrap();
let error = db.tx(|tx| tx.with(&in_kv).set("t-1", &"ann".to_owned())).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid);
assert!(error.to_string().contains("kept in kv.db, outside this transaction of sql app"), "{error}");
let error = f.store.tx(|tx| tx.with(&in_db).set("t-1", &"ann".to_owned())).unwrap_err();
assert!(error.to_string().starts_with("sql app: kv bucket sessions: key \"t-1\": kept in sql app"), "{error}");
let error = db.tx(|_| emails.add(&Email { order: 1 })).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "a call around the transaction would wait for its writer: {error}");
}
#[test]
fn a_cancel_that_rolls_back_leaves_the_job() {
let f = fixture();
let db = f.app();
let emails = db.queue::<Email>("emails").open().unwrap();
emails.id("order-7").add(&Email { order: 7 }).unwrap();
let failed = db.tx(|tx| -> crate::Result<()> {
assert!(tx.with(&emails).cancel("order-7")?);
assert!(tx.with(&emails).get("order-7")?.is_none());
Err(Error::invalid("changed its mind"))
});
assert!(failed.is_err());
assert!(emails.get("order-7").unwrap().is_some());
db.tx(|tx| tx.with(&emails).cancel("order-7")).unwrap();
assert!(emails.get("order-7").unwrap().is_none());
}
#[test]
fn the_store_closes_what_a_database_keeps_before_its_file() {
let mut f = fixture();
let db = f.app();
let views = db.bucket::<i64>("views").idle(Duration::from_secs(3600)).open().unwrap();
views.set("home", &1).unwrap();
f.clock.advance(Duration::from_secs(600));
views.get("home").unwrap(); let emails = db.queue::<Email>("emails").open().unwrap();
let _worker = emails.work(|_: Email, _run| -> crate::Result<()> { Ok(()) }).unwrap();
f.reopen();
let entry = f.app().bucket::<i64>("views").open().unwrap().entry("home").unwrap().unwrap();
let expires = entry.expires_at.unwrap().duration_since(f.store.now()).unwrap();
assert_eq!(expires, Duration::from_secs(3600), "the renewal was written before the file closed");
}
#[cfg(feature = "kv")]
#[test]
fn a_bucket_kept_in_a_database_commits_as_far_as_the_database_does() {
let dir = tempfile::tempdir().unwrap();
let store = Store::open(dir.path(), Options { background: false, ..Options::default() }).unwrap();
let fast = store.database("fast").durability(crate::Durability::Os).open().unwrap();
let sessions = fast.bucket::<i64>("sessions").open().unwrap();
sessions.set("a", &1).unwrap();
let kept: i64 = fast.scalar(sql!("select count(*) from sqlite_schema where name = '_tinystore_kv_cells'")).unwrap();
assert_eq!(kept, 1, "kv's tables are in the database's file");
assert_eq!(fast.tx(|tx| tx.scalar::<i64>(sql!("pragma synchronous"))).unwrap(), 1);
assert_eq!(sessions.get("a").unwrap(), Some(1));
store.close().unwrap();
}
#[test]
fn an_encrypted_bucket_of_a_database_commits_with_its_rows_and_shows_no_value_in_its_file() {
let fixture = fixture();
let db = fixture.app();
let passwords = db.bucket::<String>("source-passwords").encrypted().open().unwrap();
db.tx(|tx| -> Result<(), Error> {
tx.exec(sql!("insert into orders (id, total) values (?, ?)", 7, 100))?;
tx.with(&passwords).set(7, &"hunter2-the-password".to_owned())
})
.unwrap();
assert_eq!(passwords.get(7).unwrap().as_deref(), Some("hunter2-the-password"));
let files = std::fs::read_dir(fixture.dir.path().join("sql")).unwrap().map(|entry| entry.unwrap().path());
for file in files {
let bytes = std::fs::read(&file).unwrap();
assert!(!bytes.windows(7).any(|window| window == b"hunter2"), "{} shows the value", file.display());
}
assert!(fixture.dir.path().join("encryption.key").exists(), "the store's key, not the database's");
}