Skip to content

Commit 25e577f

Browse files
committed
test: add cancellation tokens to async tests
1 parent 3709638 commit 25e577f

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,37 @@ jobs:
4646

4747
- name: Test net8.0
4848
id: test-net8_0
49-
continue-on-error: true
49+
continue-on-error: false
5050
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net8.0
5151

5252
- name: Test net9.0
5353
id: test-net9_0
54-
continue-on-error: true
54+
continue-on-error: false
5555
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net9.0
5656

5757
- name: Test net10.0
5858
id: test-net10_0
59-
continue-on-error: true
59+
continue-on-error: false
6060
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net10.0
6161

6262
- name: Test net462
6363
id: test-net462
64-
continue-on-error: true
64+
continue-on-error: false
6565
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net462
6666

6767
- name: Test net472
6868
id: test-net472
69-
continue-on-error: true
69+
continue-on-error: false
7070
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net472
7171

7272
- name: Test net48
7373
id: test-net48
74-
continue-on-error: true
74+
continue-on-error: false
7575
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net48
7676

7777
- name: Test net481
7878
id: test-net481
79-
continue-on-error: true
79+
continue-on-error: false
8080
run: dotnet test src/NetCoreForce.Client.Tests --configuration $config --no-restore --no-build --verbosity normal --framework net481
8181

8282
- name: Upload nuget artifact

‎src/NetCoreForce.FunctionalTests/NetCoreForce.FunctionalTests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<!-- xUnit v3 for .NET 8+ and .NET Framework 4.7.2+ - v3 test projects must be executables. -->
99
<PropertyGroup Condition=" '$(UseXunitV3)' == 'true' ">
1010
<OutputType>Exe</OutputType>
11+
<DefineConstants>$(DefineConstants);XUNIT_V3</DefineConstants>
1112
</PropertyGroup>
1213

1314
<ItemGroup Condition=" '$(UseXunitV3)' == 'true' ">

‎src/NetCoreForce.FunctionalTests/QueryAsyncTests.cs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public async Task QueryAsync()
2626
{
2727
ForceClient client = await forceClientFixture.GetForceClient();
2828

29-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 3000");
29+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 3000", cancellationToken: TestCancellation.Token);
3030

3131
int count = 0;
3232
SfContact contact = null;
33-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
33+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
3434
{
3535
while (await contactsEnumerator.MoveNextAsync())
3636
{
@@ -50,11 +50,11 @@ public async Task QueryAsync_small_batchSize()
5050
{
5151
ForceClient client = await forceClientFixture.GetForceClient();
5252

53-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1000", batchSize: 200);
53+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1000", batchSize: 200, cancellationToken: TestCancellation.Token);
5454

5555
int count = 0;
5656
SfContact contact = null;
57-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
57+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
5858
{
5959
// Assert.NotNull(contactsEnumerator);
6060

@@ -82,11 +82,11 @@ public async Task QueryAsync_single_result()
8282
{
8383
ForceClient client = await forceClientFixture.GetForceClient();
8484

85-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1");
85+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1", cancellationToken: TestCancellation.Token);
8686

8787
int count = 0;
8888
SfContact contact = null;
89-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
89+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
9090
{
9191
while (await contactsEnumerator.MoveNextAsync())
9292
{
@@ -104,10 +104,10 @@ public async Task QueryAsync_large_resultset()
104104
{
105105
ForceClient client = await forceClientFixture.GetForceClient();
106106

107-
var enumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact");
107+
var enumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact", cancellationToken: TestCancellation.Token);
108108

109109
int count = 0;
110-
await using (var enumerator = enumerable.GetAsyncEnumerator())
110+
await using (var enumerator = enumerable.GetAsyncEnumerator(TestCancellation.Token))
111111
{
112112
while (await enumerator.MoveNextAsync())
113113
{
@@ -130,11 +130,11 @@ public async Task QueryAsync_valid_result_collection()
130130
{
131131
ForceClient client = await forceClientFixture.GetForceClient();
132132

133-
var enumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact limit 450", batchSize: 200);
133+
var enumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact limit 450", batchSize: 200, cancellationToken: TestCancellation.Token);
134134

135135
List<SfContact> results = new List<SfContact>();
136136

137-
await using (var enumerator = enumerable.GetAsyncEnumerator())
137+
await using (var enumerator = enumerable.GetAsyncEnumerator(TestCancellation.Token))
138138
{
139139
while (await enumerator.MoveNextAsync())
140140
{
@@ -151,10 +151,10 @@ public async Task QueryAsync_custom_batchSize()
151151
{
152152
ForceClient client = await forceClientFixture.GetForceClient();
153153

154-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 3000", batchSize: 200);
154+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 3000", batchSize: 200, cancellationToken: TestCancellation.Token);
155155

156156
int count = 0;
157-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
157+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
158158
{
159159
while (await contactsEnumerator.MoveNextAsync())
160160
{
@@ -171,11 +171,11 @@ public async Task QueryAsync_invalid_batchsize()
171171
{
172172
ForceClient client = await forceClientFixture.GetForceClient();
173173

174-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1000", batchSize: 100);
174+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact LIMIT 1000", batchSize: 100, cancellationToken: TestCancellation.Token);
175175

176176
await Assert.ThrowsAsync<ArgumentException>(async () =>
177177
{
178-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
178+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
179179
{
180180
await contactsEnumerator.MoveNextAsync();
181181
}
@@ -187,10 +187,10 @@ public async Task QueryAsync_no_results()
187187
{
188188
ForceClient client = await forceClientFixture.GetForceClient();
189189

190-
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact WHERE Name='xyz123foobar'");
190+
var contactsEnumerable = client.QueryAsync<SfContact>("SELECT Id FROM Contact WHERE Name='xyz123foobar'", cancellationToken: TestCancellation.Token);
191191

192192
SfContact contact = null;
193-
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator())
193+
await using (IAsyncEnumerator<SfContact> contactsEnumerator = contactsEnumerable.GetAsyncEnumerator(TestCancellation.Token))
194194
{
195195
while (await contactsEnumerator.MoveNextAsync())
196196
{

‎src/NetCoreForce.FunctionalTests/SOSLTests.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Linq;
66
using System.Threading.Tasks;
77
using Xunit;
8-
#if !(NET8_0_OR_GREATER || NET472_OR_GREATER)
8+
#if !XUNIT_V3
99
using Xunit.Abstractions;
1010
#endif
1111
using NetCoreForce.Client;
@@ -43,7 +43,7 @@ public SOSLTests(ForceClientFixture fixture, ITestOutputHelper output)
4343
}
4444

4545
//manual only, see class summary
46-
#if NET8_0_OR_GREATER || NET472_OR_GREATER
46+
#if XUNIT_V3
4747
[Fact(Explicit = true)]
4848
#else
4949
[Fact(Skip = ManualOnlyReason)]
@@ -78,7 +78,7 @@ public async Task BasicSearch()
7878
}
7979

8080
//manual only, see class summary
81-
#if NET8_0_OR_GREATER || NET472_OR_GREATER
81+
#if XUNIT_V3
8282
[Fact(Explicit = true)]
8383
#else
8484
[Fact(Skip = ManualOnlyReason)]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading;
2+
using Xunit;
3+
4+
namespace NetCoreForce.FunctionalTests
5+
{
6+
/// <summary>
7+
/// Cancellation token for the current test.
8+
/// xUnit v3 provides one via TestContext so long-running calls stop when a test run is cancelled; xUnit v2 (net462) has no equivalent.
9+
/// </summary>
10+
internal static class TestCancellation
11+
{
12+
public static CancellationToken Token
13+
{
14+
get
15+
{
16+
#if XUNIT_V3
17+
return TestContext.Current.CancellationToken;
18+
#else
19+
return CancellationToken.None;
20+
#endif
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)