Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 8a0a38b

Browse files
committed
Fix test failures of examples
1 parent d1beefd commit 8a0a38b

9 files changed

+39
-21
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tokio = { version = "0.2.6", features = ["io-util", "io-driver", "stream"] }
2020
tokio-util = { version = "0.2.0", features = ["codec"] }
2121

2222
[dev-dependencies]
23+
futures = "0.3.8"
2324
tokio = { version = "0.2", features = ["full", "io-util"] }
2425
actix = "0.9.0"
2526
actix-rt = "1.0.0"

examples/stdin.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ async fn main() {
1515

1616
let mut framed = FramedRead::new(file, LinesCodec::new());
1717

18+
println!("Type something and hit enter!");
1819
while let Some(got) = framed.next().await {
19-
println!("Got this: {:?}", got);
20+
println!("Got: {:?}", got);
2021
}
21-
22-
println!("Received None, lol");
2322
}

examples/stdin_actix.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ async fn main() {
1313

1414
let mut framed = FramedRead::new(file, LinesCodec::new());
1515

16+
println!("Type something and hit enter!");
1617
while let Some(got) = framed.next().await {
17-
println!("Got this: {:?}", got);
18+
println!("Got: {:?}", got);
1819
}
19-
20-
println!("Received None, lol");
2120
}

examples/stdin_actix_web.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
extern crate futures;
12
extern crate tokio;
23
extern crate tokio_file_unix;
34

5+
use futures::{pin_mut, select};
6+
use futures::future::FutureExt;
47
use tokio_util::codec::FramedRead;
58
use tokio_util::codec::LinesCodec;
69
use crate::tokio::stream::StreamExt;
@@ -14,7 +17,8 @@ async fn index(info: web::Path<String>) -> impl Responder {
1417

1518
#[actix_rt::main]
1619
async fn main() -> std::io::Result<()> {
17-
actix_rt::spawn(async {
20+
println!("Type something and hit enter!");
21+
let stdin_fut = async {
1822
let file = tokio_file_unix::raw_stdin().unwrap();
1923
let file = tokio_file_unix::File::new_nb(file).unwrap();
2024
let file = file.into_io().unwrap();
@@ -35,10 +39,18 @@ async fn main() -> std::io::Result<()> {
3539

3640
println!("Got bytes: {:?}", String::from_utf8(body.to_vec()).unwrap());
3741
}
38-
});
42+
Ok(())
43+
}.fuse();
3944

40-
HttpServer::new(|| App::new().service(index))
45+
let server_fut = HttpServer::new(|| App::new().service(index))
4146
.bind("127.0.0.1:8080")?
42-
.start()
43-
.await
44-
}
47+
.run()
48+
.fuse();
49+
50+
pin_mut!(stdin_fut, server_fut);
51+
52+
select! {
53+
result = stdin_fut => result,
54+
result = server_fut => result,
55+
}
56+
}

tests/run

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env bash
22
set -eux
3-
for example in stdin stdin_stream; do
3+
4+
for example in stdin stdin_actix; do
45
cat tests/test_input.txt | cargo run --example "${example}" | diff -u tests/test_output.txt -
56
cargo run --example "${example}" </dev/null | tail -n +2 | diff -u /dev/null -
67
done
7-
cat tests/test_input.txt | cargo run --example stdin_lines | diff -u tests/test_lines_output.txt -
8-
cargo run --example stdin_lines </dev/null | tail -n +2 | diff -u /dev/null -
8+
cat tests/test_actix_web_input.txt | cargo run --example stdin_actix_web | diff -u tests/test_actix_web_output.txt -
9+
cargo run --example stdin_actix_web </dev/null | tail -n +2 | diff -u /dev/null -
910

1011
cargo run --example seek
1112
diff -q tests/seek.txt <(echo aaccccAAbbbbbbbb)

tests/test_actix_web_input.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
the-quick-brown-fox
2+
jumps-over-the-lazy
3+
dog

tests/test_actix_web_output.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Type something and hit enter!
2+
Sending this: Ok("the-quick-brown-fox")
3+
Got bytes: "Hello Got this: the-quick-brown-fox"
4+
Sending this: Ok("jumps-over-the-lazy")
5+
Got bytes: "Hello Got this: jumps-over-the-lazy"
6+
Sending this: Ok("dog")
7+
Got bytes: "Hello Got this: dog"

tests/test_lines_output.txt

-4
This file was deleted.

tests/test_output.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Type something and hit enter!
2-
Got: Ok("The quick brown fox\n")
3-
Got: Ok("jumps over the lazy\n")
2+
Got: Ok("The quick brown fox")
3+
Got: Ok("jumps over the lazy")
44
Got: Ok("dog.")

0 commit comments

Comments
 (0)