-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixture.rs
66 lines (55 loc) · 1.93 KB
/
fixture.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::io;
use cargo_fixture::FixtureClient;
use tokio::task::JoinHandle;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut fixture = FixtureClient::connect().await.unwrap();
// Spin up a simple HTTP server and share its listening port number
// with tests via an environment variables.
let (handle, port) = spawn_server().await.unwrap();
eprintln!("HTTP server running on port {port}...");
fixture
.set_env_var("HTTP_PORT", port.to_string())
.await
.unwrap();
// Tell the fixture we're ready to run tests.
// This will return when cargo test call is complete.
let success = fixture.ready().await.unwrap();
eprintln!("Tests finished, success: {success}");
// Wrap up.
eprintln!("Shutting down HTTP server...");
handle.abort();
match handle.await {
Err(err) if !err.is_cancelled() => {
panic!("Http server error: {}", err)
}
_ => {}
}
}
/// A very simple hyper-based server.
async fn spawn_server() -> io::Result<(JoinHandle<()>, u16)> {
use std::convert::Infallible;
use hyper::body;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
async fn service(_: Request<body::Incoming>) -> Result<Response<String>, Infallible> {
Ok(Response::new("OK".into()))
}
let handle = tokio::spawn(async move {
while let Ok((stream, _addr)) = listener.accept().await {
let io = TokioIo::new(stream);
tokio::task::spawn(async move {
http1::Builder::new()
.serve_connection(io, service_fn(service))
.await
.unwrap();
});
}
});
Ok((handle, port))
}