From c42c71b02d7e6cf952fce8db121e7628101a4364 Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Tue, 21 Jan 2020 23:37:45 -0800 Subject: [PATCH] Update to latest tokio --- integration_tests/async_await/Cargo.toml | 2 +- integration_tests/async_await/src/main.rs | 22 ++++------- juniper/Cargo.toml | 2 +- juniper/src/executor_tests/async_await/mod.rs | 22 ++++------- juniper_benchmarks/Cargo.toml | 9 ++--- juniper_benchmarks/benches/benchmark.rs | 37 ++++++++++++++----- juniper_benchmarks/src/lib.rs | 1 - juniper_codegen/src/util/mod.rs | 2 +- juniper_rocket_async/Cargo.toml | 2 +- 9 files changed, 49 insertions(+), 50 deletions(-) diff --git a/integration_tests/async_await/Cargo.toml b/integration_tests/async_await/Cargo.toml index e7f25512..cdc2eb67 100644 --- a/integration_tests/async_await/Cargo.toml +++ b/integration_tests/async_await/Cargo.toml @@ -10,4 +10,4 @@ async = [] [dependencies] juniper = { path = "../../juniper", features = ["async"] } futures = "=0.3.1" -tokio = "=0.2.0-alpha.6" +tokio = { version = "0.2", features = ["rt-core", "time", "macros"] } \ No newline at end of file diff --git a/integration_tests/async_await/src/main.rs b/integration_tests/async_await/src/main.rs index aee9fde3..6e8f16f7 100644 --- a/integration_tests/async_await/src/main.rs +++ b/integration_tests/async_await/src/main.rs @@ -34,8 +34,7 @@ impl User { } async fn delayed() -> bool { - let when = tokio::clock::now() + std::time::Duration::from_millis(100); - tokio::timer::delay(when).await; + tokio::time::delay_for(std::time::Duration::from_millis(100)).await; true } } @@ -61,8 +60,7 @@ impl Query { } async fn delayed() -> bool { - let when = tokio::clock::now() + std::time::Duration::from_millis(100); - tokio::timer::delay(when).await; + tokio::time::delay_for(std::time::Duration::from_millis(100)).await; true } } @@ -72,14 +70,8 @@ struct Mutation; #[juniper::graphql_object] impl Mutation {} -fn run(f: impl std::future::Future) -> O { - tokio::runtime::current_thread::Runtime::new() - .unwrap() - .block_on(f) -} - -#[test] -fn async_simple() { +#[tokio::test] +async fn async_simple() { let schema = RootNode::new(Query, Mutation); let doc = r#" query { @@ -95,9 +87,9 @@ fn async_simple() { "#; let vars = Default::default(); - let f = juniper::execute_async(doc, None, &schema, &vars, &()); - - let (res, errs) = run(f).unwrap(); + let (res, errs) = juniper::execute_async(doc, None, &schema, &vars, &()) + .await + .unwrap(); assert!(errs.is_empty()); diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index 895fa295..c5a087c2 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -48,4 +48,4 @@ uuid = { version = ">= 0.7, < 0.8", optional = true } [dev-dependencies] bencher = "0.1.2" serde_json = { version = "1.0.2" } -tokio = "=0.2.0-alpha.6" +tokio = { version = "0.2", features = ["macros", "rt-core", "time"] } diff --git a/juniper/src/executor_tests/async_await/mod.rs b/juniper/src/executor_tests/async_await/mod.rs index 68fb43ca..ef955ee5 100644 --- a/juniper/src/executor_tests/async_await/mod.rs +++ b/juniper/src/executor_tests/async_await/mod.rs @@ -35,8 +35,7 @@ impl User { } async fn delayed() -> bool { - let when = tokio::clock::now() + std::time::Duration::from_millis(100); - tokio::timer::delay(when).await; + tokio::time::delay_for(std::time::Duration::from_millis(100)).await; true } } @@ -62,8 +61,7 @@ impl Query { } async fn delayed() -> bool { - let when = tokio::clock::now() + std::time::Duration::from_millis(100); - tokio::timer::delay(when).await; + tokio::time::delay_for(std::time::Duration::from_millis(100)).await; true } } @@ -73,14 +71,8 @@ struct Mutation; #[crate::graphql_object_internal] impl Mutation {} -fn run(f: impl std::future::Future) -> O { - tokio::runtime::current_thread::Runtime::new() - .unwrap() - .block_on(f) -} - -#[test] -fn async_simple() { +#[tokio::test] +async fn async_simple() { let schema = RootNode::new(Query, Mutation); let doc = r#" query { @@ -94,9 +86,9 @@ fn async_simple() { "#; let vars = Default::default(); - let f = crate::execute_async(doc, None, &schema, &vars, &()); - - let (res, errs) = run(f).unwrap(); + let (res, errs) = crate::execute_async(doc, None, &schema, &vars, &()) + .await + .unwrap(); assert!(errs.is_empty()); diff --git a/juniper_benchmarks/Cargo.toml b/juniper_benchmarks/Cargo.toml index 566e5b4f..5fd54ef1 100644 --- a/juniper_benchmarks/Cargo.toml +++ b/juniper_benchmarks/Cargo.toml @@ -5,18 +5,17 @@ authors = ["Christoph Herzog "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [[bench]] name = "benchmark" harness = false [features] -async = ["juniper/async", "futures"] +async = [] [dependencies] -juniper = { path = "../juniper" } -futures = { version = "=0.3.1", optional = true } +juniper = { path = "../juniper", features = ["async"] } +futures = "=0.3.1" [dev-dependencies] criterion = "0.2.11" -tokio = "=0.2.0-alpha.6" +tokio = { version = "0.2.0", features = ["rt-threaded", "rt-core"] } diff --git a/juniper_benchmarks/benches/benchmark.rs b/juniper_benchmarks/benches/benchmark.rs index fb0646ac..597970c7 100644 --- a/juniper_benchmarks/benches/benchmark.rs +++ b/juniper_benchmarks/benches/benchmark.rs @@ -5,10 +5,10 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion, Parameter use juniper::{graphql_value, InputValue, ToInputValue, Value}; use juniper_benchmarks as j; -fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { - const QUERY: &'static str = r#" +fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) { + const ASYNC_QUERY: &'static str = r#" query Query($id: Int) { - user(id: $id) { + users_async_instant(ids: [$id]!) { id kind username @@ -17,8 +17,19 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { } "#; + const SYNC_QUERY: &'static str = r#" + query Query($id: Int) { + users_sync_instant(ids: [$id]!) { + id + kind + username + email + } + } +"#; + c.bench( - "Sync vs Async - Single User Flat - Instant", + "Sync vs Async - Users Flat - Instant", ParameterizedBenchmark::new( "Sync", |b, count| { @@ -28,7 +39,7 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { let ids = InputValue::list(ids); b.iter(|| { j::execute( - QUERY, + SYNC_QUERY, vec![("ids".to_string(), ids.clone())].into_iter().collect(), ) }) @@ -36,7 +47,10 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { vec![1, 10], ) .with_function("Async - Single Thread", |b, count| { - let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); + let mut rt = tokio::runtime::Builder::new() + .basic_scheduler() + .build() + .unwrap(); let ids = (0..*count) .map(|x| InputValue::scalar(x as i32)) @@ -45,14 +59,17 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { b.iter(|| { let f = j::execute_async( - QUERY, + ASYNC_QUERY, vec![("ids".to_string(), ids.clone())].into_iter().collect(), ); rt.block_on(f) }) }) .with_function("Async - Threadpool", |b, count| { - let rt = tokio::runtime::Runtime::new().unwrap(); + let mut rt = tokio::runtime::Builder::new() + .threaded_scheduler() + .build() + .unwrap(); let ids = (0..*count) .map(|x| InputValue::scalar(x as i32)) @@ -61,7 +78,7 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { b.iter(|| { let f = j::execute_async( - QUERY, + ASYNC_QUERY, vec![("ids".to_string(), ids.clone())].into_iter().collect(), ); rt.block_on(f) @@ -70,5 +87,5 @@ fn bench_sync_vs_async_single_user_flat_instant(c: &mut Criterion) { ); } -criterion_group!(benches, bench_sync_vs_async_single_user_flat_instant); +criterion_group!(benches, bench_sync_vs_async_users_flat_instant); criterion_main!(benches); diff --git a/juniper_benchmarks/src/lib.rs b/juniper_benchmarks/src/lib.rs index f7a031a5..6cdb7a51 100644 --- a/juniper_benchmarks/src/lib.rs +++ b/juniper_benchmarks/src/lib.rs @@ -105,7 +105,6 @@ pub fn execute(query: &str, vars: Variables) -> QueryResult { juniper::execute(query, None, &root, &vars, &ctx).map_err(|e| format!("{:?}", e)) } -#[cfg(feature = "async")] pub async fn execute_async(query: &str, vars: Variables) -> QueryResult { let root = new_schema(); let ctx = Context::new(); diff --git a/juniper_codegen/src/util/mod.rs b/juniper_codegen/src/util/mod.rs index ee95762a..2b817b87 100644 --- a/juniper_codegen/src/util/mod.rs +++ b/juniper_codegen/src/util/mod.rs @@ -930,7 +930,7 @@ impl GraphQLTypeDefiniton { ) -> #juniper_crate_name::BoxFuture<'b, #juniper_crate_name::ExecutionResult<#scalar>> where #scalar: Send + Sync, { - //use futures::future; + use futures::future; use #juniper_crate_name::GraphQLType; match field { #( #resolve_matches_async )* diff --git a/juniper_rocket_async/Cargo.toml b/juniper_rocket_async/Cargo.toml index eba95bf7..f58fd4da 100644 --- a/juniper_rocket_async/Cargo.toml +++ b/juniper_rocket_async/Cargo.toml @@ -22,7 +22,7 @@ juniper = { version = "0.14.1", default-features = false, path = "../juniper"} futures = { version = "=0.3.1", features = ["compat"] } rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" } -tokio = "=0.2.0-alpha.6" +tokio = "0.2" [dev-dependencies.juniper] version = "0.14.1"