Skip to content

Commit

Permalink
Deprecate ServiceProviderExt and new_client_box test helpers
Browse files Browse the repository at this point in the history
The new_client_box method has not been fully deprecated but moved to the
Service trait. This makes this trait a bit more contrieved (with 3 ways
to connect clients) and will have to be fixed.
However, I prefer this temporary situation because moving
the method in a trait Ext was only hidding the issue.
I will address that in a following step.

Signed-off-by: Didier Wenzek <[email protected]>
  • Loading branch information
didier-wenzek committed Mar 21, 2024
1 parent 85aa79c commit e508f11
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 60 deletions.
10 changes: 7 additions & 3 deletions crates/common/batcher/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ mod tests {
use crate::config::BatchConfigBuilder;
use crate::driver::BatchDriver;
use std::time::Duration;
use tedge_actors::test_helpers::ServiceProviderExt;
use tedge_actors::Builder;
use tedge_actors::MessageSource;
use tedge_actors::NoConfig;
use tedge_actors::SimpleMessageBoxBuilder;
use tokio::time::timeout;

Expand Down Expand Up @@ -254,8 +255,11 @@ mod tests {
.message_leap_limit(0)
.build();
let batcher = Batcher::new(config);
let mut box_builder = SimpleMessageBoxBuilder::new("test", 1);
let test_box = box_builder.new_client_box();
let mut box_builder = SimpleMessageBoxBuilder::new("SUT", 1);
let mut test_box_builder = SimpleMessageBoxBuilder::new("Test", 1);
box_builder.connect_sink(NoConfig, &test_box_builder);
test_box_builder.connect_sink(NoConfig, &box_builder);
let test_box = test_box_builder.build();
let driver_box = box_builder.build();

let driver = BatchDriver::new(batcher, driver_box);
Expand Down
6 changes: 4 additions & 2 deletions crates/core/tedge_actors/src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ where
#[cfg(test)]
#[cfg(feature = "test-helpers")]
pub mod tests {
use crate::test_helpers::ServiceProviderExt;
use crate::*;
use async_trait::async_trait;
use futures::channel::mpsc;
Expand Down Expand Up @@ -77,7 +76,10 @@ pub mod tests {
#[tokio::test]
async fn running_an_actor_without_a_runtime() {
let mut box_builder = SimpleMessageBoxBuilder::new("test", 16);
let mut client_message_box = box_builder.new_client_box();
let mut client_box_builder = SimpleMessageBoxBuilder::new("client", 16);
client_box_builder.connect_sink(NoConfig, &box_builder);
client_box_builder.connect_source(NoConfig, &mut box_builder);
let mut client_message_box = client_box_builder.build();
let mut runtime_box = box_builder.get_signal_sender();
let actor_message_box = box_builder.build();
let actor = Echo {
Expand Down
25 changes: 14 additions & 11 deletions crates/core/tedge_actors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@
//! [Actor and message box builders](crate::builders) are provided to address these specificities
//! with a generic approach without exposing the internal structure of the actors.
//!
//! To test the `Calculator` example we need first to create its box using a [SimpleMessageBoxBuilder],
//! as this actor expects a [SimpleMessageBox].
//! And then, to create a test box connected to the actor message box,
//! we use the [ServiceProviderExt](crate::test_helpers::ServiceProviderExt) test helper extension
//! and the [new_client_box](crate::test_helpers::ServiceProviderExt::new_client_box) method.
//! To test the `Calculator` example we create two message boxes [SimpleMessageBox]es using [SimpleMessageBoxBuilder]s.
//! The first box will given to the actor under test,
//! the second box will be used by the test to control the actor input and to observe its output.
//! For that, the two boxes are connected so the output messages of the first are sent to the second, and vice-versa.
//!
//! ```
//! # use crate::tedge_actors::Actor;
//! # use crate::tedge_actors::Builder;
//! # use crate::tedge_actors::ChannelError;
//! # use crate::tedge_actors::MessageReceiver;
//! # use crate::tedge_actors::MessageSink;
//! # use crate::tedge_actors::NoConfig;
//! # use crate::tedge_actors::Sender;
//! # use crate::tedge_actors::SimpleMessageBox;
Expand All @@ -125,15 +125,18 @@
//! # #[tokio::main]
//! # async fn main() {
//! #
//! // Add the `new_client_box()` extension to the `SimpleMessageBoxBuilder`.
//! use tedge_actors::test_helpers::ServiceProviderExt;
//!
//! // Use a builder for the actor message box
//! // Use builders to create message boxes for the actor and test driver
//! use tedge_actors::MessageSource;
//! let mut actor_box_builder = SimpleMessageBoxBuilder::new("Actor", 10);
//! let mut test_box_builder = SimpleMessageBoxBuilder::new("Test", 10);
//!
//! // Connect the two boxes under construction, so each receives the message sent by the other
//! actor_box_builder.connect_source(NoConfig, &mut test_box_builder);
//! test_box_builder.connect_source(NoConfig, &mut actor_box_builder);
//!
//! // Create a test box ready then the actor box
//! let mut test_box = actor_box_builder.new_client_box();
//! // Build the boxes
//! let actor_box = actor_box_builder.build();
//! let mut test_box = test_box_builder.build();
//!
//! // The actor is then spawn in the background with its message box.
//! let mut actor = Calculator::new(actor_box);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tedge_actors/src/servers/message_boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ impl<Request: Message, Response: Message> Sender<Request> for RequestSender<Requ
mod tests {
use super::*;

use crate::test_helpers::ServiceProviderExt;
use crate::Builder;
use crate::ConcurrentServerActor;
use crate::DynSender;
Expand All @@ -168,6 +167,7 @@ mod tests {
use crate::RuntimeRequestSink;
use crate::Server;
use crate::ServerMessageBoxBuilder;
use crate::Service;
use crate::SimpleMessageBox;
use async_trait::async_trait;
use std::time::Duration;
Expand Down
20 changes: 19 additions & 1 deletion crates/core/tedge_actors/src/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
//! #[cfg(feature = "test-helpers")]
//! # #[tokio::main]
//! # async fn main_test() {
//! # use tedge_actors::test_helpers::ServiceProviderExt;
//! # use tedge_actors::Service;
//! #
//! // As for any actor, one needs a handle to the message box of the server.
//! // The simpler is to use a builder.
Expand Down Expand Up @@ -98,10 +98,15 @@ pub use builders::*;
pub use message_boxes::*;
use std::fmt::Debug;

use crate::Builder;
use crate::DynSender;
use crate::Message;
use crate::MessageSink;
use crate::MessageSource;
use crate::NoConfig;
use crate::Sender;
use crate::SimpleMessageBox;
use crate::SimpleMessageBoxBuilder;
use async_trait::async_trait;

/// Define how a server process a request
Expand Down Expand Up @@ -151,6 +156,10 @@ pub trait Service<Request: Message, Response: Message>:
/// The client provides a `response_sender` on which it wants to response to be sent to.
/// In exchange the client is returned a request sender on which its requests will have to be sent.
fn connect_client(&mut self, response_sender: DynSender<Response>) -> DynSender<Request>;

#[cfg(feature = "test-helpers")]
/// Create a simple message box connected to a server box under construction.
fn new_client_box(&mut self) -> SimpleMessageBox<Response, Request>;
}

impl<T, Request: Message, Response: Message> Service<Request, Response> for T
Expand All @@ -164,4 +173,13 @@ where
};
request_sender.into()
}

#[cfg(feature = "test-helpers")]
fn new_client_box(&mut self) -> SimpleMessageBox<Response, Request> {
let name = "client-box";
let capacity = 16;
let mut client_box = SimpleMessageBoxBuilder::new(name, capacity);
client_box.connect_sink(NoConfig, &self.connect_client(client_box.get_sender()));
client_box.build()
}
}
41 changes: 0 additions & 41 deletions crates/core/tedge_actors/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
//! Testing actors
use crate::Builder;
use crate::ChannelError;
use crate::CloneSender;
use crate::DynSender;
use crate::Message;
use crate::MessageReceiver;
use crate::MessageSink;
use crate::MessageSource;
use crate::NoConfig;
use crate::NoMessage;
use crate::RequestEnvelope;
use crate::RequestSender;
use crate::RuntimeRequest;
use crate::Sender;
use crate::ServerMessageBoxBuilder;
use crate::SimpleMessageBox;
use crate::SimpleMessageBoxBuilder;
use async_trait::async_trait;
Expand Down Expand Up @@ -376,42 +371,6 @@ impl<T> AsMut<T> for TimedMessageBox<T> {
}
}

pub trait ServiceProviderExt<I: Message, O: Message> {
/// Create a simple message box connected to a server box under construction.
fn new_client_box(&mut self) -> SimpleMessageBox<O, I>;
}

impl<I: Message, O: Message> ServiceProviderExt<I, O> for DynSender<RequestEnvelope<I, O>> {
fn new_client_box(&mut self) -> SimpleMessageBox<O, I> {
let name = "client-box";
let capacity = 16;
let mut client_box = SimpleMessageBoxBuilder::new(name, capacity);
let request_sender = Box::new(RequestSender {
sender: self.sender_clone(),
reply_to: client_box.get_sender(),
});
client_box.connect_sink(NoConfig, &request_sender.sender_clone());
client_box.build()
}
}

impl<I: Message, O: Message> ServiceProviderExt<I, O> for ServerMessageBoxBuilder<I, O> {
fn new_client_box(&mut self) -> SimpleMessageBox<O, I> {
self.request_sender().new_client_box()
}
}

impl<I: Message, O: Message> ServiceProviderExt<I, O> for SimpleMessageBoxBuilder<I, O> {
fn new_client_box(&mut self) -> SimpleMessageBox<O, I> {
let name = "client-box";
let capacity = 16;
let mut client_box = SimpleMessageBoxBuilder::new(name, capacity);
self.connect_sink(NoConfig, &client_box);
self.connect_source(NoConfig, &mut client_box);
client_box.build()
}
}

pub trait WithTimeout<T>
where
T: Future,
Expand Down
1 change: 0 additions & 1 deletion crates/core/tedge_actors/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::test_helpers::ServiceProviderExt;
use crate::*;
use async_trait::async_trait;
use std::time::Duration;
Expand Down

0 comments on commit e508f11

Please sign in to comment.