-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathQueryCacheThreadingTests.cs
More file actions
283 lines (254 loc) · 12.3 KB
/
QueryCacheThreadingTests.cs
File metadata and controls
283 lines (254 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Microsoft.UI.Reactor.Core;
using Xunit;
namespace Microsoft.UI.Reactor.Tests.Core;
/// <summary>
/// Race / contention coverage for <see cref="QueryCache"/>. All tests here use real
/// threads (<see cref="Task.Run"/> + <see cref="Barrier"/>) and complete within a
/// bounded timeout — slow individually, important collectively.
/// </summary>
[Trait("Category", "Threading")]
public class QueryCacheThreadingTests
{
private const int StressThreads = 8;
// Drain budget for every WaitAsync in this file. Workloads are bounded by
// per-test CancellationTokenSource windows (typically 250ms–5s) — this
// timeout only exists to catch a genuine deadlock. On a local machine the
// tests finish in milliseconds; on shared CI runners (virtualized cores,
// bursty scheduling) flushing the Task scheduler after cancellation can
// stretch unpredictably. A generous ceiling kills flakes without weakening
// the deadlock guarantee. Bumped 60s→120s after a 1-in-1000 stress hit on
// Concurrent_Subscribe_Unsubscribe_Converges_To_Zero where the workload is
// pure lock-bound bookkeeping (no eviction is reachable in 60s given the
// 5-minute default CacheTime) — i.e. the timeout was scheduler-induced.
private static readonly TimeSpan DrainTimeout = TimeSpan.FromSeconds(120);
// ════════════════════════════════════════════════════════════════
// Concurrent Set + TryGet — no torn reads
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task Concurrent_Set_And_TryGet_Never_Observe_Torn_Entry()
{
using var cache = new QueryCache();
using var barrier = new Barrier(8);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
int tornReads = 0;
var tasks = new List<Task>();
for (int i = 0; i < 4; i++)
{
int me = i;
tasks.Add(Task.Run(() =>
{
barrier.SignalAndWait();
while (!cts.IsCancellationRequested)
cache.Set("k", me * 1000, TimeSpan.Zero, TimeSpan.FromMinutes(5));
}));
}
for (int i = 0; i < 4; i++)
{
tasks.Add(Task.Run(() =>
{
barrier.SignalAndWait();
while (!cts.IsCancellationRequested)
{
if (cache.TryGet<int>("k", out var e))
{
// Accept only the values actually written by the writers.
int v = e.Value;
if (v != 0 && v != 1000 && v != 2000 && v != 3000)
Interlocked.Increment(ref tornReads);
}
}
}));
}
cts.CancelAfter(TimeSpan.FromMilliseconds(250));
await Task.WhenAll(tasks).WaitAsync(DrainTimeout);
Assert.Equal(0, tornReads);
}
// ════════════════════════════════════════════════════════════════
// Concurrent Subscribe/Unsubscribe — ref-count never negative
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task Concurrent_Subscribe_Unsubscribe_Converges_To_Zero()
{
using var cache = new QueryCache();
const int threads = StressThreads;
const int iters = 500;
using var barrier = new Barrier(threads);
int negativeCount = 0;
var tasks = new Task[threads];
for (int i = 0; i < threads; i++)
{
tasks[i] = Task.Run(() =>
{
barrier.SignalAndWait();
for (int n = 0; n < iters; n++)
{
int after = cache.Subscribe("k");
if (after <= 0) Interlocked.Increment(ref negativeCount);
}
for (int n = 0; n < iters; n++)
{
int after = cache.Unsubscribe("k");
if (after < 0) Interlocked.Increment(ref negativeCount);
}
});
}
await Task.WhenAll(tasks).WaitAsync(DrainTimeout);
Assert.Equal(0, negativeCount);
// Final count should be exactly zero.
cache.Subscribe("k");
Assert.Equal(0, cache.Unsubscribe("k")); // if start was 0, +1 -1 = 0.
}
// ════════════════════════════════════════════════════════════════
// Invalidate-during-Set race
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task Invalidate_Racing_With_Set_Leaves_Consistent_State()
{
using var cache = new QueryCache();
var cts = new CancellationTokenSource();
int inconsistencies = 0;
var setter = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
cache.Set("k", 42, TimeSpan.Zero, TimeSpan.FromMinutes(5));
});
var invalidator = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
cache.Invalidate("k");
});
var reader = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
{
// Either the entry is absent or its value is 42 — never a different value.
if (cache.TryGet<int>("k", out var e) && e.Value != 42)
Interlocked.Increment(ref inconsistencies);
}
});
await Task.Delay(250);
cts.Cancel();
await Task.WhenAll(setter, invalidator, reader).WaitAsync(DrainTimeout);
Assert.Equal(0, inconsistencies);
}
// ════════════════════════════════════════════════════════════════
// Eviction timer vs. Subscribe race
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task Resubscribe_Racing_Against_EvictNow_Retains_Entry()
{
// Repeat many times to surface the race — if eviction observes the
// zero-subscriber count then a Subscribe bumps it before the remove
// tries, the entry must stay.
for (int trial = 0; trial < 200; trial++)
{
using var cache = new QueryCache();
var now = DateTime.UtcNow;
cache.UtcNow = () => now;
cache.Subscribe("k");
cache.Set("k", 1, TimeSpan.Zero, TimeSpan.FromMilliseconds(1));
cache.Unsubscribe("k");
cache.UtcNow = () => now.AddSeconds(5); // well past cache time
using var barrier = new Barrier(2);
Task sub = Task.Run(() =>
{
barrier.SignalAndWait();
cache.Subscribe("k");
});
Task evict = Task.Run(() =>
{
barrier.SignalAndWait();
cache.EvictNow();
});
await Task.WhenAll(sub, evict).WaitAsync(DrainTimeout);
// At least one of two things must be true:
// (a) EvictNow ran first and the entry was cleared; Subscribe created a fresh slot.
// (b) Subscribe won and the entry survived.
// Either way, the final subscriber count is 1 and the slot is observable.
Assert.True(cache.Count >= 1);
}
}
// ════════════════════════════════════════════════════════════════
// InvalidatePattern concurrent with Set
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task InvalidatePattern_Racing_With_Set_Does_Not_Throw()
{
using var cache = new QueryCache();
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250));
var setter = Task.Run(() =>
{
int i = 0;
while (!cts.IsCancellationRequested)
{
cache.Set($"user/{i++ % 100}", i, TimeSpan.Zero, TimeSpan.FromMinutes(5));
}
});
var invalidator = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
cache.InvalidatePattern("user/");
});
// The test passes iff neither task throws.
await Task.WhenAll(setter, invalidator).WaitAsync(DrainTimeout);
Assert.True(setter.IsCompletedSuccessfully);
Assert.True(invalidator.IsCompletedSuccessfully);
}
// ════════════════════════════════════════════════════════════════
// EntryChanged fires exactly once per Set
// ════════════════════════════════════════════════════════════════
[Fact]
public void EntryChanged_Fires_Exactly_Once_Per_Set()
{
using var cache = new QueryCache();
int fires = 0;
cache.EntryChanged += _ => Interlocked.Increment(ref fires);
for (int i = 0; i < 100; i++)
cache.Set("k", i, TimeSpan.Zero, TimeSpan.FromMinutes(5));
Assert.Equal(100, fires);
}
// ════════════════════════════════════════════════════════════════
// Stress — mixed ops across many keys + threads
// ════════════════════════════════════════════════════════════════
[Fact]
public async Task Stress_1000_Keys_Mixed_Ops_Terminates_Without_Exceptions()
{
using var cache = new QueryCache();
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
int exceptions = 0;
var tasks = new List<Task>();
for (int t = 0; t < StressThreads; t++)
{
int seed = t;
tasks.Add(Task.Run(() =>
{
var rnd = new Random(seed);
try
{
while (!cts.IsCancellationRequested)
{
string k = $"key/{rnd.Next(1000)}";
int op = rnd.Next(6);
switch (op)
{
case 0: cache.Set(k, rnd.Next(), TimeSpan.Zero, TimeSpan.FromMilliseconds(50)); break;
case 1: cache.TryGet<int>(k, out _); break;
case 2: cache.Invalidate(k); break;
case 3: cache.Subscribe(k); break;
case 4:
try { cache.Unsubscribe(k); } catch (InvalidOperationException) { /* ok — underflow-guard working */ }
break;
case 5: cache.EvictNow(); break;
}
}
}
catch (Exception)
{
Interlocked.Increment(ref exceptions);
}
}));
}
await Task.WhenAll(tasks).WaitAsync(DrainTimeout);
Assert.Equal(0, exceptions);
}
}