// Copyright 2017 Thomas de Zeeuw // // Licensed under the Apache License, Version 2.0 or the MIT license , at your option. This file may not be // used, copied, modified, or distributed except according to those terms. #[cfg(feature = "context-future")] mod tests { extern crate io_context; extern crate futures; use std::{io, fmt, mem}; use std::error::Error; use self::io_context::*; use self::futures::{stream, future, Async, AsyncSink, Poll, StartSend}; fn assert_send() {} fn assert_sync() {} fn assert_debug() {} fn assert_size(want: usize) { assert_eq!(mem::size_of::(), want); } #[test] fn assertions() { assert_send::>(); assert_sync::>(); assert_debug::>(); assert_size::>(mem::size_of::() + 8); } #[test] fn future() { use self::futures::Future; let mut ctx = Context::background(); let cancel_signal = ctx.add_cancel_signal(); let mut future = ContextFuture::new_future(ctx, future::ok(1)); cancel_signal.cancel(); assert_eq!(future.poll(), Err(DoneReason::Canceled)); let ctx = Context::background(); let mut future = ContextFuture::new_future::<_, io::Error>(ctx, future::ok(1)); assert!(future.poll().is_ok()); } #[test] fn stream() { use self::futures::Stream; let mut ctx = Context::background(); let cancel_signal = ctx.add_cancel_signal(); let stream = stream::iter(vec![Ok(1), Ok(2), Ok(3)]); let mut stream = ContextFuture::new_stream(ctx, stream); assert_eq!(stream.poll(), Ok(Async::Ready(Some(1)))); assert_eq!(stream.poll(), Ok(Async::Ready(Some(2)))); cancel_signal.cancel(); assert_eq!(stream.poll(), Err(DoneReason::Canceled)); } #[test] fn sink() { use self::futures::Sink; let mut ctx = Context::background(); let cancel_signal = ctx.add_cancel_signal(); struct Discard; impl Sink for Discard { type SinkItem = u8; type SinkError = io::Error; fn start_send(&mut self, _item: Self::SinkItem) -> StartSend { Ok(AsyncSink::Ready) } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { Ok(Async::Ready(())) } } let mut sink = ContextFuture::new_sink(ctx, Discard); assert!(sink.start_send(1).is_ok()); assert!(sink.poll_complete().is_ok()); cancel_signal.cancel(); assert_eq!(sink.poll_complete().unwrap_err().description(), Into::::into(DoneReason::Canceled).description()); } } #[cfg(feature = "context-future")] pub use::tests::*;