Merge pull request #37 from vkill/async-graphql-tide3

async-graphql-tide: replace async-std to smol for tests
This commit is contained in:
Sunli 2020-04-29 09:06:58 +08:00 committed by GitHub
commit 03654978b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 29 deletions

View File

@ -18,5 +18,6 @@ tide = "0.8"
[dev-dependencies] [dev-dependencies]
async-std = "1.5.0" async-std = "1.5.0"
surf = "2.0.0-alpha.1"
serde_json = "1.0.51" serde_json = "1.0.51"
smol = { version = "0.1", features = ["tokio02"] }
reqwest = "0.10.4"

View File

@ -1,7 +1,6 @@
mod test_utils; mod test_utils;
use async_std::prelude::*;
use async_std::task;
use serde_json::json; use serde_json::json;
use smol::{Task, Timer};
use std::time::Duration; use std::time::Duration;
use tide::Request; use tide::Request;
@ -11,10 +10,10 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>
#[test] #[test]
fn quickstart() -> Result<()> { fn quickstart() -> Result<()> {
task::block_on(async { smol::run(async {
let listen_addr = test_utils::find_listen_addr().await; let listen_addr = test_utils::find_listen_addr().await;
let server: task::JoinHandle<Result<()>> = task::spawn(async move { let server = Task::<Result<()>>::spawn(async move {
struct QueryRoot; struct QueryRoot;
#[Object] #[Object]
impl QueryRoot { impl QueryRoot {
@ -34,21 +33,27 @@ fn quickstart() -> Result<()> {
Ok(()) Ok(())
}); });
let client: task::JoinHandle<Result<()>> = task::spawn(async move { let client = Task::<Result<()>>::spawn(async move {
task::sleep(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let string = surf::post(format!("http://{}", listen_addr)) let resp = reqwest::Client::new()
.body_bytes(r#"{"query":"{ add(a: 10, b: 20) }"}"#) .post(format!("http://{}", listen_addr).as_str())
.set_header("Content-Type".parse().unwrap(), "application/json") .body(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
.recv_string() .header(reqwest::header::CONTENT_TYPE, "application/json")
.send()
.await?; .await?;
assert_eq!(resp.status(), reqwest::StatusCode::OK);
let string = resp.text().await?;
println!("{}", string);
assert_eq!(string, json!({"data": {"add": 30}}).to_string()); assert_eq!(string, json!({"data": {"add": 30}}).to_string());
Ok(()) Ok(())
}); });
server.race(client).await?; client.await?;
server.cancel().await;
Ok(()) Ok(())
}) })
@ -56,10 +61,10 @@ fn quickstart() -> Result<()> {
#[test] #[test]
fn hello() -> Result<()> { fn hello() -> Result<()> {
task::block_on(async { smol::run(async {
let listen_addr = test_utils::find_listen_addr().await; let listen_addr = test_utils::find_listen_addr().await;
let server: task::JoinHandle<Result<()>> = task::spawn(async move { let server = Task::<Result<()>>::spawn(async move {
struct Hello(String); struct Hello(String);
struct QueryRoot; struct QueryRoot;
#[Object] #[Object]
@ -98,24 +103,34 @@ fn hello() -> Result<()> {
Ok(()) Ok(())
}); });
let client: task::JoinHandle<Result<()>> = task::spawn(async move { let client = Task::<Result<()>>::spawn(async move {
task::sleep(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let string = surf::post(format!("http://{}", listen_addr)) let resp = reqwest::Client::new()
.body_bytes(r#"{"query":"{ hello }"}"#) .post(format!("http://{}", listen_addr).as_str())
.set_header("Content-Type".parse().unwrap(), "application/json") .body(r#"{"query":"{ hello }"}"#)
.set_header("Name".parse().unwrap(), "Foo") .header(reqwest::header::CONTENT_TYPE, "application/json")
.recv_string() .header("Name", "Foo")
.send()
.await?; .await?;
assert_eq!(resp.status(), reqwest::StatusCode::OK);
let string = resp.text().await?;
println!("{}", string);
assert_eq!(string, json!({"data":{"hello":"Hello, Foo!"}}).to_string()); assert_eq!(string, json!({"data":{"hello":"Hello, Foo!"}}).to_string());
let string = surf::post(format!("http://{}", listen_addr)) let resp = reqwest::Client::new()
.body_bytes(r#"{"query":"{ hello }"}"#) .post(format!("http://{}", listen_addr).as_str())
.set_header("Content-Type".parse().unwrap(), "application/json") .body(r#"{"query":"{ hello }"}"#)
.recv_string() .header(reqwest::header::CONTENT_TYPE, "application/json")
.send()
.await?; .await?;
assert_eq!(resp.status(), reqwest::StatusCode::OK);
let string = resp.text().await?;
println!("{}", string);
assert_eq!( assert_eq!(
string, string,
json!({"data":{"hello":"Hello, world!"}}).to_string() json!({"data":{"hello":"Hello, world!"}}).to_string()
@ -124,7 +139,8 @@ fn hello() -> Result<()> {
Ok(()) Ok(())
}); });
server.race(client).await?; client.await?;
server.cancel().await;
Ok(()) Ok(())
}) })

View File

@ -1,6 +1,5 @@
pub async fn find_listen_addr() -> async_std::net::SocketAddr { pub async fn find_listen_addr() -> std::net::SocketAddr {
async_std::net::TcpListener::bind("localhost:0") std::net::TcpListener::bind("localhost:0")
.await
.unwrap() .unwrap()
.local_addr() .local_addr()
.unwrap() .unwrap()