-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TestLoop] (1/n) Initial code for TestLoop test framework. (#8481)
* [TestLoop] (1/n) Initial code for TestLoop test framework. * Revise. * Revise more.
- Loading branch information
1 parent
195dd39
commit 83aeb4a
Showing
12 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "near-async" | ||
version = "0.0.0" | ||
authors.workspace = true | ||
publish = true | ||
rust-version.workspace = true | ||
edition.workspace = true | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/near/nearcore" | ||
description = "This crate contains the async helpers specific for nearcore" | ||
|
||
[dependencies] | ||
actix.workspace = true | ||
derive-enum-from-into.workspace = true | ||
derive_more.workspace = true | ||
once_cell.workspace = true | ||
serde.workspace = true | ||
serde_json.workspace = true | ||
|
||
near-o11y = { path = "../o11y" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../licenses/LICENSE-APACHE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../licenses/LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Core Async Helpers | ||
|
||
This crate contains helpers related to common asynchronous programming patterns | ||
used in nearcore: | ||
|
||
* messaging: common interfaces for sending messages between components. | ||
* test_loop: a event-loop-based test framework that can test multiple components | ||
together in a synchronous way. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod sum_numbers; | ||
mod sum_numbers_test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::messaging::ArcSender; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct ReportSumMsg(pub i64); | ||
|
||
#[derive(Debug)] | ||
pub enum SumRequest { | ||
Number(i64), | ||
GetSum, | ||
} | ||
|
||
// Mimics a typical backing component of some actor in nearcore. Handles request | ||
// messages, and sends some other messages to another actor. The other actor is | ||
// abstracted with an ArcSender here. We'll show how to test this in | ||
// sum_numbers_test.rs. | ||
pub struct SumNumbersComponent { | ||
result_sender: ArcSender<ReportSumMsg>, | ||
numbers: Vec<i64>, | ||
} | ||
|
||
impl SumNumbersComponent { | ||
pub fn new(result_sender: ArcSender<ReportSumMsg>) -> Self { | ||
Self { result_sender, numbers: vec![] } | ||
} | ||
|
||
pub fn handle(&mut self, msg: SumRequest) { | ||
match msg { | ||
SumRequest::Number(n) => self.numbers.push(n), | ||
SumRequest::GetSum => { | ||
let sum = self.numbers.iter().sum(); | ||
self.numbers.clear(); | ||
self.result_sender.send(ReportSumMsg(sum)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
use derive_enum_from_into::{EnumFrom, EnumTryInto}; | ||
|
||
use crate::{ | ||
messaging::Sender, | ||
test_loop::{CaptureEvents, LoopEventHandler, TestLoopBuilder, TryIntoOrSelf}, | ||
}; | ||
|
||
use super::sum_numbers::{ReportSumMsg, SumNumbersComponent, SumRequest}; | ||
|
||
#[derive(derive_more::AsMut, derive_more::AsRef)] | ||
struct TestData { | ||
summer: SumNumbersComponent, | ||
sums: Vec<ReportSumMsg>, | ||
} | ||
|
||
#[derive(Debug, EnumTryInto, EnumFrom)] | ||
enum TestEvent { | ||
Request(SumRequest), | ||
Sum(ReportSumMsg), | ||
} | ||
|
||
// Handler that forwards SumRequest messages to the SumNumberComponent. | ||
// Note that typically we would have a single handler like this, and it can | ||
// be reused for any test that needs to send messages to this component. | ||
pub struct ForwardSumRequest; | ||
|
||
impl<Data: AsMut<SumNumbersComponent>, Event: TryIntoOrSelf<SumRequest>> | ||
LoopEventHandler<Data, Event> for ForwardSumRequest | ||
{ | ||
fn handle(&mut self, event: Event, data: &mut Data) -> Option<Event> { | ||
match event.try_into_or_self() { | ||
Ok(request) => { | ||
data.as_mut().handle(request); | ||
None | ||
} | ||
Err(event) => Some(event), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_simple() { | ||
let builder = TestLoopBuilder::<TestEvent>::new(); | ||
// Build the SumNumberComponents so that it sends messages back to the test loop. | ||
let data = | ||
TestData { summer: SumNumbersComponent::new(Arc::new(builder.sender())), sums: vec![] }; | ||
let sender = builder.sender(); | ||
let mut test = builder.build(data); | ||
test.register_handler(ForwardSumRequest); | ||
test.register_handler(CaptureEvents::<ReportSumMsg>::new()); | ||
|
||
sender.send(TestEvent::Request(SumRequest::Number(1))); | ||
sender.send(TestEvent::Request(SumRequest::Number(2))); | ||
sender.send(TestEvent::Request(SumRequest::GetSum)); | ||
sender.send(TestEvent::Request(SumRequest::Number(3))); | ||
sender.send(TestEvent::Request(SumRequest::Number(4))); | ||
sender.send(TestEvent::Request(SumRequest::Number(5))); | ||
sender.send(TestEvent::Request(SumRequest::GetSum)); | ||
|
||
test.run(Duration::from_millis(1)); | ||
assert_eq!(test.data.sums, vec![ReportSumMsg(3), ReportSumMsg(12)]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod messaging; | ||
pub mod test_loop; | ||
|
||
#[cfg(test)] | ||
mod examples; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::sync::Arc; | ||
|
||
/// Anything that allows sending a message of type M. | ||
pub trait Sender<M>: Send + Sync + 'static { | ||
fn send(&self, message: M); | ||
} | ||
|
||
pub type ArcSender<M> = Arc<dyn Sender<M>>; | ||
|
||
// TODO: Sender implementation for actix::Addr and other utils. |
Oops, something went wrong.