Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

WIP: test coordinator::core::Service #350

Closed
wants to merge 7 commits into from
Closed
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
96 changes: 96 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ opentelemetry = { version = "0.2.0", optional = true }
tracing-opentelemetry = { version = "0.2.0", optional = true }
opentelemetry-jaeger = { version = "0.1.0", optional = true }

[dev-dependencies]
mockall = "0.6.0"

[[bin]]
name = "coordinator"
path = "src/bin/coordinator.rs"
Expand Down
2 changes: 0 additions & 2 deletions rust/src/aggregator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

pub mod api;
pub mod py_aggregator;
pub mod rpc;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/aggregator/py_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub struct PyAggregatorHandle {

impl Aggregator for PyAggregatorHandle {
type Error = ();
type AggregateFut = Pin<Box<dyn Future<Output = Result<Bytes, ()>>>>;
type AggregateFut = Pin<Box<dyn Future<Output = Result<Bytes, ()>> + Send>>;
type AddWeightsFut = Pin<Box<dyn Future<Output = Result<(), ()>> + Send>>;

fn add_weights(&mut self, weights: Bytes) -> Self::AddWeightsFut {
Expand Down
7 changes: 6 additions & 1 deletion rust/src/aggregator/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ mod inner {
}
}

pub use inner::{Rpc, RpcClient as Client};
pub use inner::Rpc;

#[cfg(test)]
pub use crate::tests::mocks::rpc::aggregator::Client;
#[cfg(not(test))]
pub use inner::RpcClient as Client;

/// A server that serves a single client. A new `Server` is created
/// for each new client.
Expand Down
2 changes: 2 additions & 0 deletions rust/src/coordinator/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ mod heartbeat;
mod protocol;
mod service;

#[cfg(test)]
pub(crate) use self::service::ServiceRequests;
pub use self::service::{RequestError, Selector, Service, ServiceHandle};
3 changes: 2 additions & 1 deletion rust/src/coordinator/core/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tokio::{
},
};

struct AggregationFuture(Pin<Box<dyn Future<Output = Result<(), ()>>>>);
struct AggregationFuture(Pin<Box<dyn Future<Output = Result<(), ()>> + Send>>);

impl Future for AggregationFuture {
type Output = Result<(), ()>;
Expand Down Expand Up @@ -478,6 +478,7 @@ where
}
}

#[derive(Debug)]
pub struct RequestError;

pub struct ServiceRequests(Pin<Box<dyn Stream<Item = Request> + Send>>);
Expand Down
36 changes: 35 additions & 1 deletion rust/src/coordinator/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,41 @@ mod inner {
async fn end_training(id: ClientId, success: bool);
}
}
pub use inner::{Rpc, RpcClient as Client};
pub use inner::Rpc;

#[cfg(test)]
mod mocks {
pub use super::*;

use futures::future;
use mockall::mock;
use std::io;
use tarpc::{client::Config, context::Context, rpc::Transport};

mock! {
pub NewClient {
fn spawn(self) -> io::Result<MockRpcClient>;
}
}

mock! {
pub RpcClient {
fn new<T: Transport<(), ()> + 'static>(config: Config, transport: T) -> MockNewClient;
fn end_training(&mut self, ctx: Context, id: ClientId, success: bool) -> future::Ready<io::Result<()>>;
}
}

impl Clone for MockRpcClient {
fn clone(&self) -> Self {
Self::default()
}
}
}

#[cfg(not(test))]
pub use inner::RpcClient as Client;
#[cfg(test)]
pub use mocks::MockRpcClient as Client;

impl Rpc for Server {
type EndTrainingFut = Pin<Box<dyn Future<Output = ()> + Send>>;
Expand Down
3 changes: 3 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ extern crate serde;
pub mod aggregator;
pub mod common;
pub mod coordinator;

#[cfg(test)]
mod tests;
77 changes: 77 additions & 0 deletions rust/src/tests/coordinator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{
coordinator::{core::Service, models::HeartBeatResponse, settings::FederatedLearningSettings},
tests::mocks::{
coordinator::{MaxSelector, ServiceHandle},
rpc::aggregator::{Client, MockClient},
},
};
use futures::future;
use tokio::time::{delay_for, Duration};

#[cfg(logging)]
use crate::common::{logging, settings::LoggingSettings};
#[cfg(logging)]
use tracing_subscriber::filter::EnvFilter;

#[tokio::test]
async fn test_rendez_vous_accept() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First working test :)

#[cfg(logging)]
logging::configure(LoggingSettings {
telemetry: None,
filter: EnvFilter::try_new("trace").unwrap(),
});

let rpc_client: Client = MockClient::default().into();
let aggregator_url = "http://localhost:8082".to_string();

let (service_handle, service_requests) = ServiceHandle::new();

let service = Service::new(
MaxSelector,
FederatedLearningSettings {
rounds: 1,
participants_ratio: 1.0,
min_clients: 1,
heartbeat_timeout: 10,
},
aggregator_url.clone(),
rpc_client.clone(),
service_requests,
);
let _join_handle = tokio::spawn(service);

let id = service_handle.rendez_vous_accepted().await;
let round = service_handle.heartbeat_selected(id).await;
assert_eq!(round, 0);

rpc_client
.mock()
.expect_select()
.returning(|_, _| future::ready(Ok(Ok(()))));

let (url, _token) = service_handle.start_training_accepted(id).await;
assert_eq!(url, aggregator_url);

// pretend the client trained and sent its weights to the
// aggregator. The aggregator now sends an end training requests
// to the coordinator RPC server that we fake with the
// service_handle. The service should then trigger the aggregation
// and reject subsequent heartbeats and rendez-vous
rpc_client
.mock()
.expect_aggregate()
.returning(|_| future::ready(Ok(Ok(()))));

service_handle.end_training(id, true).await;
loop {
match service_handle.heartbeat(id).await {
HeartBeatResponse::StandBy => sleep_ms(10).await,
HeartBeatResponse::Finish => break,
_ => panic!("expected StandBy or Finish"),
}
}
}

async fn sleep_ms(ms: u64) {
delay_for(Duration::from_millis(ms)).await
}
Loading