mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
`Lines` accumulated into a `String`, so a partial line interrupted by an I/O error could not be kept when it ended mid multi-byte character. The next poll then tripped `debug_assert!(output.is_empty())`, or underflowed `vector.len() - num_bytes_read` in `put_back_original_data`, which panics in release builds too on the `.expect` below it. `Lines` now holds a single `buf: Vec<u8>` and calls `read_until_internal` directly the way `Split` does, converting to `String` only once a whole line is available. An `InvalidData` error now carries the utf-8 error itself rather than a fixed string. `Lines` owns the line, so it hands over the whole `FromUtf8Error` and the caller can still recover the bytes; `read_line` and `read_to_string` have to put those bytes back into the caller's `String`, so they carry `Utf8Error` instead. `read_line_internal` has no callers outside `read_line.rs` and is now private.
110 lines
3.2 KiB
Rust
110 lines
3.2 KiB
Rust
#![warn(rust_2018_idioms)]
|
|
#![cfg(feature = "full")]
|
|
|
|
use std::io::ErrorKind;
|
|
use std::str::Utf8Error;
|
|
use tokio::io::{AsyncBufReadExt, BufReader, Error};
|
|
use tokio_test::{assert_ok, io::Builder};
|
|
|
|
use std::io::Cursor;
|
|
|
|
#[tokio::test]
|
|
async fn read_line() {
|
|
let mut buf = String::new();
|
|
let mut rd = Cursor::new(b"hello\nworld\n\n");
|
|
|
|
let n = assert_ok!(rd.read_line(&mut buf).await);
|
|
assert_eq!(n, 6);
|
|
assert_eq!(buf, "hello\n");
|
|
buf.clear();
|
|
let n = assert_ok!(rd.read_line(&mut buf).await);
|
|
assert_eq!(n, 6);
|
|
assert_eq!(buf, "world\n");
|
|
buf.clear();
|
|
let n = assert_ok!(rd.read_line(&mut buf).await);
|
|
assert_eq!(n, 1);
|
|
assert_eq!(buf, "\n");
|
|
buf.clear();
|
|
let n = assert_ok!(rd.read_line(&mut buf).await);
|
|
assert_eq!(n, 0);
|
|
assert_eq!(buf, "");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn read_line_not_all_ready() {
|
|
let mock = Builder::new()
|
|
.read(b"Hello Wor")
|
|
.read(b"ld\nFizzBuz")
|
|
.read(b"z\n1\n2")
|
|
.build();
|
|
|
|
let mut read = BufReader::new(mock);
|
|
|
|
let mut line = "We say ".to_string();
|
|
let bytes = read.read_line(&mut line).await.unwrap();
|
|
assert_eq!(bytes, "Hello World\n".len());
|
|
assert_eq!(line.as_str(), "We say Hello World\n");
|
|
|
|
line = "I solve ".to_string();
|
|
let bytes = read.read_line(&mut line).await.unwrap();
|
|
assert_eq!(bytes, "FizzBuzz\n".len());
|
|
assert_eq!(line.as_str(), "I solve FizzBuzz\n");
|
|
|
|
line.clear();
|
|
let bytes = read.read_line(&mut line).await.unwrap();
|
|
assert_eq!(bytes, 2);
|
|
assert_eq!(line.as_str(), "1\n");
|
|
|
|
line.clear();
|
|
let bytes = read.read_line(&mut line).await.unwrap();
|
|
assert_eq!(bytes, 1);
|
|
assert_eq!(line.as_str(), "2");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn read_line_invalid_utf8() {
|
|
let mock = Builder::new().read(b"Hello Wor\xffld.\n").build();
|
|
|
|
let mut read = BufReader::new(mock);
|
|
|
|
let mut line = "Foo".to_string();
|
|
let err = read.read_line(&mut line).await.expect_err("Should fail");
|
|
assert_eq!(err.kind(), ErrorKind::InvalidData);
|
|
let utf8 = err.into_inner().unwrap().downcast::<Utf8Error>().unwrap();
|
|
assert_eq!(utf8.valid_up_to(), 12);
|
|
assert_eq!(line.as_str(), "Foo");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn read_line_fail() {
|
|
let mock = Builder::new()
|
|
.read(b"Hello Wor")
|
|
.read_error(Error::new(ErrorKind::Other, "The world has no end"))
|
|
.build();
|
|
|
|
let mut read = BufReader::new(mock);
|
|
|
|
let mut line = "Foo".to_string();
|
|
let err = read.read_line(&mut line).await.expect_err("Should fail");
|
|
assert_eq!(err.kind(), ErrorKind::Other);
|
|
assert_eq!(err.to_string(), "The world has no end");
|
|
assert_eq!(line.as_str(), "FooHello Wor");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn read_line_fail_and_utf8_fail() {
|
|
let mock = Builder::new()
|
|
.read(b"Hello Wor")
|
|
.read(b"\xff\xff\xff")
|
|
.read_error(Error::new(ErrorKind::Other, "The world has no end"))
|
|
.build();
|
|
|
|
let mut read = BufReader::new(mock);
|
|
|
|
let mut line = "Foo".to_string();
|
|
let err = read.read_line(&mut line).await.expect_err("Should fail");
|
|
assert_eq!(err.kind(), ErrorKind::Other);
|
|
assert_eq!(err.to_string(), "The world has no end");
|
|
assert_eq!(line.as_str(), "Foo");
|
|
}
|