From c8f7ba7495fe246395d13c2572f8300293abd194 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 8 Aug 2024 14:03:09 -0700 Subject: [PATCH] Fixing/improving capacity test --- muse-channel/src/tests.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/muse-channel/src/tests.rs b/muse-channel/src/tests.rs index 62b8b3d..4cb194e 100644 --- a/muse-channel/src/tests.rs +++ b/muse-channel/src/tests.rs @@ -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:?}"), } @@ -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(), @@ -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(); @@ -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]