Skip to content

Commit

Permalink
change lazy to optional initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
irach-ramos committed Oct 4, 2024
1 parent e0849c8 commit 8e8609f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions golem-test-framework/src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,8 @@ impl TestDependencies for CliTestDependencies {

fn cassandra(
&self,
) -> Arc<dyn crate::components::cassandra::Cassandra + Send + Sync + 'static> {
panic!("Not supported")
) -> Option<Arc<dyn crate::components::cassandra::Cassandra + Send + Sync + 'static>> {
None
}
}

Expand Down
17 changes: 9 additions & 8 deletions golem-test-framework/src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
// limitations under the License.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::{Arc, RwLock};

use crate::components;
use crate::components::cassandra::docker::DockerCassandra;
use crate::components::cassandra::Cassandra;
use crate::components::component_compilation_service::docker::DockerComponentCompilationService;
use crate::components::component_compilation_service::spawned::SpawnedComponentCompilationService;
Expand All @@ -43,7 +42,6 @@ use crate::components::worker_service::docker::DockerWorkerService;
use crate::components::worker_service::spawned::SpawnedWorkerService;
use crate::components::worker_service::WorkerService;
use crate::config::{DbType, TestDependencies};
use once_cell::sync::Lazy;
use tracing::Level;

pub struct EnvBasedTestDependenciesConfig {
Expand Down Expand Up @@ -154,7 +152,7 @@ pub struct EnvBasedTestDependencies {
component_compilation_service: Arc<dyn ComponentCompilationService + Send + Sync + 'static>,
worker_service: Arc<dyn WorkerService + Send + Sync + 'static>,
worker_executor_cluster: Arc<dyn WorkerExecutorCluster + Send + Sync + 'static>,
cassandra: Lazy<Arc<dyn Cassandra + Send + Sync + 'static>>,
cassandra: RwLock<Option<Arc<dyn Cassandra + Send + Sync + 'static>>>,
}

impl EnvBasedTestDependencies {
Expand Down Expand Up @@ -412,8 +410,8 @@ impl EnvBasedTestDependencies {

fn make_cassandra(
_config: Arc<EnvBasedTestDependenciesConfig>,
) -> Lazy<Arc<dyn Cassandra + Send + Sync + 'static>> {
Lazy::new(|| Arc::new(DockerCassandra::new(false)))
) -> RwLock<Option<Arc<dyn Cassandra + Send + Sync + 'static>>> {
RwLock::new(None)
}

pub async fn new(config: EnvBasedTestDependenciesConfig) -> Self {
Expand Down Expand Up @@ -527,8 +525,11 @@ impl TestDependencies for EnvBasedTestDependencies {
self.worker_executor_cluster.clone()
}

fn cassandra(&self) -> Arc<dyn Cassandra + Send + Sync + 'static> {
self.cassandra.clone()
fn cassandra(&self) -> Option<Arc<dyn Cassandra + Send + Sync + 'static>> {
match self.cassandra.read() {
Ok(cassandra) => cassandra.as_ref().map(|c| c.clone()),
Err(poison_err) => poison_err.into_inner().as_ref().map(|c| c.clone()),
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions golem-test-framework/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait TestDependencies {
) -> Arc<dyn ComponentCompilationService + Send + Sync + 'static>;
fn worker_service(&self) -> Arc<dyn WorkerService + Send + Sync + 'static>;
fn worker_executor_cluster(&self) -> Arc<dyn WorkerExecutorCluster + Send + Sync + 'static>;
fn cassandra(&self) -> Arc<dyn Cassandra + Send + Sync + 'static>;
fn cassandra(&self) -> Option<Arc<dyn Cassandra + Send + Sync + 'static>>;

fn kill_all(&self) {
self.worker_executor_cluster().kill_all();
Expand All @@ -56,7 +56,9 @@ pub trait TestDependencies {
self.rdb().kill();
self.redis_monitor().kill();
self.redis().kill();
self.cassandra().kill();
if let Some(c) = self.cassandra() {
c.kill()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion golem-worker-executor-base/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl TestDependencies for TestWorkerExecutor {
self.deps.worker_executor_cluster()
}

fn cassandra(&self) -> Arc<dyn Cassandra + Send + Sync + 'static> {
fn cassandra(&self) -> Option<Arc<dyn Cassandra + Send + Sync + 'static>> {
self.deps.cassandra()
}
}
Expand Down
25 changes: 14 additions & 11 deletions golem-worker-executor-base/tests/key_value_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,21 @@ impl GetKeyValueStorage for CassandraKeyValueStorageWrapper {
}

pub(crate) async fn cassandra_storage() -> impl GetKeyValueStorage {
let cassandra = BASE_DEPS.cassandra();
cassandra.assert_valid();
let test_keyspace = format!("golem_test_{}", &Uuid::new_v4().to_string()[..8]);
let session = cassandra.get_session(None).await;
let cassandra_session = CassandraSession::new(session, true, &test_keyspace);
if let Err(err_msg) = cassandra_session.create_schema().await {
cassandra.kill();
panic!("Cannot create schema : {}", err_msg);
}
if let Some(cassandra) = BASE_DEPS.cassandra() {
cassandra.assert_valid();
let test_keyspace = format!("golem_test_{}", &Uuid::new_v4().to_string()[..8]);
let session = cassandra.get_session(None).await;
let cassandra_session = CassandraSession::new(session, true, &test_keyspace);
if let Err(err_msg) = cassandra_session.create_schema().await {
cassandra.kill();
panic!("Cannot create schema : {}", err_msg);
}

let kvs = CassandraKeyValueStorage::new(cassandra_session);
CassandraKeyValueStorageWrapper { kvs }
let kvs = CassandraKeyValueStorage::new(cassandra_session);
CassandraKeyValueStorageWrapper { kvs }
} else {
panic!("Cassandra is not configured");
}
}

pub fn ns() -> KeyValueStorageNamespace {
Expand Down
12 changes: 7 additions & 5 deletions golem-worker-executor-base/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl TestDependencies for WorkerExecutorPerTestDependencies {
panic!("Not supported")
}

fn cassandra(&self) -> Arc<dyn Cassandra + Send + Sync + 'static> {
self.cassandra.clone()
fn cassandra(&self) -> Option<Arc<dyn Cassandra + Send + Sync + 'static>> {
Some(self.cassandra.clone())
}
}

Expand Down Expand Up @@ -219,8 +219,8 @@ impl TestDependencies for WorkerExecutorTestDependencies {
panic!("Not supported")
}

fn cassandra(&self) -> Arc<dyn Cassandra + Send + Sync + 'static> {
self.cassandra.clone()
fn cassandra(&self) -> Option<Arc<dyn Cassandra + Send + Sync + 'static>> {
Some(self.cassandra.clone())
}
}

Expand All @@ -233,7 +233,9 @@ unsafe fn drop_base_deps() {
let base_deps_ptr = base_deps_ptr as *mut WorkerExecutorTestDependencies;
(*base_deps_ptr).redis().kill();
(*base_deps_ptr).redis_monitor().kill();
(*base_deps_ptr).cassandra().kill();
if let Some(c) = (*base_deps_ptr).cassandra() {
c.kill()
}
}

struct Tracing;
Expand Down

0 comments on commit 8e8609f

Please sign in to comment.