private async Task LightDictionaryConcurrentAccessDetection <TKey, TValue>(LightDictionary <TKey, TValue> dictionary, bool isValueType, object comparer, Action <LightDictionary <TKey, TValue> > add, Action <LightDictionary <TKey, TValue> > get, Action <LightDictionary <TKey, TValue> > remove) { Task task = Task.Factory.StartNew(() => { // Get the Dictionary into a corrupted state, as if it had been corrupted by concurrent access. // We this deterministically by clearing the _entries array using reflection; // this means that every Entry struct has a 'next' field of zero, which causes the infinite loop // that we want Dictionary to break out of FieldInfo entriesType = dictionary.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance); Array entriesInstance = (Array)entriesType.GetValue(dictionary); Array entryArray = (Array)Activator.CreateInstance(entriesInstance.GetType(), new object[] { ((IDictionary)dictionary).Count }); entriesType.SetValue(dictionary, entryArray); Assert.Equal(comparer, dictionary.GetType().GetField("_comparer", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(dictionary)); Assert.Equal(isValueType, dictionary.GetType().GetGenericArguments()[0].IsValueType); Assert.Equal("ThrowInvalidOperationException_ConcurrentOperationsNotSupported", Assert.Throws <InvalidOperationException>(() => add(dictionary)).TargetSite.Name); Assert.Equal("ThrowInvalidOperationException_ConcurrentOperationsNotSupported", Assert.Throws <InvalidOperationException>(() => get(dictionary)).TargetSite.Name); Assert.Equal("ThrowInvalidOperationException_ConcurrentOperationsNotSupported", Assert.Throws <InvalidOperationException>(() => remove(dictionary)).TargetSite.Name); }, TaskCreationOptions.LongRunning); // If Dictionary regresses, we do not want to hang here indefinitely Assert.True((await Task.WhenAny(task, Task.Delay(TimeSpan.FromSeconds(60))) == task) && task.IsCompleted); }