public void TryGetValue_should_return_true_for_added_keys() { var keys = Enumerable.Range(0, 100 * 000).Select(_ => Guid.NewGuid()).ToArray(); foreach (var key in keys) { dic.Add(key, key.ToString()); dic.TryGetValue(key, out _).Should().BeTrue(); } foreach (var key in keys) { dic.TryGetValue(key, out _).Should().BeTrue(); } }
private void LaunchReader(TimeSpan time, CancellationToken cancellationToken) { var watch = Stopwatch.StartNew(); var lastRead = watch.ElapsedTicks; var readerTask = Task.Run(() => { var random = new Random(Guid.NewGuid().GetHashCode()); try { while (watch.Elapsed < time && !cancellationToken.IsCancellationRequested) { var index = random.Next(keys.Length); var found = dic.TryGetValue(keys[index], out var value); if (!found && index < keys.Length / 2) { throw new Exception("Failed to find a key which never gets removed!"); } if (found && value != keys[index].ToString()) { throw new Exception("TryGetValue() returned some garbage as value!"); } Interlocked.Exchange(ref lastRead, watch.ElapsedTicks); } } catch (Exception error) { Console.Out.WriteLine(error); throw new Exception("Error in reader!", error); } }); var checkerTask = Task.Run(() => { while (watch.Elapsed < time && !cancellationToken.IsCancellationRequested) { Thread.Sleep(50); var diff = new TimeSpan(watch.ElapsedTicks - Interlocked.Read(ref lastRead)); if (diff.TotalSeconds >= 0.5) { throw new Exception("A reader hangs!"); } } }); tasks.Add(readerTask); tasks.Add(checkerTask); }