using ZoneTree.Collections; using ZoneTree.Collections.BTree; using ZoneTree.Collections.BTree.Lock; using ZoneTree.Comparers; namespace ZoneTree.UnitTests; public sealed class BTreeThreadSafetyTests { [TestCase(3_000_000, BTreeLockMode.TopLevelReaderWriter)] [TestCase(3_000_000, BTreeLockMode.TopLevelMonitor)] [TestCase(3_000_000, BTreeLockMode.NodeLevelMonitor)] [TestCase(3_000_000, BTreeLockMode.NodeLevelReaderWriter)] public void MassiveInsertsAndReads(int count, BTreeLockMode lockMode) { var readCount = 0; var tree = new BTree(new Int64ComparerAscending(), lockMode); var task1 = Parallel.ForEachAsync(Enumerable.Range(0, count), (i, t) => { tree.TryInsert(i, i, out _); return ValueTask.CompletedTask; }); Thread.Sleep(1); var task2 = Parallel.ForEachAsync(Enumerable.Range(0, count), (i, t) => { if (tree.TryGetValue(i, out var j)) { if (i != j) throw new Exception($"{i} != {j}"); Interlocked.Increment(ref readCount); } return ValueTask.CompletedTask; }); task1.Wait(); task2.Wait(); Console.WriteLine("Read Count: " + readCount); } }