use tracing::error;
use uuid::Uuid;
pub mod consistency_utils;
pub mod default_cluster_options;
pub mod doc_generation;
pub mod features;
mod helpers;
pub mod node_version;
pub mod test_binary_collection;
pub mod test_bucket;
pub mod test_cluster;
pub mod test_collection;
pub mod test_config;
pub mod test_manager;
pub mod test_query_index_manager;
pub mod test_scope;
pub mod test_search_index_manager;
use rand::distr::Alphanumeric;
use rand::{rng, Rng, RngExt};
use tokio::time::Instant;
pub fn new_key() -> String {
Uuid::new_v4().to_string()
}
pub fn generate_key_with_letter_prefix() -> String {
let mut name = new_key();
loop {
if !name.as_bytes()[0].is_ascii_alphabetic() {
name = name[1..].to_string();
} else {
break;
}
}
name
}
pub fn generate_string_value(len: usize) -> String {
rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect::<String>()
}
pub async fn try_times<Fut, T>(
times: usize,
sleep: tokio::time::Duration,
fail_msg: impl AsRef<str>,
mut f: impl FnMut() -> Fut,
) -> T
where
Fut: std::future::Future<Output = Result<Option<T>, couchbase::error::Error>>,
{
let mut count = 0;
while count < times {
match f().await {
Ok(Some(r)) => return r,
Ok(None) => {}
Err(e) => {
error!("{e:?}");
}
};
tokio::time::sleep(sleep).await;
count += 1;
}
panic!("{}", fail_msg.as_ref());
}
pub async fn try_until<Fut, T>(
deadline: Instant,
sleep: tokio::time::Duration,
fail_msg: impl AsRef<str>,
mut f: impl FnMut() -> Fut,
) -> T
where
Fut: std::future::Future<Output = Result<Option<T>, couchbase::error::Error>>,
{
while Instant::now() < deadline {
match f().await {
Ok(Some(r)) => return r,
Ok(None) => {}
Err(e) => {
error!("{e:?}");
}
};
tokio::time::sleep(sleep).await;
}
panic!("{}", fail_msg.as_ref());
}