From 049ed1a856ddc307f353e85a53297b0d45f588b1 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Mon, 24 Feb 2020 11:09:20 +0100 Subject: [PATCH 1/2] create tests to cover request cancellation ( #140) --- .../Chat/Schema/ChatQuery.cs | 9 ++++++++ .../QueryAndMutationTests/Base.cs | 21 +++++++++++++++++++ .../WebsocketTests/Base.cs | 19 ++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/GraphQL.Client.Tests.Common/Chat/Schema/ChatQuery.cs b/tests/GraphQL.Client.Tests.Common/Chat/Schema/ChatQuery.cs index 32357338..641ca262 100644 --- a/tests/GraphQL.Client.Tests.Common/Chat/Schema/ChatQuery.cs +++ b/tests/GraphQL.Client.Tests.Common/Chat/Schema/ChatQuery.cs @@ -1,5 +1,7 @@ +using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using GraphQL.Types; namespace GraphQL.Client.Tests.Common.Chat.Schema { @@ -21,6 +23,13 @@ public ChatQuery(IChat chat) { context.Errors.Add(new ExecutionError("this error contains extension fields", TestExtensions)); return null; }); + + Field() + .Name("longRunning") + .ResolveAsync(async context => { + await Task.Delay(TimeSpan.FromSeconds(5)); + return "finally returned"; + }); } } } diff --git a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs index 16e73c92..39380ea4 100644 --- a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs +++ b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs @@ -1,4 +1,8 @@ +using System; using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; using GraphQL.Client.Abstractions; using GraphQL.Client.Abstractions.Websocket; using GraphQL.Client.Http; @@ -155,5 +159,22 @@ public async void PreprocessHttpRequestMessageIsCalled() { Assert.Equal("Luke", response.Data.Human.Name); } } + + [Fact] + public void PostRequestCanBeCancelled() { + var graphQLRequest = new GraphQLRequest(@" + query Long { + longRunning + }"); + + using (var setup = WebHostHelpers.SetupTest(false, serializer)) { + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + + Func requestTask = () => setup.Client.SendQueryAsync(graphQLRequest, () => new {longRunning = string.Empty}, cts.Token); + Action timeMeasurement = () => requestTask.Should().Throw(); + + timeMeasurement.ExecutionTime().Should().BeCloseTo(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(200)); + } + } } } diff --git a/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs b/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs index 6112a00c..1e4d0903 100644 --- a/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs +++ b/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Net.WebSockets; using System.Threading; +using System.Threading.Tasks; using FluentAssertions; using GraphQL.Client.Abstractions; using GraphQL.Client.Abstractions.Websocket; @@ -38,7 +39,6 @@ public async void AssertTestingHarness() { } } - [Fact] public async void CanSendRequestViaWebsocket() { var port = NetworkHelpers.GetFreeTcpPortNumber(); @@ -51,6 +51,23 @@ public async void CanSendRequestViaWebsocket() { } } + [Fact] + public void PostRequestCanBeCancelled() { + var graphQLRequest = new GraphQLRequest(@" + query Long { + longRunning + }"); + + using (var setup = WebHostHelpers.SetupTest(true, Serializer)) { + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + + Func requestTask = () => setup.Client.SendQueryAsync(graphQLRequest, () => new { longRunning = string.Empty }, cts.Token); + Action timeMeasurement = () => requestTask.Should().Throw(); + + timeMeasurement.ExecutionTime().Should().BeCloseTo(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(200)); + } + } + [Fact] public async void CanHandleRequestErrorViaWebsocket() { var port = NetworkHelpers.GetFreeTcpPortNumber(); From af1024720889f293c8a45c32b35e3c6aaea81555 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Mon, 24 Feb 2020 11:28:52 +0100 Subject: [PATCH 2/2] speed up cancellation tests --- tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs | 2 +- tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs index 39380ea4..18a0e43f 100644 --- a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs +++ b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs @@ -168,7 +168,7 @@ query Long { }"); using (var setup = WebHostHelpers.SetupTest(false, serializer)) { - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)); Func requestTask = () => setup.Client.SendQueryAsync(graphQLRequest, () => new {longRunning = string.Empty}, cts.Token); Action timeMeasurement = () => requestTask.Should().Throw(); diff --git a/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs b/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs index 1e4d0903..b3aaf95f 100644 --- a/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs +++ b/tests/GraphQL.Integration.Tests/WebsocketTests/Base.cs @@ -52,14 +52,14 @@ public async void CanSendRequestViaWebsocket() { } [Fact] - public void PostRequestCanBeCancelled() { + public void WebsocketRequestCanBeCancelled() { var graphQLRequest = new GraphQLRequest(@" query Long { longRunning }"); using (var setup = WebHostHelpers.SetupTest(true, Serializer)) { - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)); Func requestTask = () => setup.Client.SendQueryAsync(graphQLRequest, () => new { longRunning = string.Empty }, cts.Token); Action timeMeasurement = () => requestTask.Should().Throw();