#![expect(
clippy::unwrap_used,
reason = "test scope: failed precondition = test failure"
)]
use moirai_core::channel::{mpmc, spsc};
use std::thread;
use std::time::{Duration, Instant};
fn main() {
println!("=== Moirai Blocking Channel Examples ===\n");
demonstrate_spsc_blocking();
println!();
demonstrate_mpmc_blocking();
println!();
demonstrate_channel_trait_polymorphism();
}
fn demonstrate_spsc_blocking() {
println!("1. SPSC Channel Blocking Behavior:");
let (tx, rx) = spsc::<String>(2);
tx.send("Message 1".to_string()).unwrap();
tx.send("Message 2".to_string()).unwrap();
println!(" - Channel filled with 2 messages (capacity: 2)");
let consumer = thread::spawn(move || {
println!(" - Consumer: Starting...");
thread::sleep(Duration::from_millis(100));
for i in 1..=3 {
let msg = rx.recv().unwrap();
println!(" - Consumer: Received '{}'", msg);
if i < 3 {
thread::sleep(Duration::from_millis(50));
}
}
});
println!(" - Producer: Attempting to send 3rd message (will block)...");
let start = Instant::now();
tx.send("Message 3".to_string()).unwrap();
let elapsed = start.elapsed();
println!(" - Producer: Send completed after {:?}", elapsed);
consumer.join().unwrap();
}
fn demonstrate_mpmc_blocking() {
println!("2. MPMC Channel Blocking Behavior:");
let (tx, rx) = mpmc::<i32>(3);
let producers: Vec<_> = (0..3)
.map(|id| {
let tx = tx.clone();
thread::spawn(move || {
for i in 0..2 {
let value = id * 10 + i;
println!(" - Producer {}: Sending {}", id, value);
tx.send(value).unwrap();
thread::sleep(Duration::from_millis(20));
}
})
})
.collect();
let consumers: Vec<_> = (0..2)
.map(|id| {
let rx = rx.clone();
thread::spawn(move || {
thread::sleep(Duration::from_millis(50)); for _ in 0..3 {
let value = rx.recv().unwrap();
println!(" - Consumer {}: Received {}", id, value);
thread::sleep(Duration::from_millis(30));
}
})
})
.collect();
for p in producers {
p.join().unwrap();
}
for c in consumers {
c.join().unwrap();
}
}
fn demonstrate_channel_trait_polymorphism() {
println!("3. Channel Trait Polymorphism (LSP Compliance):");
let _spsc_channel = spsc::<i32>(5);
let _mpmc_channel = mpmc::<i32>(5);
}