Move test server into its own function

This commit is contained in:
Jacob Hoffman-Andrews
2020-06-14 15:54:51 -07:00
committed by Martin Algesten
parent c1d7dbcfe9
commit 8a05241eac

View File

@@ -54,21 +54,19 @@ fn agent_cookies() {
agent.get("test://host/agent_cookies").call(); agent.get("test://host/agent_cookies").call();
} }
#[test] // Start a test server on an available port, that times out idle connections at 2 seconds.
fn connection_reuse() { // Return the port this server is listening on.
use std::io::{BufRead, BufReader, Read, Write}; fn start_idle_timeout_server() -> u16 {
use std::io::{BufRead, BufReader, Write};
use std::time::Duration; use std::time::Duration;
// Start a test server on an available port, that times out idle connections at 2 seconds.
let listener = std::net::TcpListener::bind("localhost:0").unwrap(); let listener = std::net::TcpListener::bind("localhost:0").unwrap();
let port = listener.local_addr().unwrap().port(); let port = listener.local_addr().unwrap().port();
let url = format!("http://localhost:{}", port);
thread::spawn(move || { thread::spawn(move || {
for stream in listener.incoming() { for stream in listener.incoming() {
thread::spawn(move || { thread::spawn(move || {
let stream = stream.unwrap(); let stream = stream.unwrap();
stream stream
.set_read_timeout(Some(Duration::from_millis(2000))) .set_read_timeout(Some(Duration::from_secs(2)))
.unwrap(); .unwrap();
let mut write_stream = stream.try_clone().unwrap(); let mut write_stream = stream.try_clone().unwrap();
for line in BufReader::new(stream).lines() { for line in BufReader::new(stream).lines() {
@@ -85,7 +83,16 @@ fn connection_reuse() {
}); });
} }
}); });
port
}
#[test]
fn connection_reuse() {
use std::io::Read;
use std::time::Duration;
let port = start_idle_timeout_server();
let url = format!("http://localhost:{}", port);
let agent = Agent::default().build(); let agent = Agent::default().build();
let resp = agent.get(&url).call(); let resp = agent.get(&url).call();