use super::counter::Counter;
use crate::{new_stream, StreamResult};
use macro_rules_attribute::apply;
use smol::LocalExecutor;
use smol_macros::test;
#[apply(test!)]
async fn forever_stream_dropped(_ex: &LocalExecutor<'_>) {
let drop_counter = Counter::default();
let cloned_drop_counter = drop_counter.clone();
{
let mut stream: crate::Stream<(), u64, ()> = new_stream(move |context| {
let drop_detector = DropDetector::new(cloned_drop_counter);
let mut value = 0u64;
loop {
context.emit(value);
value += 1;
if false {
break;
}
}
drop_detector.say_hello();
()
});
for i in 0..100 {
assert_eq!(stream.next(()), StreamResult::Next(i));
}
assert_eq!(drop_counter.count(), 0);
}
assert_eq!(drop_counter.count(), 1);
}
struct DropDetector {
drop_counter: Counter,
}
impl DropDetector {
fn new(drop_counter: Counter) -> Self {
Self { drop_counter }
}
fn say_hello(&self) {
println!("Hello from DropDetector");
}
}
impl Drop for DropDetector {
fn drop(&mut self) {
self.drop_counter.increment();
}
}