Skip to content

Commit

Permalink
Fixing/improving capacity test
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Aug 8, 2024
1 parent 6602f3e commit c8f7ba7
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions muse-channel/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,29 @@ fn capacity() {
let (in_sender, in_receiver) = bounded(1);
let (out_sender, out_receiver) = bounded(1);

std::thread::spawn(move || {
let remote_thread = std::thread::spawn(move || {
while let Some(RootedValue::Primitive(Primitive::Int(value))) = in_receiver.recv() {
out_sender
.send(RootedValue::Primitive(Primitive::Int(value + 1)))
.unwrap();
}
out_sender.send(RootedValue::NIL).unwrap();
});
// First message will process fully and fill the out channel
in_sender
.send(RootedValue::Primitive(Primitive::Int(0)))
.unwrap();
// Second message will be read from the in channel and cause the task to
// pause.
in_sender
.send(RootedValue::Primitive(Primitive::Int(1)))
.unwrap();
match in_sender.try_send(RootedValue::Primitive(Primitive::Int(2))) {
// Third message fills the in channel.
in_sender
.send(RootedValue::Primitive(Primitive::Int(2)))
.unwrap();
// Finally, try_send should report a full error.
match in_sender.try_send(RootedValue::Primitive(Primitive::Int(3))) {
Err(TrySendError::Full(_)) => {}
other => unreachable!("unexpected try_send result: {other:?}"),
}
Expand All @@ -63,7 +72,7 @@ fn capacity() {
Some(RootedValue::Primitive(Primitive::Int(1)))
);
in_sender
.send(RootedValue::Primitive(Primitive::Int(2)))
.send(RootedValue::Primitive(Primitive::Int(3)))
.unwrap();
assert_eq!(
out_receiver.recv(),
Expand All @@ -73,7 +82,11 @@ fn capacity() {
out_receiver.recv(),
Some(RootedValue::Primitive(Primitive::Int(3)))
);
for i in 3..100 {
assert_eq!(
out_receiver.recv(),
Some(RootedValue::Primitive(Primitive::Int(4)))
);
for i in 4..100 {
in_sender
.send(RootedValue::Primitive(Primitive::Int(i)))
.unwrap();
Expand All @@ -82,6 +95,11 @@ fn capacity() {
Some(RootedValue::Primitive(Primitive::Int(i + 1)))
);
}
// Disconnect the channel.
drop(in_sender);
assert_eq!(out_receiver.recv(), Some(RootedValue::NIL));
assert_eq!(out_receiver.recv(), None);
remote_thread.join().unwrap();
}

#[test]
Expand Down

0 comments on commit c8f7ba7

Please sign in to comment.