diff --git a/tests/support/src/frames.rs b/tests/support/src/frames.rs index 8890af8..ec9c9f1 100644 --- a/tests/support/src/frames.rs +++ b/tests/support/src/frames.rs @@ -3,8 +3,8 @@ use std::fmt; use bytes::{Bytes, IntoBuf}; use http::{self, HeaderMap, HttpTryFrom}; -use h2::frame::{self, Frame, StreamId}; use super::SendFrame; +use h2::frame::{self, Frame, StreamId}; pub const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0]; pub const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0]; @@ -12,7 +12,8 @@ pub const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0]; // ==== helper functions to easily construct h2 Frames ==== pub fn headers(id: T) -> Mock - where T: Into, +where + T: Into, { Mock(frame::Headers::new( id.into(), @@ -22,15 +23,17 @@ pub fn headers(id: T) -> Mock } pub fn data(id: T, buf: B) -> Mock - where T: Into, - B: Into, +where + T: Into, + B: Into, { Mock(frame::Data::new(id.into(), buf.into())) } pub fn push_promise(id: T1, promised: T2) -> Mock -where T1: Into, - T2: Into, +where + T1: Into, + T2: Into, { Mock(frame::PushPromise::new( id.into(), @@ -41,19 +44,22 @@ where T1: Into, } pub fn window_update(id: T, sz: u32) -> frame::WindowUpdate - where T: Into, +where + T: Into, { frame::WindowUpdate::new(id.into(), sz) } pub fn go_away(id: T) -> Mock - where T: Into, +where + T: Into, { Mock(frame::GoAway::new(id.into(), frame::Reason::NoError)) } pub fn reset(id: T) -> Mock - where T: Into, +where + T: Into, { Mock(frame::Reset::new(id.into(), frame::Reason::NoError)) } @@ -73,7 +79,9 @@ impl fmt::Debug for Mock { } impl From> for Frame -where T: Into { +where + T: Into, +{ fn from(src: Mock) -> Self { src.0.into() } @@ -83,30 +91,24 @@ where T: Into { impl Mock { pub fn request(self, method: M, uri: U) -> Self - where M: HttpTryInto, - U: HttpTryInto, + where + M: HttpTryInto, + U: HttpTryInto, { let method = method.try_into().unwrap(); let uri = uri.try_into().unwrap(); let (id, _, fields) = self.into_parts(); - let frame = frame::Headers::new( - id, - frame::Pseudo::request(method, uri), - fields - ); + let frame = frame::Headers::new(id, frame::Pseudo::request(method, uri), fields); Mock(frame) } pub fn response(self, status: S) -> Self - where S: HttpTryInto, + where + S: HttpTryInto, { let status = status.try_into().unwrap(); let (id, _, fields) = self.into_parts(); - let frame = frame::Headers::new( - id, - frame::Pseudo::response(status), - fields - ); + let frame = frame::Headers::new(id, frame::Pseudo::response(status), fields); Mock(frame) } @@ -161,18 +163,15 @@ impl From> for SendFrame { impl Mock { pub fn request(self, method: M, uri: U) -> Self - where M: HttpTryInto, - U: HttpTryInto, + where + M: HttpTryInto, + U: HttpTryInto, { let method = method.try_into().unwrap(); let uri = uri.try_into().unwrap(); let (id, promised, _, fields) = self.into_parts(); - let frame = frame::PushPromise::new( - id, - promised, - frame::Pseudo::request(method, uri), - fields - ); + let frame = + frame::PushPromise::new(id, promised, frame::Pseudo::request(method, uri), fields); Mock(frame) } @@ -201,15 +200,24 @@ impl From> for SendFrame { impl Mock { pub fn protocol_error(self) -> Self { - Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::ProtocolError)) + Mock(frame::GoAway::new( + self.0.last_stream_id(), + frame::Reason::ProtocolError, + )) } pub fn flow_control(self) -> Self { - Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FlowControlError)) + Mock(frame::GoAway::new( + self.0.last_stream_id(), + frame::Reason::FlowControlError, + )) } pub fn frame_size(self) -> Self { - Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FrameSizeError)) + Mock(frame::GoAway::new( + self.0.last_stream_id(), + frame::Reason::FrameSizeError, + )) } } @@ -264,8 +272,9 @@ pub trait HttpTryInto { } impl HttpTryInto for U - where T: HttpTryFrom, - T::Error: fmt::Debug, +where + T: HttpTryFrom, + T::Error: fmt::Debug, { type Error = T::Error; diff --git a/tests/support/src/future_ext.rs b/tests/support/src/future_ext.rs index 414eb98..a156c24 100644 --- a/tests/support/src/future_ext.rs +++ b/tests/support/src/future_ext.rs @@ -1,4 +1,4 @@ -use futures::{Future, Async, Poll}; +use futures::{Async, Future, Poll}; use std::fmt; @@ -6,17 +6,21 @@ use std::fmt; pub trait FutureExt: Future { /// Panic on error fn unwrap(self) -> Unwrap - where Self: Sized, - Self::Error: fmt::Debug, + where + Self: Sized, + Self::Error: fmt::Debug, { - Unwrap { inner: self } + Unwrap { + inner: self, + } } /// Panic on error, with a message. fn expect(self, msg: T) -> Expect - where Self: Sized, - Self::Error: fmt::Debug, - T: fmt::Display, + where + Self: Sized, + Self::Error: fmt::Debug, + T: fmt::Display, { Expect { inner: self, @@ -28,10 +32,11 @@ pub trait FutureExt: Future { /// /// `self` must not resolve before `other` does. fn drive(self, other: T) -> Drive - where T: Future, - T::Error: fmt::Debug, - Self: Future + Sized, - Self::Error: fmt::Debug, + where + T: Future, + T::Error: fmt::Debug, + Self: Future + Sized, + Self::Error: fmt::Debug, { Drive { driver: Some(self), @@ -40,8 +45,7 @@ pub trait FutureExt: Future { } } -impl FutureExt for T { -} +impl FutureExt for T {} // ===== Unwrap ====== @@ -51,9 +55,10 @@ pub struct Unwrap { } impl Future for Unwrap - where T: Future, - T::Item: fmt::Debug, - T::Error: fmt::Debug, +where + T: Future, + T::Item: fmt::Debug, + T::Error: fmt::Debug, { type Item = T::Item; type Error = (); @@ -73,9 +78,10 @@ pub struct Expect { } impl Future for Expect - where T: Future, - T::Item: fmt::Debug, - T::Error: fmt::Debug, +where + T: Future, + T::Item: fmt::Debug, + T::Error: fmt::Debug, { type Item = T::Item; type Error = (); @@ -96,10 +102,11 @@ pub struct Drive { } impl Future for Drive - where T: Future, - U: Future, - T::Error: fmt::Debug, - U::Error: fmt::Debug, +where + T: Future, + U: Future, + T::Error: fmt::Debug, + U::Error: fmt::Debug, { type Item = (T, U::Item); type Error = (); @@ -113,9 +120,9 @@ impl Future for Drive // Get the driver let driver = self.driver.take().unwrap(); - return Ok((driver, val).into()) - } - Ok(_) => {} + return Ok((driver, val).into()); + }, + Ok(_) => {}, Err(e) => panic!("unexpected error; {:?}", e), } @@ -128,8 +135,8 @@ impl Future for Drive looped = true; continue; } - } - Ok(Async::NotReady) => {} + }, + Ok(Async::NotReady) => {}, Err(e) => panic!("unexpected error; {:?}", e), } diff --git a/tests/support/src/lib.rs b/tests/support/src/lib.rs index 027a97a..be80b42 100644 --- a/tests/support/src/lib.rs +++ b/tests/support/src/lib.rs @@ -7,10 +7,10 @@ pub extern crate http; #[macro_use] pub extern crate tokio_io; +pub extern crate env_logger; #[macro_use] pub extern crate futures; pub extern crate mock_io; -pub extern crate env_logger; #[macro_use] mod assert; diff --git a/tests/support/src/mock.rs b/tests/support/src/mock.rs index 324baeb..60fb640 100644 --- a/tests/support/src/mock.rs +++ b/tests/support/src/mock.rs @@ -1,11 +1,11 @@ use {FutureExt, SendFrame}; -use h2::{self, SendError, RecvError}; +use h2::{self, RecvError, SendError}; use h2::frame::{self, Frame}; -use futures::{Async, Future, Stream, Poll}; -use futures::task::{self, Task}; +use futures::{Async, Future, Poll, Stream}; use futures::sync::oneshot; +use futures::task::{self, Task}; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::io::read_exact; @@ -58,7 +58,9 @@ pub fn new() -> (Mock, Handle) { }; let handle = Handle { - codec: h2::Codec::new(Pipe { inner }), + codec: h2::Codec::new(Pipe { + inner, + }), }; (mock, handle) @@ -92,36 +94,37 @@ impl Handle { } /// Read the client preface - pub fn read_preface(self) - -> Box> - { + pub fn read_preface(self) -> Box> { let buf = vec![0; PREFACE.len()]; - let ret = read_exact(self, buf) - .and_then(|(me, buf)| { - assert_eq!(buf, PREFACE); - Ok(me) - }); + let ret = read_exact(self, buf).and_then(|(me, buf)| { + assert_eq!(buf, PREFACE); + Ok(me) + }); Box::new(ret) } /// Perform the H2 handshake - pub fn assert_client_handshake(self) - -> Box> - { + pub fn assert_client_handshake( + self, + ) -> Box> { self.assert_client_handshake_with_settings(frame::Settings::default()) } /// Perform the H2 handshake - pub fn assert_client_handshake_with_settings(mut self, settings: T) - -> Box> - where T: Into, + pub fn assert_client_handshake_with_settings( + mut self, + settings: T, + ) -> Box> + where + T: Into, { let settings = settings.into(); // Send a settings frame self.send(settings.into()).unwrap(); - let ret = self.read_preface().unwrap() + let ret = self.read_preface() + .unwrap() .and_then(|me| me.into_future().unwrap()) .map(|(frame, mut me)| { match frame { @@ -133,13 +136,13 @@ impl Handle { me.send(ack.into()).unwrap(); (settings, me) - } + }, Some(frame) => { panic!("unexpected frame; frame={:?}", frame); - } + }, None => { panic!("unexpected EOF"); - } + }, } }) .then(|res| { @@ -155,8 +158,7 @@ impl Handle { (settings, me) }) - }) - ; + }); Box::new(ret) } @@ -177,8 +179,7 @@ impl io::Read for Handle { } } -impl AsyncRead for Handle { -} +impl AsyncRead for Handle {} impl io::Write for Handle { fn write(&mut self, buf: &[u8]) -> io::Result { @@ -215,7 +216,10 @@ impl Drop for Handle { impl io::Read for Mock { fn read(&mut self, buf: &mut [u8]) -> io::Result { - assert!(buf.len() > 0, "attempted read with zero length buffer... wut?"); + assert!( + buf.len() > 0, + "attempted read with zero length buffer... wut?" + ); let mut me = self.pipe.inner.lock().unwrap(); @@ -236,8 +240,7 @@ impl io::Read for Mock { } } -impl AsyncRead for Mock { -} +impl AsyncRead for Mock {} impl io::Write for Mock { fn write(&mut self, buf: &[u8]) -> io::Result { @@ -273,7 +276,10 @@ impl AsyncWrite for Mock { impl io::Read for Pipe { fn read(&mut self, buf: &mut [u8]) -> io::Result { - assert!(buf.len() > 0, "attempted read with zero length buffer... wut?"); + assert!( + buf.len() > 0, + "attempted read with zero length buffer... wut?" + ); let mut me = self.inner.lock().unwrap(); @@ -290,8 +296,7 @@ impl io::Read for Pipe { } } -impl AsyncRead for Pipe { -} +impl AsyncRead for Pipe {} impl io::Write for Pipe { fn write(&mut self, buf: &[u8]) -> io::Result { @@ -319,38 +324,43 @@ impl AsyncWrite for Pipe { } pub trait HandleFutureExt { - fn recv_settings(self) -> RecvFrame, Handle), Error=()>>> - where Self: Sized + 'static, - Self: Future, - Self::Error: fmt::Debug, + fn recv_settings(self) -> RecvFrame, Handle), Error = ()>>> + where + Self: Sized + 'static, + Self: Future, + Self::Error: fmt::Debug, { - let map = self.map(|(settings, handle)| (Some(settings.into()), handle)).unwrap(); + let map = self.map(|(settings, handle)| (Some(settings.into()), handle)) + .unwrap(); - let boxed: Box, Handle), Error=()>> = Box::new(map); + let boxed: Box, Handle), Error = ()>> = Box::new(map); RecvFrame { inner: boxed, frame: frame::Settings::default().into(), } } - fn ignore_settings(self) -> Box> - where Self: Sized + 'static, - Self: Future, - Self::Error: fmt::Debug, + fn ignore_settings(self) -> Box> + where + Self: Sized + 'static, + Self: Future, + Self::Error: fmt::Debug, { Box::new(self.map(|(_settings, handle)| handle).unwrap()) } fn recv_frame(self, frame: T) -> RecvFrame<::Future> - where Self: IntoRecvFrame + Sized, - T: Into, + where + Self: IntoRecvFrame + Sized, + T: Into, { self.into_recv_frame(frame.into()) } fn send_frame(self, frame: T) -> SendFrameFut - where Self: Sized, - T: Into, + where + Self: Sized, + T: Into, { SendFrameFut { inner: self, @@ -358,10 +368,11 @@ pub trait HandleFutureExt { } } - fn idle_ms(self, ms: usize) -> Box> - where Self: Sized + 'static, - Self: Future, - Self::Error: fmt::Debug, + fn idle_ms(self, ms: usize) -> Box> + where + Self: Sized + 'static, + Self: Future, + Self::Error: fmt::Debug, { use std::thread; use std::time::Duration; @@ -383,8 +394,9 @@ pub trait HandleFutureExt { })) } - fn close(self) -> Box> - where Self: Future + Sized + 'static, + fn close(self) -> Box> + where + Self: Future + Sized + 'static, { Box::new(self.map(drop)) } @@ -396,8 +408,9 @@ pub struct RecvFrame { } impl Future for RecvFrame - where T: Future, Handle)>, - T::Error: fmt::Debug, +where + T: Future, Handle)>, + T::Error: fmt::Debug, { type Item = Handle; type Error = (); @@ -418,8 +431,9 @@ pub struct SendFrameFut { } impl Future for SendFrameFut - where T: Future, - T::Error: fmt::Debug, +where + T: Future, + T::Error: fmt::Debug, { type Item = Handle; type Error = (); @@ -452,13 +466,14 @@ impl Future for Idle { Ok(Async::NotReady) => Ok(Async::NotReady), res => { panic!("Received unexpected frame on handle; frame={:?}", res); - } + }, } } } impl HandleFutureExt for T - where T: Future + 'static, +where + T: Future + 'static, { } @@ -479,14 +494,16 @@ impl IntoRecvFrame for Handle { } impl IntoRecvFrame for T - where T: Future + 'static, - T::Error: fmt::Debug, +where + T: Future + 'static, + T::Error: fmt::Debug, { - type Future = Box, Handle), Error=()>>; + type Future = Box, Handle), Error = ()>>; fn into_recv_frame(self, frame: Frame) -> RecvFrame { - let into_fut = Box::new(self.unwrap() - .and_then(|handle| handle.into_future().unwrap()) + let into_fut = Box::new( + self.unwrap() + .and_then(|handle| handle.into_future().unwrap()), ); RecvFrame { inner: into_fut, diff --git a/tests/support/src/prelude.rs b/tests/support/src/prelude.rs index 9b8925e..57ab475 100644 --- a/tests/support/src/prelude.rs +++ b/tests/support/src/prelude.rs @@ -3,8 +3,8 @@ pub use super::h2; pub use self::h2::*; -pub use self::h2::frame::StreamId; pub use self::h2::client::{self, Client}; +pub use self::h2::frame::StreamId; pub use self::h2::server::{self, Server}; // Re-export mock @@ -20,41 +20,18 @@ pub use super::util; pub use super::{Codec, SendFrame}; // Re-export useful crates -pub use super::{ - http, - bytes, - tokio_io, - futures, - mock_io, - env_logger, -}; +pub use super::{bytes, env_logger, futures, http, mock_io, tokio_io}; // Re-export primary future types -pub use self::futures::{ - Future, - Sink, - Stream, -}; +pub use self::futures::{Future, Sink, Stream}; // And our Future extensions pub use future_ext::{FutureExt, Unwrap}; // Re-export HTTP types -pub use self::http::{ - Request, - Response, - Method, - HeaderMap, - StatusCode, -}; +pub use self::http::{HeaderMap, Method, Request, Response, StatusCode}; -pub use self::bytes::{ - Buf, - BufMut, - Bytes, - BytesMut, - IntoBuf, -}; +pub use self::bytes::{Buf, BufMut, Bytes, BytesMut, IntoBuf}; pub use tokio_io::{AsyncRead, AsyncWrite}; @@ -63,7 +40,7 @@ pub use std::time::Duration; // ===== Everything under here shouldn't be used ===== // TODO: work on deleting this code -pub use ::futures::future::poll_fn; +pub use futures::future::poll_fn; pub trait MockH2 { fn handshake(&mut self) -> &mut Self; @@ -84,25 +61,24 @@ pub trait ClientExt { } impl ClientExt for Client - where T: AsyncRead + AsyncWrite + 'static, - B: IntoBuf + 'static, +where + T: AsyncRead + AsyncWrite + 'static, + B: IntoBuf + 'static, { fn run(&mut self, f: F) -> Result { use futures::future::{self, Future}; use futures::future::Either::*; - let res = future::poll_fn(|| self.poll()) - .select2(f).wait(); + let res = future::poll_fn(|| self.poll()).select2(f).wait(); match res { Ok(A((_, b))) => { // Connection is done... b.wait() - } + }, Ok(B((v, _))) => return Ok(v), Err(A((e, _))) => panic!("err: {:?}", e), Err(B((e, _))) => return Err(e), } } } - diff --git a/tests/support/src/util.rs b/tests/support/src/util.rs index fcfe516..58b46a4 100644 --- a/tests/support/src/util.rs +++ b/tests/support/src/util.rs @@ -1,12 +1,9 @@ use h2::client; -use futures::{Poll, Async, Future}; use bytes::Bytes; +use futures::{Async, Future, Poll}; -pub fn wait_for_capacity(stream: client::Stream, - target: usize) - -> WaitForCapacity -{ +pub fn wait_for_capacity(stream: client::Stream, target: usize) -> WaitForCapacity { WaitForCapacity { stream: Some(stream), target: target, @@ -36,7 +33,7 @@ impl Future for WaitForCapacity { println!("CAP={:?}", act); if act >= self.target { - return Ok(self.stream.take().unwrap().into()) + return Ok(self.stream.take().unwrap().into()); } Ok(Async::NotReady)