Skip to content

Commit 8f713a9

Browse files
committed
Add more tests for shared_state
1 parent b122931 commit 8f713a9

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

round-based/src/state_machine/delivery.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<M> crate::Stream for Incomings<M> {
2121
let scheduler = ready!(self.shared_state.can_schedule());
2222

2323
scheduler
24-
.protocol_needs_more_msgs()
24+
.protocol_needs_one_more_msg()
2525
.map(|msg| Some(Ok(msg)))
2626
}
2727
}

round-based/src/state_machine/shared_state.rs

+72-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, M> CanSchedule<&'a SharedStateRef<M>> {
151151
}
152152

153153
/// Indicated that the protocol needs more messages from other parties
154-
pub fn protocol_needs_more_msgs(self) -> Poll<crate::Incoming<M>> {
154+
pub fn protocol_needs_one_more_msg(self) -> Poll<crate::Incoming<M>> {
155155
let mut s = self.borrow_mut();
156156
match s.incoming_msg.take() {
157157
Some(msg) => Poll::Ready(msg),
@@ -173,7 +173,7 @@ impl<'a, M> CanSchedule<&'a SharedStateRef<M>> {
173173
mod test {
174174
use core::task::Poll;
175175

176-
use crate::{MessageDestination, Outgoing};
176+
use crate::{Incoming, MessageDestination, Outgoing};
177177

178178
use super::SharedStateRef;
179179

@@ -211,6 +211,75 @@ mod test {
211211
};
212212
}
213213

214+
#[test]
215+
fn recv_msg() {
216+
let shared_state = SharedStateRef::<&'static str>::new();
217+
218+
let incomings_state = shared_state.clone();
219+
let executor_state = shared_state;
220+
221+
// Request incoming msg
222+
{
223+
let Poll::Ready(scheduler) = incomings_state.can_schedule() else {
224+
panic!("can't schedule");
225+
};
226+
let Poll::Pending = scheduler.protocol_needs_one_more_msg() else {
227+
panic!("unexpected incoming msg");
228+
};
229+
}
230+
231+
// Scheduling one more task is not possible until incoming msg is received
232+
assert!(matches!(incomings_state.can_schedule(), Poll::Pending));
233+
234+
// Executor receives an incoming msg
235+
let incoming_msg = Incoming {
236+
id: 0,
237+
sender: 1,
238+
msg_type: crate::MessageType::Broadcast,
239+
msg: "hello",
240+
};
241+
executor_state.executor_received_msg(incoming_msg).unwrap();
242+
243+
// Incoming msg becomes available to the protocol
244+
{
245+
let Poll::Ready(scheduler) = incomings_state.can_schedule() else {
246+
panic!("can't schedule");
247+
};
248+
let Poll::Ready(msg) = scheduler.protocol_needs_one_more_msg() else {
249+
panic!("no incoming msg");
250+
};
251+
assert_eq!(msg, incoming_msg)
252+
}
253+
}
254+
255+
#[test]
256+
fn yielding() {
257+
let shared_state = SharedStateRef::<()>::new();
258+
259+
let runtime_state = shared_state.clone();
260+
let executor_state = shared_state;
261+
262+
// Request yielding
263+
{
264+
let Poll::Ready(scheduler) = runtime_state.can_schedule() else {
265+
panic!("can't schedule");
266+
};
267+
scheduler.protocol_yields();
268+
}
269+
270+
// Scheduling one more task is not possible until yield flag is reset
271+
assert!(matches!(runtime_state.can_schedule(), Poll::Pending));
272+
273+
// Executor reads and resets the yielded flag
274+
{
275+
let yielded = executor_state.executor_reads_and_resets_yielded_flag();
276+
assert!(yielded);
277+
}
278+
279+
// Now, work can be scheduled again...
280+
assert!(matches!(executor_state.can_schedule(), Poll::Ready(_)));
281+
}
282+
214283
#[test]
215284
fn task_cannot_be_scheduled_when_another_task_is_scheduled() {
216285
let try_obtain_lock_and_fail = |shared_state: &SharedStateRef<u32>| {
@@ -245,7 +314,7 @@ mod test {
245314
let Poll::Ready(scheduler) = shared_state.can_schedule() else {
246315
panic!("can't schedule");
247316
};
248-
let Poll::Pending = scheduler.protocol_needs_more_msgs() else {
317+
let Poll::Pending = scheduler.protocol_needs_one_more_msg() else {
249318
panic!("receiving resolved too early")
250319
};
251320

0 commit comments

Comments
 (0)