use std::{thread, time}; use swctx::mkpair; #[test] fn say_hello_before_wait() { let (sctx, wctx) = mkpair::<&str, (), &str>(); let jh = thread::spawn(move || { sctx.set("hello").unwrap(); }); // join ensures that SetCtx::set() has been called jh.join().unwrap(); assert_eq!(wctx.wait().unwrap(), "hello"); } #[test] fn say_hello_after_wait() { let (sctx, wctx) = mkpair::<&str, (), &str>(); let jh = thread::spawn(move || { // yolo assume 500ms is sufficient thread::sleep(time::Duration::from_millis(500)); sctx.set("hello").unwrap(); }); assert_eq!(wctx.wait().unwrap(), "hello"); jh.join().unwrap(); } #[test] fn async_say_hello_before_wait() { let tokrt = tokio::runtime::Runtime::new().unwrap(); let (sctx, wctx) = mkpair::<&str, (), &str>(); let jh = thread::spawn(move || { sctx.set("hello").unwrap(); }); // join ensures that SetCtx::set() has been called jh.join().unwrap(); tokrt.block_on(async { assert_eq!(wctx.wait_async().await.unwrap(), "hello"); }); } #[test] fn async_say_hello_after_wait() { let tokrt = tokio::runtime::Runtime::new().unwrap(); let (sctx, wctx) = mkpair::<&str, (), &str>(); let jh = thread::spawn(move || { // yolo assume 500ms is sufficient thread::sleep(time::Duration::from_millis(500)); sctx.set("hello").unwrap(); }); tokrt.block_on(async { assert_eq!(wctx.wait_async().await.unwrap(), "hello"); }); jh.join().unwrap(); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :