feat: use ATUIN_TEST_SQLITE_STORE_TIMEOUT to specify test timeout of SQLite store (#1703)

Low-end devices like RISC-V SBCs are sometimes too slow to initialize SQLite in 0.1s. Option to specify a higher value allows check to pass on such devices with relaxed restrictions.
This commit is contained in:
Eric Long 2024-02-12 17:25:06 +08:00 committed by GitHub
parent 1464cb657a
commit 5a805dc22c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 17 deletions

View File

@ -197,7 +197,7 @@ mod tests {
use crypto_secretbox::{KeyInit, XSalsa20Poly1305}; use crypto_secretbox::{KeyInit, XSalsa20Poly1305};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use crate::record::sqlite_store::SqliteStore; use crate::record::sqlite_store::{test_sqlite_store_timeout, SqliteStore};
use super::{KvRecord, KvStore, KV_VERSION}; use super::{KvRecord, KvStore, KV_VERSION};
@ -221,7 +221,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn build_kv() { async fn build_kv() {
let mut store = SqliteStore::new(":memory:", 0.1).await.unwrap(); let mut store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let kv = KvStore::new(); let kv = KvStore::new();
let key: [u8; 32] = XSalsa20Poly1305::generate_key(&mut OsRng).into(); let key: [u8; 32] = XSalsa20Poly1305::generate_key(&mut OsRng).into();
let host_id = atuin_common::record::HostId(atuin_common::utils::uuid_v7()); let host_id = atuin_common::record::HostId(atuin_common::utils::uuid_v7());

View File

@ -351,6 +351,14 @@ impl Store for SqliteStore {
} }
} }
#[cfg(test)]
pub(crate) fn test_sqlite_store_timeout() -> f64 {
std::env::var("ATUIN_TEST_SQLITE_STORE_TIMEOUT")
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or(0.1)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use atuin_common::{ use atuin_common::{
@ -363,7 +371,7 @@ mod tests {
record::{encryption::PASETO_V4, store::Store}, record::{encryption::PASETO_V4, store::Store},
}; };
use super::SqliteStore; use super::{test_sqlite_store_timeout, SqliteStore};
fn test_record() -> Record<EncryptedData> { fn test_record() -> Record<EncryptedData> {
Record::builder() Record::builder()
@ -380,7 +388,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn create_db() { async fn create_db() {
let db = SqliteStore::new(":memory:", 0.1).await; let db = SqliteStore::new(":memory:", test_sqlite_store_timeout()).await;
assert!( assert!(
db.is_ok(), db.is_ok(),
@ -391,7 +399,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn push_record() { async fn push_record() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.expect("failed to insert record"); db.push(&record).await.expect("failed to insert record");
@ -399,7 +409,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn get_record() { async fn get_record() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.unwrap(); db.push(&record).await.unwrap();
@ -410,7 +422,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn last() { async fn last() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.unwrap(); db.push(&record).await.unwrap();
@ -428,7 +442,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn first() { async fn first() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.unwrap(); db.push(&record).await.unwrap();
@ -446,7 +462,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn len() { async fn len() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.unwrap(); db.push(&record).await.unwrap();
@ -460,7 +478,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn len_tag() { async fn len_tag() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let record = test_record(); let record = test_record();
db.push(&record).await.unwrap(); db.push(&record).await.unwrap();
@ -474,7 +494,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn len_different_tags() { async fn len_different_tags() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
// these have different tags, so the len should be the same // these have different tags, so the len should be the same
// we model multiple stores within one database // we model multiple stores within one database
@ -494,7 +516,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn append_a_bunch() { async fn append_a_bunch() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let mut tail = test_record(); let mut tail = test_record();
db.push(&tail).await.expect("failed to push record"); db.push(&tail).await.expect("failed to push record");
@ -519,7 +543,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn append_a_big_bunch() { async fn append_a_big_bunch() {
let db = SqliteStore::new(":memory:", 0.1).await.unwrap(); let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let mut records: Vec<Record<EncryptedData>> = Vec::with_capacity(10000); let mut records: Vec<Record<EncryptedData>> = Vec::with_capacity(10000);
@ -542,7 +568,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn re_encrypt() { async fn re_encrypt() {
let store = SqliteStore::new(":memory:", 0.1).await.unwrap(); let store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await
.unwrap();
let (key, _) = generate_encoded_key().unwrap(); let (key, _) = generate_encoded_key().unwrap();
let data = vec![0u8, 1u8, 2u8, 3u8]; let data = vec![0u8, 1u8, 2u8, 3u8];
let host_id = HostId(uuid_v7()); let host_id = HostId(uuid_v7());

View File

@ -324,7 +324,7 @@ mod tests {
use crate::record::{ use crate::record::{
encryption::PASETO_V4, encryption::PASETO_V4,
sqlite_store::SqliteStore, sqlite_store::{test_sqlite_store_timeout, SqliteStore},
store::Store, store::Store,
sync::{self, Operation}, sync::{self, Operation},
}; };
@ -351,10 +351,10 @@ mod tests {
local_records: Vec<Record<EncryptedData>>, local_records: Vec<Record<EncryptedData>>,
remote_records: Vec<Record<EncryptedData>>, remote_records: Vec<Record<EncryptedData>>,
) -> (SqliteStore, Vec<Diff>) { ) -> (SqliteStore, Vec<Diff>) {
let local_store = SqliteStore::new(":memory:", 0.1) let local_store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await .await
.expect("failed to open in memory sqlite"); .expect("failed to open in memory sqlite");
let remote_store = SqliteStore::new(":memory:", 0.1) let remote_store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
.await .await
.expect("failed to open in memory sqlite"); // "remote" .expect("failed to open in memory sqlite"); // "remote"