Skip to content

Commit 0aa1060

Browse files
committed
Handle race conditions
1 parent 7764a16 commit 0aa1060

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

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

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public async Task Free_text_search_index_can_be_opted_out_from()
4040
{
4141
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, false, CancellationToken.None);
4242

43-
//TODO: find a better way
44-
await Task.Delay(1000);
45-
4643
var freeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName));
4744
var nonFreeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexName));
4845

@@ -55,48 +52,35 @@ public async Task Indexes_should_be_reset_on_setup()
5552
{
5653
var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } };
5754

58-
await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore);
59-
60-
//TODO: find a better way
61-
await Task.Delay(1000);
62-
63-
var indexStatsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
55+
var indexWithCustomConfigStats = await UpdateIndex(index);
6456

65-
Assert.That(indexStatsBefore.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene));
57+
Assert.That(indexWithCustomConfigStats.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene));
6658

6759
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None);
6860

69-
//TODO: find a better way
70-
await Task.Delay(1000);
61+
WaitForIndexDefinitionUpdate(indexWithCustomConfigStats);
7162

72-
var indexStatsAfter = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
73-
Assert.That(indexStatsAfter.SearchEngineType, Is.EqualTo(SearchEngineType.Corax));
63+
var indexAfterResetStats = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
64+
65+
Assert.That(indexAfterResetStats.SearchEngineType, Is.EqualTo(SearchEngineType.Corax));
7466
}
7567

7668
[Test]
7769
public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_ignore()
7870
{
79-
var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } };
80-
81-
await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore);
82-
83-
await configuration.DocumentStore.Maintenance.SendAsync(new SetIndexesLockOperation(new SetIndexesLockOperation.Parameters
71+
var index = new MessagesViewIndexWithFullTextSearch
8472
{
85-
IndexNames = [index.IndexName],
86-
Mode = IndexLockMode.LockedIgnore
87-
}));
88-
89-
//TODO: find a better way
90-
await Task.Delay(1000);
73+
Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() },
74+
LockMode = IndexLockMode.LockedIgnore
75+
};
9176

92-
var indexStatsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
77+
var indexStatsBefore = await UpdateIndex(index);
9378

9479
Assert.That(indexStatsBefore.SearchEngineType, Is.EqualTo(SearchEngineType.Lucene));
9580

96-
9781
await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None);
9882

99-
//TODO: find a better way
83+
// 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
10084
await Task.Delay(1000);
10185

10286
var indexStatsAfter = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
@@ -106,16 +90,41 @@ await configuration.DocumentStore.Maintenance.SendAsync(new SetIndexesLockOperat
10690
[Test]
10791
public async Task Indexes_should_not_be_reset_on_setup_when_locked_as_error()
10892
{
109-
var index = new MessagesViewIndexWithFullTextSearch { Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() } };
93+
var index = new MessagesViewIndexWithFullTextSearch
94+
{
95+
Configuration = { ["Indexing.Static.SearchEngineType"] = SearchEngineType.Lucene.ToString() },
96+
LockMode = IndexLockMode.LockedError
97+
};
98+
99+
await UpdateIndex(index);
100+
101+
Assert.ThrowsAsync<IndexCreationException>(async () => await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None));
102+
}
103+
104+
async Task<IndexStats> UpdateIndex(IAbstractIndexCreationTask index)
105+
{
106+
var statsBefore = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
110107

111108
await IndexCreation.CreateIndexesAsync([index], configuration.DocumentStore);
112109

113-
await configuration.DocumentStore.Maintenance.SendAsync(new SetIndexesLockOperation(new SetIndexesLockOperation.Parameters
114-
{
115-
IndexNames = [index.IndexName],
116-
Mode = IndexLockMode.LockedError
117-
}));
110+
WaitForIndexDefinitionUpdate(statsBefore);
118111

119-
Assert.ThrowsAsync<IndexCreationException>(async () => await DatabaseSetup.CreateIndexes(configuration.DocumentStore, true, CancellationToken.None));
112+
return await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(index.IndexName));
113+
}
114+
115+
void WaitForIndexDefinitionUpdate(IndexStats indexStats)
116+
{
117+
Assert.That(SpinWait.SpinUntil(() =>
118+
{
119+
try
120+
{
121+
return configuration.DocumentStore.Maintenance.Send(new GetIndexStatisticsOperation(indexStats.Name)).CreatedTimestamp > indexStats.CreatedTimestamp;
122+
}
123+
catch (OperationCanceledException)
124+
{
125+
// keep going since we can get this if we query right when the update happens
126+
return false;
127+
}
128+
}, TimeSpan.FromSeconds(10)), Is.True);
120129
}
121130
}

0 commit comments

Comments
 (0)