|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Threading; |
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using BitFaster.Caching.Lru;
|
6 | 7 | using FluentAssertions;
|
7 | 8 | using Xunit;
|
8 | 9 | using Xunit.Abstractions;
|
| 10 | +using static BitFaster.Caching.UnitTests.Lru.LruItemSoakTests; |
9 | 11 |
|
10 | 12 | namespace BitFaster.Caching.UnitTests.Lru
|
11 | 13 | {
|
@@ -282,6 +284,52 @@ await Threaded.Run(4, r => {
|
282 | 284 | RunIntegrityCheck();
|
283 | 285 | }
|
284 | 286 |
|
| 287 | + // This test will run forever if there is a live lock. |
| 288 | + // Since the cache bookkeeping has some overhead, it is harder to provoke |
| 289 | + // spinning inside the reader thread compared to LruItemSoakTests.DetectTornStruct. |
| 290 | + [Theory] |
| 291 | + [Repeat(10)] |
| 292 | + public async Task WhenValueIsBigStructNoLiveLock(int _) |
| 293 | + { |
| 294 | + using var source = new CancellationTokenSource(); |
| 295 | + var started = new TaskCompletionSource<bool>(); |
| 296 | + var cache = new ConcurrentLru<int, Guid>(1, capacity, EqualityComparer<int>.Default); |
| 297 | + |
| 298 | + var setTask = Task.Run(() => Setter(cache, source.Token, started)); |
| 299 | + await started.Task; |
| 300 | + Checker(cache, source); |
| 301 | + |
| 302 | + await setTask; |
| 303 | + } |
| 304 | + |
| 305 | + private void Setter(ICache<int, Guid> cache, CancellationToken cancelToken, TaskCompletionSource<bool> started) |
| 306 | + { |
| 307 | + started.SetResult(true); |
| 308 | + |
| 309 | + while (true) |
| 310 | + { |
| 311 | + cache.AddOrUpdate(1, Guid.NewGuid()); |
| 312 | + cache.AddOrUpdate(1, Guid.NewGuid()); |
| 313 | + |
| 314 | + if (cancelToken.IsCancellationRequested) |
| 315 | + { |
| 316 | + return; |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + private void Checker(ICache<int, Guid> cache,CancellationTokenSource source) |
| 322 | + { |
| 323 | + // On my machine, without SeqLock, this consistently fails below 100 iterations |
| 324 | + // on debug build, and below 1000 on release build |
| 325 | + for (int count = 0; count < 100_000; ++count) |
| 326 | + { |
| 327 | + cache.TryGet(1, out _); |
| 328 | + } |
| 329 | + |
| 330 | + source.Cancel(); |
| 331 | + } |
| 332 | + |
285 | 333 | private void RunIntegrityCheck()
|
286 | 334 | {
|
287 | 335 | new ConcurrentLruIntegrityChecker<int, string, LruItem<int, string>, LruPolicy<int, string>, TelemetryPolicy<int, string>>(this.lru).Validate();
|
|
0 commit comments