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

Introduce the new_test_store and restructure the code. #2574

Merged
merged 18 commits into from
Oct 8, 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
2 changes: 1 addition & 1 deletion linera-chain/src/unit_tests/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use linera_execution::{
use linera_views::{
context::{Context as _, MemoryContext},
memory::TEST_MEMORY_MAX_STREAM_QUERIES,
test_utils::generate_test_namespace,
random::generate_test_namespace,
views::{View, ViewError},
};

Expand Down
128 changes: 53 additions & 75 deletions linera-core/src/unit_tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,20 @@ use linera_execution::{
};
use linera_storage::{DbStorage, Storage, TestClock};
#[cfg(all(not(target_arch = "wasm32"), feature = "storage-service"))]
use linera_storage_service::{
client::{service_config_from_endpoint, ServiceStoreClient},
common::storage_service_test_endpoint,
};
use linera_storage_service::client::ServiceStoreClient;
use linera_version::VersionInfo;
#[cfg(feature = "dynamodb")]
use linera_views::dynamo_db::{
create_dynamo_db_common_config, DynamoDbStore, DynamoDbStoreConfig, LocalStackTestContext,
};
use linera_views::dynamo_db::DynamoDbStore;
#[cfg(feature = "scylladb")]
use linera_views::scylla_db::{create_scylla_db_common_config, ScyllaDbStore, ScyllaDbStoreConfig};
use linera_views::scylla_db::ScyllaDbStore;
use linera_views::{
memory::{create_memory_store_test_config, MemoryStore},
test_utils::generate_test_namespace,
memory::MemoryStore, random::generate_test_namespace, store::TestKeyValueStore as _,
};
use tokio::sync::oneshot;
use tokio_stream::wrappers::UnboundedReceiverStream;
#[cfg(feature = "rocksdb")]
use {
linera_views::rocks_db::RocksDbStore,
linera_views::store::AdminKeyValueStore as _,
tokio::sync::{Semaphore, SemaphorePermit},
};

Expand Down Expand Up @@ -882,6 +875,8 @@ static ROCKS_DB_SEMAPHORE: Semaphore = Semaphore::const_new(5);

#[derive(Default)]
pub struct MemoryStorageBuilder {
namespace: String,
instance_counter: usize,
wasm_runtime: Option<WasmRuntime>,
clock: TestClock,
}
Expand All @@ -891,11 +886,15 @@ impl StorageBuilder for MemoryStorageBuilder {
type Storage = DbStorage<MemoryStore, TestClock>;

async fn build(&mut self) -> Result<Self::Storage, anyhow::Error> {
let store_config = create_memory_store_test_config();
let namespace = generate_test_namespace();
self.instance_counter += 1;
let config = MemoryStore::new_test_config().await?;
if self.namespace.is_empty() {
self.namespace = generate_test_namespace();
}
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
let root_key = &[];
Ok(DbStorage::new_for_testing(
store_config,
config,
&namespace,
root_key,
self.wasm_runtime,
Expand Down Expand Up @@ -923,6 +922,8 @@ impl MemoryStorageBuilder {

#[cfg(feature = "rocksdb")]
pub struct RocksDbStorageBuilder {
namespace: String,
instance_counter: usize,
wasm_runtime: Option<WasmRuntime>,
clock: TestClock,
_permit: SemaphorePermit<'static>,
Expand All @@ -932,6 +933,8 @@ pub struct RocksDbStorageBuilder {
impl RocksDbStorageBuilder {
pub async fn new() -> Self {
RocksDbStorageBuilder {
namespace: String::new(),
instance_counter: 0,
wasm_runtime: None,
clock: TestClock::default(),
_permit: ROCKS_DB_SEMAPHORE.acquire().await.unwrap(),
Expand All @@ -955,18 +958,21 @@ impl StorageBuilder for RocksDbStorageBuilder {
type Storage = DbStorage<RocksDbStore, TestClock>;

async fn build(&mut self) -> Result<Self::Storage, anyhow::Error> {
let store_config = RocksDbStore::new_test_config().await?;
let namespace = generate_test_namespace();
self.instance_counter += 1;
let config = RocksDbStore::new_test_config().await?;
if self.namespace.is_empty() {
self.namespace = generate_test_namespace();
}
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
let root_key = &[];
let storage = DbStorage::new_for_testing(
store_config,
Ok(DbStorage::new_for_testing(
config,
&namespace,
root_key,
self.wasm_runtime,
self.clock.clone(),
)
.await?;
Ok(storage)
.await?)
}

fn clock(&self) -> &TestClock {
Expand All @@ -975,8 +981,8 @@ impl StorageBuilder for RocksDbStorageBuilder {
}

#[cfg(all(not(target_arch = "wasm32"), feature = "storage-service"))]
#[derive(Default)]
pub struct ServiceStorageBuilder {
endpoint: String,
namespace: String,
instance_counter: usize,
wasm_runtime: Option<WasmRuntime>,
Expand All @@ -992,15 +998,9 @@ impl ServiceStorageBuilder {

/// Creates a `ServiceStorage` with the given Wasm runtime.
pub async fn with_wasm_runtime(wasm_runtime: impl Into<Option<WasmRuntime>>) -> Self {
let endpoint = storage_service_test_endpoint().unwrap();
let clock = TestClock::default();
let namespace = generate_test_namespace();
Self {
endpoint,
namespace,
instance_counter: 0,
ServiceStorageBuilder {
wasm_runtime: wasm_runtime.into(),
clock,
..ServiceStorageBuilder::default()
}
}
}
Expand All @@ -1011,12 +1011,15 @@ impl StorageBuilder for ServiceStorageBuilder {
type Storage = DbStorage<ServiceStoreClient, TestClock>;

async fn build(&mut self) -> anyhow::Result<Self::Storage> {
let store_config = service_config_from_endpoint(&self.endpoint)?;
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
self.instance_counter += 1;
let config = ServiceStoreClient::new_test_config().await?;
if self.namespace.is_empty() {
self.namespace = generate_test_namespace();
}
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
let root_key = &[];
Ok(DbStorage::new_for_testing(
store_config,
config,
&namespace,
root_key,
self.wasm_runtime,
Expand All @@ -1033,8 +1036,8 @@ impl StorageBuilder for ServiceStorageBuilder {
#[cfg(feature = "dynamodb")]
#[derive(Default)]
pub struct DynamoDbStorageBuilder {
namespace: String,
instance_counter: usize,
localstack: Option<LocalStackTestContext>,
wasm_runtime: Option<WasmRuntime>,
clock: TestClock,
}
Expand All @@ -1058,28 +1061,21 @@ impl StorageBuilder for DynamoDbStorageBuilder {
type Storage = DbStorage<DynamoDbStore, TestClock>;

async fn build(&mut self) -> Result<Self::Storage, anyhow::Error> {
if self.localstack.is_none() {
self.localstack = Some(LocalStackTestContext::new().await?);
self.instance_counter += 1;
let config = DynamoDbStore::new_test_config().await?;
if self.namespace.is_empty() {
self.namespace = generate_test_namespace();
}
let config = self.localstack.as_ref().unwrap().dynamo_db_config();
let namespace = generate_test_namespace();
let namespace = format!("{}_{}", namespace, self.instance_counter);
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
let root_key = &[];
let common_config = create_dynamo_db_common_config();
let store_config = DynamoDbStoreConfig {
Ok(DbStorage::new_for_testing(
config,
common_config,
};
self.instance_counter += 1;
let storage = DbStorage::new_for_testing(
store_config,
&namespace,
root_key,
self.wasm_runtime,
self.clock.clone(),
)
.await?;
Ok(storage)
.await?)
}

fn clock(&self) -> &TestClock {
Expand All @@ -1088,29 +1084,14 @@ impl StorageBuilder for DynamoDbStorageBuilder {
}

#[cfg(feature = "scylladb")]
#[derive(Default)]
pub struct ScyllaDbStorageBuilder {
namespace: String,
instance_counter: usize,
uri: String,
wasm_runtime: Option<WasmRuntime>,
clock: TestClock,
}

#[cfg(feature = "scylladb")]
impl Default for ScyllaDbStorageBuilder {
fn default() -> Self {
let instance_counter = 0;
let uri = "localhost:9042".to_string();
let wasm_runtime = None;
let clock = TestClock::new();
ScyllaDbStorageBuilder {
instance_counter,
uri,
wasm_runtime,
clock,
}
}
}

#[cfg(feature = "scylladb")]
impl ScyllaDbStorageBuilder {
/// Creates a [`ScyllaDbStorageBuilder`] that uses the specified [`WasmRuntime`] to run Wasm
Expand All @@ -1131,23 +1112,20 @@ impl StorageBuilder for ScyllaDbStorageBuilder {

async fn build(&mut self) -> Result<Self::Storage, anyhow::Error> {
self.instance_counter += 1;
let namespace = generate_test_namespace();
let namespace = format!("{}_{}", namespace, self.instance_counter);
let config = ScyllaDbStore::new_test_config().await?;
if self.namespace.is_empty() {
self.namespace = generate_test_namespace();
}
let namespace = format!("{}_{}", self.namespace, self.instance_counter);
let root_key = &[];
let common_config = create_scylla_db_common_config();
let store_config = ScyllaDbStoreConfig {
uri: self.uri.clone(),
common_config,
};
let storage = DbStorage::new_for_testing(
store_config,
Ok(DbStorage::new_for_testing(
config,
&namespace,
root_key,
self.wasm_runtime,
self.clock.clone(),
)
.await?;
Ok(storage)
.await?)
}

fn clock(&self) -> &TestClock {
Expand Down
7 changes: 4 additions & 3 deletions linera-core/src/unit_tests/worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ use linera_execution::{
use linera_storage::{DbStorage, Storage, TestClock};
use linera_views::{
context::MemoryContext,
memory::{create_memory_store_test_config, MemoryStore},
test_utils::generate_test_namespace,
memory::MemoryStore,
random::generate_test_namespace,
store::TestKeyValueStore as _,
views::{CryptoHashView, RootView},
};
use test_case::test_case;
Expand Down Expand Up @@ -2886,7 +2887,7 @@ where
#[test(tokio::test)]
async fn test_cross_chain_helper() -> anyhow::Result<()> {
// Make a committee and worker (only used for signing certificates)
let store_config = create_memory_store_test_config();
let store_config = MemoryStore::new_test_config().await?;
let namespace = generate_test_namespace();
let root_key = &[];
let store = DbStorage::<MemoryStore, _>::new_for_testing(
Expand Down
2 changes: 1 addition & 1 deletion linera-execution/src/test_utils/system_execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use linera_base::{
use linera_views::{
context::{Context, MemoryContext},
memory::TEST_MEMORY_MAX_STREAM_QUERIES,
test_utils::generate_test_namespace,
random::generate_test_namespace,
views::{CryptoHashView, View, ViewError},
};

Expand Down
20 changes: 10 additions & 10 deletions linera-service/src/cli_wrappers/local_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use linera_execution::ResourceControlPolicy;
#[cfg(all(feature = "storage-service", with_testing))]
use linera_storage_service::common::storage_service_test_endpoint;
#[cfg(all(feature = "scylladb", with_testing))]
use linera_views::scylla_db::create_scylla_db_test_uri;
use linera_views::{scylla_db::ScyllaDbStore, store::TestKeyValueStore as _};
use tempfile::{tempdir, TempDir};
use tokio::process::{Child, Command};
use tonic_health::pb::{
Expand Down Expand Up @@ -57,14 +57,14 @@ pub async fn get_node_port() -> u16 {
}

#[cfg(with_testing)]
async fn make_testing_config(database: Database) -> StorageConfig {
async fn make_testing_config(database: Database) -> Result<StorageConfig> {
match database {
Database::Service => {
#[cfg(feature = "storage-service")]
{
let endpoint = storage_service_test_endpoint()
.expect("Reading LINERA_STORAGE_SERVICE environment variable");
StorageConfig::Service { endpoint }
Ok(StorageConfig::Service { endpoint })
}
#[cfg(not(feature = "storage-service"))]
panic!("Database::Service is selected without the feature storage_service");
Expand All @@ -73,16 +73,16 @@ async fn make_testing_config(database: Database) -> StorageConfig {
#[cfg(feature = "dynamodb")]
{
let use_localstack = true;
StorageConfig::DynamoDb { use_localstack }
Ok(StorageConfig::DynamoDb { use_localstack })
}
#[cfg(not(feature = "dynamodb"))]
panic!("Database::DynamoDb is selected without the feature aws");
}
Database::ScyllaDb => {
#[cfg(feature = "scylladb")]
{
let uri = create_scylla_db_test_uri();
StorageConfig::ScyllaDb { uri }
let config = ScyllaDbStore::new_test_config().await?;
Ok(StorageConfig::ScyllaDb { uri: config.uri })
}
#[cfg(not(feature = "scylladb"))]
panic!("Database::ScyllaDb is selected without the feature sctlladb");
Expand All @@ -100,11 +100,11 @@ pub enum StorageConfigBuilder {

impl StorageConfigBuilder {
#[allow(unused_variables)]
pub async fn build(self, database: Database) -> StorageConfig {
pub async fn build(self, database: Database) -> Result<StorageConfig> {
match self {
#[cfg(with_testing)]
StorageConfigBuilder::TestConfig => make_testing_config(database).await,
StorageConfigBuilder::ExistingConfig { storage_config } => storage_config,
StorageConfigBuilder::ExistingConfig { storage_config } => Ok(storage_config),
}
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ impl LocalNetConfig {
initial_amount: Amount::from_tokens(1_000_000),
policy: ResourceControlPolicy::devnet(),
testing_prng_seed: Some(37),
namespace: linera_views::test_utils::generate_test_namespace(),
namespace: linera_views::random::generate_test_namespace(),
num_initial_validators: 4,
num_shards,
storage_config_builder,
Expand All @@ -265,7 +265,7 @@ impl LineraNetConfig for LocalNetConfig {
type Net = LocalNet;

async fn instantiate(self) -> Result<(Self::Net, ClientWrapper)> {
let server_config = self.storage_config_builder.build(self.database).await;
let server_config = self.storage_config_builder.build(self.database).await?;
let mut net = LocalNet::new(
self.network,
self.testing_prng_seed,
Expand Down
Loading
Loading