Skip to content

Commit 943e0c5

Browse files
committed
Add test cancellation
1 parent 8818f4a commit 943e0c5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/ServiceControl.Audit.Persistence.Tests.RavenDB/IndexSetupTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task Free_text_search_index_should_be_used_by_default()
3838
[Test]
3939
public async Task Free_text_search_index_can_be_opted_out_from()
4040
{
41-
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, false, CancellationToken.None);
41+
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, false, TestTimeoutCancellationToken);
4242

4343
var freeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName));
4444
var nonFreeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexName));
@@ -56,7 +56,7 @@ public async Task Indexes_should_be_reset_on_setup()
5656

5757
Assert.That(indexWithCustomConfigStats.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene));
5858

59-
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None);
59+
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, TestTimeoutCancellationToken);
6060

6161
await WaitForIndexDefinitionUpdate(indexWithCustomConfigStats);
6262

@@ -78,7 +78,7 @@ public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_ignore()
7878

7979
Assert.That(indexStatsBefore.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene));
8080

81-
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None);
81+
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, TestTimeoutCancellationToken);
8282

8383
// raven will ignore the update since index was locked, so best we can do is wait a bit and check that settings hasn't changed
8484
await Task.Delay(1000);
@@ -98,25 +98,25 @@ public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_error()
9898

9999
await UpdateIndex(index);
100100

101-
Assert.ThrowsAsync<IndexCreationException>(async () => await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None));
101+
Assert.ThrowsAsync<IndexCreationException>(async () => await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, TestTimeoutCancellationToken));
102102
}
103103

104-
async Task<IndexStats> UpdateIndex(IAbstractIndexCreationTask index, CancellationToken cancellationToken = default)
104+
async Task<IndexStats> UpdateIndex(IAbstractIndexCreationTask index)
105105
{
106-
var statsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName), cancellationToken);
106+
var statsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName), TestTimeoutCancellationToken);
107107

108-
await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore, null, null, cancellationToken);
108+
await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore, null, null, TestTimeoutCancellationToken);
109109

110-
return await WaitForIndexDefinitionUpdate(statsBefore, cancellationToken);
110+
return await WaitForIndexDefinitionUpdate(statsBefore);
111111
}
112112

113-
async Task<IndexStats> WaitForIndexDefinitionUpdate(IndexStats oldStats, CancellationToken cancellationToken = default)
113+
async Task<IndexStats> WaitForIndexDefinitionUpdate(IndexStats oldStats)
114114
{
115115
while (true)
116116
{
117117
try
118118
{
119-
var newStats = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(oldStats.Name), cancellationToken);
119+
var newStats = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(oldStats.Name), TestTimeoutCancellationToken);
120120

121121
if (newStats.CreatedTimestamp > oldStats.CreatedTimestamp)
122122
{
@@ -128,7 +128,7 @@ async Task<IndexStats> WaitForIndexDefinitionUpdate(IndexStats oldStats, Cancell
128128
// keep going since we can get this if we query right when the update happens
129129
}
130130

131-
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
131+
await Task.Delay(TimeSpan.FromMilliseconds(100), TestTimeoutCancellationToken);
132132
}
133133
}
134134
}

src/ServiceControl.Audit.Persistence.Tests/PersistenceTestFixture.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace ServiceControl.Audit.Persistence.Tests
22
{
33
using System;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
7+
using System.Threading;
68
using System.Threading.Tasks;
79
using Auditing.BodyStorage;
810
using NUnit.Framework;
@@ -18,12 +20,15 @@ public virtual Task Setup()
1820
{
1921
configuration = new PersistenceTestsConfiguration();
2022

23+
testCancellationTokenSource = Debugger.IsAttached ? new CancellationTokenSource() : new CancellationTokenSource(TestTimeout);
24+
2125
return configuration.Configure(SetSettings);
2226
}
2327

2428
[TearDown]
2529
public virtual Task Cleanup()
2630
{
31+
testCancellationTokenSource?.Dispose();
2732
return configuration?.Cleanup();
2833
}
2934

@@ -64,5 +69,11 @@ protected ValueTask<IAuditIngestionUnitOfWork> StartAuditUnitOfWork(int batchSiz
6469
protected IServiceProvider ServiceProvider => configuration.ServiceProvider;
6570

6671
protected PersistenceTestsConfiguration configuration;
72+
73+
protected CancellationToken TestTimeoutCancellationToken => testCancellationTokenSource.Token;
74+
75+
CancellationTokenSource testCancellationTokenSource;
76+
77+
static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(30);
6778
}
6879
}

0 commit comments

Comments
 (0)