forked from rust-lang/flate2-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
extern crate flate2; | ||
extern crate tokio_io; | ||
extern crate futures; | ||
|
||
use flate2::read::GzDecoder; | ||
use std::cmp; | ||
use std::fs::File; | ||
use std::io::{self, Read}; | ||
use tokio_io::AsyncRead; | ||
use tokio_io::io::read_to_end; | ||
use futures::prelude::*; | ||
use futures::task; | ||
|
||
|
||
struct BadReader<T> { | ||
reader: T, | ||
x: bool | ||
} | ||
|
||
impl<T> BadReader<T> { | ||
fn new(reader: T) -> BadReader<T> { | ||
BadReader { reader, x: true } | ||
} | ||
} | ||
|
||
impl<T: Read> Read for BadReader<T> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
if self.x { | ||
self.x = false; | ||
let len = cmp::min(buf.len(), 1); | ||
self.reader.read(&mut buf[..len]) | ||
} else { | ||
self.x = true; | ||
Err(io::ErrorKind::WouldBlock.into()) | ||
} | ||
} | ||
} | ||
|
||
struct AssertAsync<T>(T); | ||
|
||
impl<T: Read> Read for AssertAsync<T> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
self.0.read(buf) | ||
} | ||
} | ||
|
||
impl<T: Read> AsyncRead for AssertAsync<T> {} | ||
|
||
struct AlwaysNotify<T>(T); | ||
|
||
impl<T: Future> Future for AlwaysNotify<T> { | ||
type Item = T::Item; | ||
type Error = T::Error; | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
let ret = self.0.poll(); | ||
if let Ok(Async::NotReady) = &ret { | ||
task::current().notify(); | ||
} | ||
ret | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_gz_asyncread() { | ||
let f = File::open("tests/good-file.gz").unwrap(); | ||
|
||
let fut = read_to_end(AssertAsync(GzDecoder::new(BadReader::new(f))), Vec::new()); | ||
let (_, content) = AlwaysNotify(fut).wait().unwrap(); | ||
|
||
let mut expected = Vec::new(); | ||
File::open("tests/good-file.txt") | ||
.unwrap() | ||
.read_to_end(&mut expected) | ||
.unwrap(); | ||
|
||
assert!(content == expected); | ||
} |