Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test):#1675 use fns for creating test server #3072

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(test):use fns for creating std server
This is a part of #1675 issue. It slightly reduces the number of
duplicated code by using one common function for init a logger and
creating a std server.
  • Loading branch information
alexs-sh committed Dec 4, 2022
commit 5fd7d75ec564dc07f67718f91fe8151123c2a1fe
58 changes: 29 additions & 29 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,18 +1351,29 @@ mod conn {

use support::{TokioExecutor, TokioTimer};

async fn setup_test_server() -> (TkTcpListener, SocketAddr) {
fn setup_logger() {
let _ = pretty_env_logger::try_init();
}

async fn setup_tk_test_server() -> (TkTcpListener, SocketAddr) {
setup_logger();
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
(listener, addr)
}

fn setup_std_test_server() -> (TcpListener, SocketAddr) {
setup_logger();
let listener = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0))).unwrap();
let addr = listener.local_addr().unwrap();
(listener, addr)
}

#[tokio::test]
async fn get() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

let server = async move {
let mut sock = listener.accept().await.unwrap().0;
Expand Down Expand Up @@ -1402,7 +1413,7 @@ mod conn {

#[tokio::test]
async fn get_custom_reason_phrase() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

let server = async move {
let mut sock = listener.accept().await.unwrap().0;
Expand Down Expand Up @@ -1454,8 +1465,7 @@ mod conn {

#[test]
fn incoming_content_length() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1502,8 +1512,7 @@ mod conn {
#[test]
fn aborted_body_isnt_completed() {
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx, rx) = oneshot::channel();
Expand Down Expand Up @@ -1558,8 +1567,7 @@ mod conn {

#[test]
fn uri_absolute_form() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1604,8 +1612,7 @@ mod conn {

#[test]
fn http1_conn_coerces_http2_request() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1650,8 +1657,7 @@ mod conn {

#[test]
fn pipeline() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1703,10 +1709,7 @@ mod conn {

#[test]
fn upgrade() {
let _ = ::pretty_env_logger::try_init();

let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1790,10 +1793,7 @@ mod conn {

#[test]
fn connect_method() {
let _ = ::pretty_env_logger::try_init();

let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let (server, addr) = setup_std_test_server();
let rt = support::runtime();

let (tx1, rx1) = oneshot::channel();
Expand Down Expand Up @@ -1884,7 +1884,7 @@ mod conn {
async fn http2_detect_conn_eof() {
use futures_util::future;

let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

let (shdn_tx, mut shdn_rx) = tokio::sync::watch::channel(false);
tokio::task::spawn(async move {
Expand Down Expand Up @@ -1963,7 +1963,7 @@ mod conn {

#[tokio::test]
async fn http2_keep_alive_detects_unresponsive_server() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// spawn a server that reads but doesn't write
tokio::spawn(async move {
Expand Down Expand Up @@ -1999,7 +1999,7 @@ mod conn {
// will use the default behavior which will NOT detect the server
// is unresponsive while no streams are active.

let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// spawn a server that reads but doesn't write
tokio::spawn(async move {
Expand Down Expand Up @@ -2031,7 +2031,7 @@ mod conn {

#[tokio::test]
async fn http2_keep_alive_closes_open_streams() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// spawn a server that reads but doesn't write
tokio::spawn(async move {
Expand Down Expand Up @@ -2077,7 +2077,7 @@ mod conn {
// alive is enabled
use hyper::service::service_fn;

let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// Spawn an HTTP2 server that reads the whole body and responds
tokio::spawn(async move {
Expand Down Expand Up @@ -2129,7 +2129,7 @@ mod conn {

#[tokio::test]
async fn h2_connect() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// Spawn an HTTP2 server that asks for bread and responds with baguette.
tokio::spawn(async move {
Expand Down Expand Up @@ -2185,7 +2185,7 @@ mod conn {

#[tokio::test]
async fn h2_connect_rejected() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;
let (done_tx, done_rx) = oneshot::channel();

tokio::spawn(async move {
Expand Down Expand Up @@ -2236,7 +2236,7 @@ mod conn {

#[tokio::test]
async fn test_body_panics() {
let (listener, addr) = setup_test_server().await;
let (listener, addr) = setup_tk_test_server().await;

// spawn a server that reads but doesn't write
tokio::spawn(async move {
Expand Down