Skip to content

Commit

Permalink
Rename Operator trait to Service
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Oct 26, 2023
1 parent ed689df commit c720d44
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions service/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ pub trait Service {
}

#[derive(Debug, Default)]

Check warning on line 33 in service/src/operator.rs

View check run for this annotation

Codecov / codecov/patch

service/src/operator.rs#L33

Added line #L33 was not covered by tests
pub struct OperatorService<O: Service> {
operator: Arc<O>,
pub struct OperatorService<S: Service> {
service: Arc<S>,
}

#[tonic::async_trait]
impl<O: Service + Sync + Send + 'static> PostServiceOperator for OperatorService<O> {
impl<S: Service + Sync + Send + 'static> PostServiceOperator for OperatorService<S> {
async fn status(
&self,
request: Request<PostServiceStatusRequest>,
) -> Result<Response<PostServiceStatusResponse>, Status> {
log::debug!("got a request from {:?}", request.remote_addr());

let status = match self.operator.status() {
let status = match self.service.status() {
ServiceState::Idle => spacemesh_v1::post_service_status_response::Status::Idle,
ServiceState::Proving => spacemesh_v1::post_service_status_response::Status::Proving,
};
Expand All @@ -58,17 +58,17 @@ impl<O: Service + Sync + Send + 'static> PostServiceOperator for OperatorService
pub struct OperatorServer {}

impl OperatorServer {
pub async fn run<O>(listener: TcpListener, operator: Arc<O>) -> eyre::Result<()>
pub async fn run<S>(listener: TcpListener, service: Arc<S>) -> eyre::Result<()>
where
O: Service + Sync + Send + 'static,
S: Service + Sync + Send + 'static,
{
log::info!("running operator service on {}", listener.local_addr()?);

Check warning on line 65 in service/src/operator.rs

View check run for this annotation

Codecov / codecov/patch

service/src/operator.rs#L65

Added line #L65 was not covered by tests

let reflection_service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(spacemesh_v1::FILE_DESCRIPTOR_SET)
.build()?;

let operator_service = PostServiceOperatorServer::new(OperatorService { operator });
let operator_service = PostServiceOperatorServer::new(OperatorService { service });

Server::builder()
.add_service(reflection_service)
Expand All @@ -91,20 +91,18 @@ mod tests {

#[tokio::test]
async fn test_status() {
let mut svc_operator = super::MockService::new();
svc_operator
.expect_status()
let mut svc = super::MockService::new();
svc.expect_status()
.once()
.returning(|| super::ServiceState::Idle);
svc_operator
.expect_status()
svc.expect_status()
.once()
.returning(|| super::ServiceState::Proving);

let listener = TcpListener::bind("localhost:0").await.unwrap();
let addr: std::net::SocketAddr = listener.local_addr().unwrap();

tokio::spawn(super::OperatorServer::run(listener, Arc::new(svc_operator)));
tokio::spawn(super::OperatorServer::run(listener, Arc::new(svc)));

let mut client = PostServiceOperatorClient::connect(format!("http://{addr}"))
.await
Expand Down

0 comments on commit c720d44

Please sign in to comment.