private void ClearCache() { lock (_writeBehindFunc) { CompleteAsync(); _cache.Clear(); _dirty.Clear(); } }
public void CompareTest() { const int size = 1000000; int reps = 3; Stopwatch timer; IDictionary <Guid, TestValue> dict = new ConcurrentDictionary <Guid, TestValue>(new Dictionary <Guid, TestValue>(size)); IDictionary <Guid, TestValue> test = new LurchTable <Guid, TestValue>(size); for (int rep = 0; rep < reps; rep++) { var sample = CreateSample(Guid.NewGuid(), size, 1); timer = Stopwatch.StartNew(); Parallel(1, sample, item => dict.Add(item, new TestValue { Id = item, Count = rep })); Trace.TraceInformation("Dict Add: {0}", timer.Elapsed); timer = Stopwatch.StartNew(); Parallel(1, sample, item => test.Add(item, new TestValue { Id = item, Count = rep })); Trace.TraceInformation("Test Add: {0}", timer.Elapsed); timer = Stopwatch.StartNew(); Parallel(1, sample, item => dict[item] = new TestValue { Id = item, Count = rep }); Trace.TraceInformation("Dict Update: {0}", timer.Elapsed); timer = Stopwatch.StartNew(); Parallel(1, sample, item => test[item] = new TestValue { Id = item, Count = rep }); Trace.TraceInformation("Test Update: {0}", timer.Elapsed); timer = Stopwatch.StartNew(); Parallel(1, sample, item => dict.Remove(item)); Trace.TraceInformation("Dict Rem: {0}", timer.Elapsed); Assert.AreEqual(0, dict.Count); timer = Stopwatch.StartNew(); Parallel(1, sample, item => test.Remove(item)); Trace.TraceInformation("Test Rem: {0}", timer.Elapsed); test.Clear(); dict.Clear(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
public void Clear_OnEmptyCollection_DoesNotInvalidateEnumerator() { if (ModifyEnumeratorAllowed.HasFlag(ModifyOperation.Clear)) { IDictionary dictionary = new LurchTable <string, string>(); IEnumerator valuesEnum = dictionary.GetEnumerator(); dictionary.Clear(); Assert.Empty(dictionary); Assert.False(valuesEnum.MoveNext()); } }
public static void TestClear() { var dictionary = new LurchTable <int, int>(); for (int i = 0; i < 10; i++) { dictionary.TryAdd(i, i); } Assert.Equal(10, dictionary.Count); dictionary.Clear(); Assert.Equal(0, dictionary.Count); int item; Assert.False(dictionary.TryRemove(1, out item), "TestClear: FAILED. TryRemove succeeded after Clear"); Assert.True(dictionary.IsEmpty, "TestClear: FAILED. IsEmpty returned false after Clear"); }
public void Clear() { cache.Clear(); }
// Must SYNC on lock (_flushSync) private void ClearCache() { _cache.Clear(); _dirty.Clear(); }
public static void TestTryUpdate() { var dictionary = new LurchTable <string, int>(); Assert.Null(Record.Exception( () => dictionary.TryUpdate(null, 0, 0))); // "TestTryUpdate: FAILED. TryUpdate threw ANE when null key is passed"); for (int i = 0; i < 10; i++) { dictionary.TryAdd(i.ToString(), i); } for (int i = 0; i < 10; i++) { Assert.True(dictionary.TryUpdate(i.ToString(), i + 1, i), "TestTryUpdate: FAILED. TryUpdate failed!"); Assert.Equal(i + 1, dictionary[i.ToString()]); } //test TryUpdate concurrently dictionary.Clear(); for (int i = 0; i < 1000; i++) { dictionary.TryAdd(i.ToString(), i); } var mres = new ManualResetEventSlim(); Task[] tasks = new Task[10]; ThreadLocal <ThreadData> updatedKeys = new ThreadLocal <ThreadData>(true); for (int i = 0; i < tasks.Length; i++) { // We are creating the Task using TaskCreationOptions.LongRunning because... // there is no guarantee that the Task will be created on another thread. // There is also no guarantee that using this TaskCreationOption will force // it to be run on another thread. tasks[i] = Task.Factory.StartNew((obj) => { mres.Wait(); int index = (((int)obj) + 1) + 1000; updatedKeys.Value = new ThreadData(); updatedKeys.Value.ThreadIndex = index; for (int j = 0; j < dictionary.Count; j++) { if (dictionary.TryUpdate(j.ToString(), index, j)) { if (dictionary[j.ToString()] != index) { updatedKeys.Value.Succeeded = false; return; } updatedKeys.Value.Keys.Add(j.ToString()); } } }, i, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } mres.Set(); Task.WaitAll(tasks); int numberSucceeded = 0; int totalKeysUpdated = 0; foreach (var threadData in updatedKeys.Values) { totalKeysUpdated += threadData.Keys.Count; if (threadData.Succeeded) { numberSucceeded++; } } Assert.True(numberSucceeded == tasks.Length, "One or more threads failed!"); Assert.True(totalKeysUpdated == dictionary.Count, string.Format("TestTryUpdate: FAILED. The updated keys count doesn't match the dictionary count, expected {0}, actual {1}", dictionary.Count, totalKeysUpdated)); foreach (var value in updatedKeys.Values) { for (int i = 0; i < value.Keys.Count; i++) { Assert.True(dictionary[value.Keys[i]] == value.ThreadIndex, string.Format("TestTryUpdate: FAILED. The updated value doesn't match the thread index, expected {0} actual {1}", value.ThreadIndex, dictionary[value.Keys[i]])); } } //test TryUpdate with non atomic values (intPtr > 8) var dict = new LurchTable <int, Struct16>(); dict.TryAdd(1, new Struct16(1, -1)); Assert.True(dict.TryUpdate(1, new Struct16(2, -2), new Struct16(1, -1)), "TestTryUpdate: FAILED. TryUpdate failed for non atomic values ( > 8 bytes)"); }