Repository navigation
Expand file tree
/
Copy pathtest.rs
More file actions
64 lines (58 loc) · 1.83 KB
/
Copy pathtest.rs
File metadata and controls
64 lines (58 loc) · 1.83 KB
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
use crate::{
runtime::Runtime,
test_util::runtime,
topology::config::{SinkConfig, SinkContext},
Error,
};
use bytes05::Bytes;
use futures::{compat::Future01CompatExt, FutureExt, TryFutureExt};
use futures01::{sync::mpsc, Future, Sink};
use hyper::{
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
};
use serde::Deserialize;
pub fn load_sink<T>(config: &str) -> crate::Result<(T, SinkContext, Runtime)>
where
for<'a> T: Deserialize<'a> + SinkConfig,
{
let sink_config: T = toml::from_str(config)?;
let rt = runtime();
let cx = SinkContext::new_test();
Ok((sink_config, cx, rt))
}
pub fn build_test_server(
addr: std::net::SocketAddr,
rt: &mut Runtime,
) -> (
mpsc::Receiver<(http::request::Parts, Bytes)>,
stream_cancel::Trigger,
impl Future<Item = (), Error = ()>,
) {
let (tx, rx) = mpsc::channel(100);
let service = make_service_fn(move |_| {
let tx = tx.clone();
async {
Ok::<_, Error>(service_fn(move |req: Request<Body>| {
let tx = tx.clone();
async {
let (parts, body) = req.into_parts();
tokio::spawn(async move {
let bytes = hyper::body::to_bytes(body).await.unwrap();
tx.send((parts, bytes)).compat().await.unwrap();
});
Ok::<_, Error>(Response::new(Body::empty()))
}
}))
}
});
let (trigger, tripwire) = stream_cancel::Tripwire::new();
let server = rt.block_on_std(async move {
Server::bind(&addr)
.serve(service)
.with_graceful_shutdown(tripwire.clone().compat().map(|_| ()))
.compat()
.map_err(|e| panic!("server error: {}", e))
});
(rx, trigger, server)
}