Skip to content

Commit

Permalink
Removed unnecessary mock/no-op implementations (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo authored Sep 23, 2024
1 parent 08eedaa commit 010b665
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 1,156 deletions.
12 changes: 10 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,19 @@ description = "Generates openapi spec from the code and saves it to the openapi

[tasks.generate-worker-service-openapi]
description = "Generates openapi spec for worker service"
script = "./target/debug/golem-worker-service --dump-openapi-yaml > ./target/golem-worker-service.yaml"
cwd = "./target/debug"
script = '''
mkdir -pv ../data
./golem-worker-service --dump-openapi-yaml > ../golem-worker-service.yaml
'''

[tasks.generate-component-service-openapi]
description = "Generates openapi spec for component service"
script = "./target/debug/golem-component-service --dump-openapi-yaml > ./target/golem-component-service.yaml"
cwd = "./target/debug"
script = '''
mkdir -pv ../data
./golem-component-service --dump-openapi-yaml > ../golem-component-service.yaml
'''

[tasks.merge-openapi]
install_crate = { crate_name = "golem-openapi-client-generator", version = "0.0.8" }
Expand Down
146 changes: 0 additions & 146 deletions golem-component-service-base/src/service/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use tracing::{error, info};
use crate::model::Component;
use crate::repo::component::ComponentRepo;
use crate::repo::RepoError;
use golem_common::model::component_metadata::ComponentMetadata;
use golem_service_base::model::{ComponentName, VersionedComponentId};
use golem_service_base::service::component_object_store::ComponentObjectStore;
use golem_service_base::stream::ByteStream;
Expand Down Expand Up @@ -599,151 +598,6 @@ impl ComponentServiceDefault {
}
}

#[derive(Default)]
pub struct ComponentServiceNoop {}

#[async_trait]
impl<Namespace: Display + Eq + Clone + Send + Sync> ComponentService<Namespace>
for ComponentServiceNoop
{
async fn create(
&self,
component_id: &ComponentId,
component_name: &ComponentName,
component_type: ComponentType,
_data: Vec<u8>,
namespace: &Namespace,
) -> Result<Component<Namespace>, ComponentError> {
let fake_component = Component {
namespace: namespace.clone(),
component_name: component_name.clone(),
component_size: 0,
metadata: ComponentMetadata {
exports: vec![],
producers: vec![],
memories: vec![],
},
versioned_component_id: VersionedComponentId {
component_id: component_id.clone(),
version: 0,
},
created_at: Utc::now(),
component_type,
};

Ok(fake_component)
}

async fn update(
&self,
component_id: &ComponentId,
_data: Vec<u8>,
_component_type: Option<ComponentType>,
namespace: &Namespace,
) -> Result<Component<Namespace>, ComponentError> {
let fake_component = Component {
namespace: namespace.clone(),
component_name: ComponentName("fake".to_string()),
component_size: 0,
metadata: ComponentMetadata {
exports: vec![],
producers: vec![],
memories: vec![],
},
versioned_component_id: VersionedComponentId {
component_id: component_id.clone(),
version: 0,
},
created_at: Utc::now(),
component_type: ComponentType::Durable,
};

Ok(fake_component)
}

async fn download(
&self,
_component_id: &ComponentId,
_version: Option<u64>,
_namespace: &Namespace,
) -> Result<Vec<u8>, ComponentError> {
Ok(vec![])
}

async fn download_stream(
&self,
_component_id: &ComponentId,
_version: Option<u64>,
_namespace: &Namespace,
) -> Result<ByteStream, ComponentError> {
Ok(ByteStream::empty())
}

async fn find_id_by_name(
&self,
_component_name: &ComponentName,
_namespace: &Namespace,
) -> Result<Option<ComponentId>, ComponentError> {
Ok(None)
}

async fn get_protected_data(
&self,
_component_id: &ComponentId,
_version: Option<u64>,
_namespace: &Namespace,
) -> Result<Option<Vec<u8>>, ComponentError> {
Ok(None)
}

async fn find_by_name(
&self,
_component_name: Option<ComponentName>,
_namespace: &Namespace,
) -> Result<Vec<Component<Namespace>>, ComponentError> {
Ok(vec![])
}

async fn get_by_version(
&self,
_component_id: &VersionedComponentId,
_namespace: &Namespace,
) -> Result<Option<Component<Namespace>>, ComponentError> {
Ok(None)
}

async fn get_latest_version(
&self,
_component_id: &ComponentId,
_namespace: &Namespace,
) -> Result<Option<Component<Namespace>>, ComponentError> {
Ok(None)
}

async fn get(
&self,
_component_id: &ComponentId,
_namespace: &Namespace,
) -> Result<Vec<Component<Namespace>>, ComponentError> {
Ok(vec![])
}

async fn get_namespace(
&self,
_component_id: &ComponentId,
) -> Result<Option<Namespace>, ComponentError> {
Ok(None)
}

async fn delete(
&self,
_component_id: &ComponentId,
_namespace: &Namespace,
) -> Result<(), ComponentError> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::repo::RepoError;
Expand Down
21 changes: 16 additions & 5 deletions golem-component-service/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use tracing::{error, info};

fn main() -> Result<(), std::io::Error> {
if std::env::args().any(|arg| arg == "--dump-openapi-yaml") {
let service = make_open_api_service(&Services::noop());
println!("{}", service.spec_yaml());
Ok(())
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(dump_openapi_yaml())
} else if let Some(config) = make_config_loader().load_or_dump_config() {
init_tracing_with_default_env_filter(&config.tracing);

Expand All @@ -52,14 +53,24 @@ fn main() -> Result<(), std::io::Error> {

tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.build()?
.block_on(async_main(&config, prometheus))
} else {
Ok(())
}
}

async fn dump_openapi_yaml() -> Result<(), std::io::Error> {
let config = ComponentServiceConfig::default();
let services = Services::new(&config).await.map_err(|e| {
error!("Services - init error: {}", e);
std::io::Error::new(std::io::ErrorKind::Other, e)
})?;
let service = make_open_api_service(&services);
println!("{}", service.spec_yaml());
Ok(())
}

async fn async_main(
config: &ComponentServiceConfig,
prometheus_registry: Registry,
Expand Down
17 changes: 1 addition & 16 deletions golem-component-service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use crate::config::ComponentServiceConfig;
use golem_component_service_base::repo::component::{
ComponentRepo, DbComponentRepo, LoggedComponentRepo,
};
use golem_component_service_base::service::component::{
ComponentService, ComponentServiceDefault, ComponentServiceNoop,
};
use golem_component_service_base::service::component::{ComponentService, ComponentServiceDefault};
use golem_service_base::auth::DefaultNamespace;

#[derive(Clone)]
Expand Down Expand Up @@ -91,17 +89,4 @@ impl Services {
compilation_service,
})
}

pub fn noop() -> Self {
let component_service: Arc<dyn ComponentService<DefaultNamespace> + Sync + Send> =
Arc::new(ComponentServiceNoop::default());

let compilation_service: Arc<dyn ComponentCompilationService + Sync + Send> =
Arc::new(ComponentCompilationServiceDisabled);

Services {
component_service,
compilation_service,
}
}
}
1 change: 0 additions & 1 deletion golem-worker-executor-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ autotests = false
path = "src/lib.rs"

[features]
mocks = []

[dependencies]
golem-api-grpc = { path = "../golem-api-grpc", version = "0.0.0" }
Expand Down
38 changes: 0 additions & 38 deletions golem-worker-executor-base/src/services/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,41 +836,3 @@ impl ComponentService for ComponentServiceLocalFileSystem {
}
}
}

#[cfg(any(feature = "mocks", test))]
pub struct ComponentServiceMock {}

#[cfg(any(feature = "mocks", test))]
impl Default for ComponentServiceMock {
fn default() -> Self {
Self::new()
}
}

#[cfg(any(feature = "mocks", test))]
impl ComponentServiceMock {
pub fn new() -> Self {
Self {}
}
}

#[cfg(any(feature = "mocks", test))]
#[async_trait]
impl ComponentService for ComponentServiceMock {
async fn get(
&self,
_engine: &Engine,
_component_id: &ComponentId,
_component_version: u64,
) -> Result<(Component, ComponentMetadata), GolemError> {
unimplemented!()
}

async fn get_metadata(
&self,
_component_id: &ComponentId,
_forced_version: Option<ComponentVersion>,
) -> Result<ComponentMetadata, GolemError> {
unimplemented!()
}
}
56 changes: 0 additions & 56 deletions golem-worker-executor-base/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,62 +290,6 @@ impl<Ctx: WorkerCtx> All<Ctx> {
}
}

#[cfg(any(feature = "mocks", test))]
pub async fn mocked(mocked_extra_deps: Ctx::ExtraDeps) -> Self {
let active_workers = Arc::new(active_workers::ActiveWorkers::new(
&crate::services::golem_config::MemoryConfig::default(),
));
let engine = Arc::new(wasmtime::Engine::default());
let linker = Arc::new(wasmtime::component::Linker::new(&engine));
let runtime = Handle::current();
let component_service = Arc::new(component::ComponentServiceMock::new());
let worker_service = Arc::new(worker::WorkerServiceMock::new());
let worker_enumeration_service =
Arc::new(worker_enumeration::WorkerEnumerationServiceMock::new());
let running_worker_enumeration_service =
Arc::new(worker_enumeration::RunningWorkerEnumerationServiceMock::new());
let promise_service = Arc::new(promise::PromiseServiceMock::new());
let golem_config = Arc::new(golem_config::GolemConfig::default());
let shard_service = Arc::new(shard::ShardServiceDefault::new());
let shard_manager_service = Arc::new(shard_manager::ShardManagerServiceSingleShard::new());
let key_value_service = Arc::new(key_value::DefaultKeyValueService::new(Arc::new(
crate::storage::keyvalue::memory::InMemoryKeyValueStorage::new(),
)));
let blob_storage = Arc::new(crate::storage::blob::memory::InMemoryBlobStorage::new());
let blob_store_service = Arc::new(blob_store::DefaultBlobStoreService::new(
blob_storage.clone(),
));
let oplog_service = Arc::new(oplog::mock::OplogServiceMock::new());
let rpc = Arc::new(rpc::RpcMock::new());
let scheduler_service = Arc::new(scheduler::SchedulerServiceMock::new());
let worker_activator = Arc::new(worker_activator::WorkerActivatorMock::new());
let worker_proxy = Arc::new(worker_proxy::WorkerProxyMock::new());
let events = Arc::new(Events::new(32768));
Self {
active_workers,
engine,
linker,
runtime,
component_service,
shard_manager_service,
worker_service,
worker_enumeration_service,
running_worker_enumeration_service,
promise_service,
golem_config,
shard_service,
key_value_service,
blob_store_service,
oplog_service,
rpc,
scheduler_service,
worker_activator,
worker_proxy,
events,
extra_deps: mocked_extra_deps,
}
}

pub fn from_other<T: HasAll<Ctx>>(this: &T) -> All<Ctx> {
All::new(
this.active_workers(),
Expand Down
Loading

0 comments on commit 010b665

Please sign in to comment.