Skip to content

Instantly share code, notes, and snippets.

@gterzian
Last active April 26, 2020 07:24
Show Gist options
  • Save gterzian/fe28731a93392f9f87bf23e2b68b65c8 to your computer and use it in GitHub Desktop.
Save gterzian/fe28731a93392f9f87bf23e2b68b65c8 to your computer and use it in GitHub Desktop.
#[test]
fn first() {
/// The messages sent from the "main" component,
/// to the other component running in parallel.
enum WorkMsg {
Work(u8),
Exit,
}
/// The messages sent from the "parallel" component,
/// back to the "main component".
enum ResultMsg {
Result(u8),
Exited,
}
let (work_sender, work_receiver) = unbounded();
let (result_sender, result_receiver) = unbounded();
// Spawn another component in parallel.
let _ = thread::spawn(move || loop {
// Receive, and handle, messages,
// until told to exit.
match work_receiver.recv() {
Ok(WorkMsg::Work(num)) => {
// Perform some "work", sending back the result.
let _ = result_sender.send(ResultMsg::Result(num));
}
Ok(WorkMsg::Exit) => {
// Send a confirmation of exit.
let _ = result_sender.send(ResultMsg::Exited);
break;
}
_ => panic!("Error receiving a WorkMsg."),
}
});
// Send two pieces of "work",
// followed by a request to exit.
let _ = work_sender.send(WorkMsg::Work(0));
let _ = work_sender.send(WorkMsg::Work(1));
let _ = work_sender.send(WorkMsg::Exit);
// A counter of work performed.
let mut counter = 0;
loop {
match result_receiver.recv() {
Ok(ResultMsg::Result(num)) => {
// Assert that we're receiving results
// in the same order that the requests were sent.
assert_eq!(num, counter);
counter += 1;
}
Ok(ResultMsg::Exited) => {
// Assert that we're exiting
// after having received two work results.
assert_eq!(2, counter);
break;
}
_ => panic!("Error receiving a ResultMsg."),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment