Skip to content

Commit 8a52db2

Browse files
committed
Tests for cancellation.
1 parent 38c56a0 commit 8a52db2

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using GraphQL.Types;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Should;
9+
10+
namespace GraphQL.Tests.Execution.Cancellation
11+
{
12+
public class CancellationSchema : Schema
13+
{
14+
public CancellationSchema()
15+
{
16+
Query = new CancellationTestType();
17+
}
18+
}
19+
20+
public class CancellationTestType : ObjectGraphType
21+
{
22+
public CancellationTestType()
23+
{
24+
Name = "CancellationTestType";
25+
26+
Field<StringGraphType>("one", resolve: GetOne);
27+
Field<StringGraphType>("two", resolve: GetTwo);
28+
}
29+
30+
public Task<string> GetOne(ResolveFieldContext context)
31+
{
32+
if (!context.CancellationToken.CanBeCanceled)
33+
{
34+
throw new Exception("Should have token!");
35+
}
36+
37+
return Task.FromResult("one");
38+
}
39+
40+
public Task<string> GetTwo(ResolveFieldContext context)
41+
{
42+
context.CancellationToken.ThrowIfCancellationRequested();
43+
44+
return Task.FromResult("two");
45+
}
46+
}
47+
48+
public class CancellationTests : QueryTestBase<CancellationSchema>
49+
{
50+
[Test]
51+
public void cancellation_token_in_context()
52+
{
53+
using (var tokenSource = new CancellationTokenSource())
54+
{
55+
AssertQuerySuccess("{one}", "{one: 'one'}", cancellationToken: tokenSource.Token);
56+
}
57+
}
58+
59+
[Test]
60+
public void cancellation_is_propagated()
61+
{
62+
using (var tokenSource = new CancellationTokenSource())
63+
{
64+
try
65+
{
66+
tokenSource.Cancel();
67+
AssertQuerySuccess("{two}", "{two: 'two'}", cancellationToken: tokenSource.Token);
68+
}
69+
catch(AggregateException aggExc)
70+
{
71+
aggExc.InnerException.ShouldBeType<TaskCanceledException>();
72+
return;
73+
}
74+
}
75+
76+
throw new Exception("Cancellation did not propagate!");
77+
}
78+
}
79+
}

src/GraphQL.Tests/GraphQL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="Execution\Cancellation\CancellationTests.cs" />
5556
<Compile Include="Execution\Directives\DirectivesTests.cs" />
5657
<Compile Include="StarWars\EpisodeEnum.cs" />
5758
<Compile Include="StarWars\StarWarsIntrospectionTests.cs" />

src/GraphQL.Tests/QueryTestBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Newtonsoft.Json;
77
using Newtonsoft.Json.Linq;
88
using Should;
9+
using System.Threading.Tasks;
10+
using System.Threading;
911

1012
namespace GraphQL.Tests
1113
{
@@ -31,15 +33,15 @@ public QueryTestBase()
3133

3234
public IDocumentWriter Writer { get; private set; }
3335

34-
public void AssertQuerySuccess(string query, string expected, Inputs inputs = null, object root = null)
36+
public void AssertQuerySuccess(string query, string expected, Inputs inputs = null, object root = null, CancellationToken cancellationToken = default(CancellationToken))
3537
{
3638
var queryResult = CreateQueryResult(expected);
37-
AssertQuery(query, queryResult, inputs, root);
39+
AssertQuery(query, queryResult, inputs, root, cancellationToken);
3840
}
3941

40-
public void AssertQuery(string query, ExecutionResult executionResult, Inputs inputs, object root)
42+
public void AssertQuery(string query, ExecutionResult executionResult, Inputs inputs, object root, CancellationToken cancellationToken = default(CancellationToken))
4143
{
42-
var runResult = Executer.ExecuteAsync(Schema, root, query, null, inputs).Result;
44+
var runResult = Executer.ExecuteAsync(Schema, root, query, null, inputs, cancellationToken).Result;
4345

4446
var writtenResult = Writer.Write(runResult);
4547
var expectedResult = Writer.Write(executionResult);

0 commit comments

Comments
 (0)