Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add env ATUIN_TEST_LOCAL_TIMEOUT to control test timeout of SQLite #2337

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions crates/atuin-client/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,8 @@ impl Database for Sqlite {

#[cfg(test)]
mod test {
use crate::settings::test_local_timeout;

use super::*;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -834,7 +836,9 @@ mod test {

#[tokio::test(flavor = "multi_thread")]
async fn test_search_prefix() {
let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
let mut db = Sqlite::new("sqlite::memory:", test_local_timeout())
.await
.unwrap();
new_history_item(&mut db, "ls /home/ellie").await.unwrap();

assert_search_eq(&db, SearchMode::Prefix, FilterMode::Global, "ls", 1)
Expand All @@ -850,7 +854,9 @@ mod test {

#[tokio::test(flavor = "multi_thread")]
async fn test_search_fulltext() {
let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
let mut db = Sqlite::new("sqlite::memory:", test_local_timeout())
.await
.unwrap();
new_history_item(&mut db, "ls /home/ellie").await.unwrap();

assert_search_eq(&db, SearchMode::FullText, FilterMode::Global, "ls", 1)
Expand Down Expand Up @@ -934,7 +940,9 @@ mod test {

#[tokio::test(flavor = "multi_thread")]
async fn test_search_fuzzy() {
let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
let mut db = Sqlite::new("sqlite::memory:", test_local_timeout())
.await
.unwrap();
new_history_item(&mut db, "ls /home/ellie").await.unwrap();
new_history_item(&mut db, "ls /home/frank").await.unwrap();
new_history_item(&mut db, "cd /home/Ellie").await.unwrap();
Expand Down Expand Up @@ -1035,7 +1043,9 @@ mod test {

#[tokio::test(flavor = "multi_thread")]
async fn test_search_reordered_fuzzy() {
let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
let mut db = Sqlite::new("sqlite::memory:", test_local_timeout())
.await
.unwrap();
// test ordering of results: we should choose the first, even though it happened longer ago.

new_history_item(&mut db, "curl").await.unwrap();
Expand Down Expand Up @@ -1069,7 +1079,9 @@ mod test {
git_root: None,
};

let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
let mut db = Sqlite::new("sqlite::memory:", test_local_timeout())
.await
.unwrap();
for _i in 1..10000 {
new_history_item(&mut db, "i am a duplicated command")
.await
Expand Down
5 changes: 3 additions & 2 deletions crates/atuin-client/src/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ mod tests {
use crypto_secretbox::{KeyInit, XSalsa20Poly1305};
use rand::rngs::OsRng;

use crate::record::sqlite_store::{test_sqlite_store_timeout, SqliteStore};
use crate::record::sqlite_store::SqliteStore;
use crate::settings::test_local_timeout;

use super::{KvRecord, KvStore, KV_VERSION};

Expand All @@ -221,7 +222,7 @@ mod tests {

#[tokio::test]
async fn build_kv() {
let mut store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let mut store = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let kv = KvStore::new();
Expand Down
33 changes: 13 additions & 20 deletions crates/atuin-client/src/record/sqlite_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ 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)]
mod tests {
use atuin_common::{
Expand All @@ -380,9 +372,10 @@ mod tests {
use crate::{
encryption::generate_encoded_key,
record::{encryption::PASETO_V4, store::Store},
settings::test_local_timeout,
};

use super::{test_sqlite_store_timeout, SqliteStore};
use super::SqliteStore;

fn test_record() -> Record<EncryptedData> {
Record::builder()
Expand All @@ -399,7 +392,7 @@ mod tests {

#[tokio::test]
async fn create_db() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout()).await;
let db = SqliteStore::new(":memory:", test_local_timeout()).await;

assert!(
db.is_ok(),
Expand All @@ -410,7 +403,7 @@ mod tests {

#[tokio::test]
async fn push_record() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -420,7 +413,7 @@ mod tests {

#[tokio::test]
async fn get_record() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -433,7 +426,7 @@ mod tests {

#[tokio::test]
async fn last() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -453,7 +446,7 @@ mod tests {

#[tokio::test]
async fn first() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -473,7 +466,7 @@ mod tests {

#[tokio::test]
async fn len() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -489,7 +482,7 @@ mod tests {

#[tokio::test]
async fn len_tag() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let record = test_record();
Expand All @@ -505,7 +498,7 @@ mod tests {

#[tokio::test]
async fn len_different_tags() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();

Expand All @@ -527,7 +520,7 @@ mod tests {

#[tokio::test]
async fn append_a_bunch() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();

Expand All @@ -554,7 +547,7 @@ mod tests {

#[tokio::test]
async fn append_a_big_bunch() {
let db = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let db = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();

Expand All @@ -579,7 +572,7 @@ mod tests {

#[tokio::test]
async fn re_encrypt() {
let store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let store = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let (key, _) = generate_encoded_key().unwrap();
Expand Down
17 changes: 10 additions & 7 deletions crates/atuin-client/src/record/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,14 @@ mod tests {
use atuin_common::record::{Diff, EncryptedData, HostId, Record};
use pretty_assertions::assert_eq;

use crate::record::{
encryption::PASETO_V4,
sqlite_store::{test_sqlite_store_timeout, SqliteStore},
store::Store,
sync::{self, Operation},
use crate::{
record::{
encryption::PASETO_V4,
sqlite_store::SqliteStore,
store::Store,
sync::{self, Operation},
},
settings::test_local_timeout,
};

fn test_record() -> Record<EncryptedData> {
Expand All @@ -357,10 +360,10 @@ mod tests {
local_records: Vec<Record<EncryptedData>>,
remote_records: Vec<Record<EncryptedData>>,
) -> (SqliteStore, Vec<Diff>) {
let local_store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let local_store = SqliteStore::new(":memory:", test_local_timeout())
.await
.expect("failed to open in memory sqlite");
let remote_store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let remote_store = SqliteStore::new(":memory:", test_local_timeout())
.await
.expect("failed to open in memory sqlite"); // "remote"

Expand Down
10 changes: 10 additions & 0 deletions crates/atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,16 @@ impl Default for Settings {
}
}

#[cfg(test)]
pub(crate) fn test_local_timeout() -> f64 {
std::env::var("ATUIN_TEST_LOCAL_TIMEOUT")
.ok()
.and_then(|x| x.parse().ok())
// this hardcoded value should be replaced by a simple way to get the
// default local_timeout of Settings if possible
.unwrap_or(2.0)
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down
12 changes: 7 additions & 5 deletions crates/atuin-dotfiles/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ impl AliasStore {
}

#[cfg(test)]
pub(crate) fn test_sqlite_store_timeout() -> f64 {
std::env::var("ATUIN_TEST_SQLITE_STORE_TIMEOUT")
pub(crate) fn test_local_timeout() -> f64 {
std::env::var("ATUIN_TEST_LOCAL_TIMEOUT")
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or(0.1)
// this hardcoded value should be replaced by a simple way to get the
// default local_timeout of Settings if possible
.unwrap_or(2.0)
}

#[cfg(test)]
Expand All @@ -313,7 +315,7 @@ mod tests {

use crate::shell::Alias;

use super::{test_sqlite_store_timeout, AliasRecord, AliasStore, CONFIG_SHELL_ALIAS_VERSION};
use super::{test_local_timeout, AliasRecord, AliasStore, CONFIG_SHELL_ALIAS_VERSION};
use crypto_secretbox::{KeyInit, XSalsa20Poly1305};

#[test]
Expand All @@ -335,7 +337,7 @@ mod tests {

#[tokio::test]
async fn build_aliases() {
let store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let store = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let key: [u8; 32] = XSalsa20Poly1305::generate_key(&mut OsRng).into();
Expand Down
14 changes: 3 additions & 11 deletions crates/atuin-dotfiles/src/store/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,15 @@ impl VarStore {
}
}

#[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)]
mod tests {
use rand::rngs::OsRng;

use atuin_client::record::sqlite_store::SqliteStore;

use crate::shell::Var;
use crate::{shell::Var, store::test_local_timeout};

use super::{test_sqlite_store_timeout, VarRecord, VarStore, DOTFILES_VAR_VERSION};
use super::{VarRecord, VarStore, DOTFILES_VAR_VERSION};
use crypto_secretbox::{KeyInit, XSalsa20Poly1305};

#[test]
Expand All @@ -327,7 +319,7 @@ mod tests {

#[tokio::test]
async fn build_vars() {
let store = SqliteStore::new(":memory:", test_sqlite_store_timeout())
let store = SqliteStore::new(":memory:", test_local_timeout())
.await
.unwrap();
let key: [u8; 32] = XSalsa20Poly1305::generate_key(&mut OsRng).into();
Expand Down
Loading