Skip to content

Commit

Permalink
feat(test/proxy): add try_send_to and request_to
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Apr 4, 2024
1 parent b6ab12d commit 9da83d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- coop: expose `coop::consume_budget()` to call in long computations. See documentation of the `coop` module for details.
- coop: prefer a time-based budgeting if the telemetry is enabled.
- proxy: add `Proxy::try_send_to()` and `Proxy::request_to()`.

### Fixed
- telemetry: now `elfo_message_handling_time_seconds` doesn't include the time of task switching if an actor is preempted due to elfo's budget system.
Expand Down
40 changes: 34 additions & 6 deletions elfo-test/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ impl Proxy {
})
}

pub fn try_send<M: Message>(&self, message: M) -> Result<(), TrySendError<M>> {
self.scope
.clone()
.sync_within(|| self.context.try_send(message))
}

#[track_caller]
pub fn send_to<M: Message>(
&self,
Expand All @@ -78,6 +72,24 @@ impl Proxy {
})
}

#[track_caller]
pub fn try_send<M: Message>(&self, message: M) -> Result<(), TrySendError<M>> {
self.scope
.clone()
.sync_within(|| self.context.try_send(message))
}

#[track_caller]
pub fn try_send_to<M: Message>(
&self,
recipient: Addr,
message: M,
) -> Result<(), TrySendError<M>> {
self.scope
.clone()
.sync_within(|| self.context.try_send_to(recipient, message))
}

#[track_caller]
pub fn request<R: Request>(&self, request: R) -> impl Future<Output = R::Response> + '_ {
let location = Location::caller();
Expand All @@ -90,6 +102,22 @@ impl Proxy {
})
}

#[track_caller]
pub fn request_to<R: Request>(
&self,
recipient: Addr,
request: R,
) -> impl Future<Output = R::Response> + '_ {
let location = Location::caller();
self.scope.clone().within(async move {
let name = request.name();
match self.context.request_to(recipient, request).resolve().await {
Ok(response) => response,
Err(err) => panic!("cannot send {} ({}) at {}", name, err, location),
}
})
}

pub fn respond<R: Request>(&self, token: ResponseToken<R>, response: R::Response) {
self.scope
.clone()
Expand Down

0 comments on commit 9da83d4

Please sign in to comment.