private static void ChurnConcurrent() { var dict = new NonBlocking.ConcurrentDictionary <int, string>(); //var dict = new Concurrent.ConcurrentDictionary<int, string>(); var threadCnt = 200; List <Task> tasks = new List <Task>(threadCnt); var sw = Stopwatch.StartNew(); for (int i = 0; i < threadCnt; i++) { var task = new Task(() => { for (int j = i * 1000000, l = j + 1000000; j < l; j++) { string dummy; dict.TryAdd(j, "dummy"); dict.TryRemove(j, out dummy); //Thread.Sleep(10); } System.Console.Write('.'); }, TaskCreationOptions.LongRunning); tasks.Add(task); task.Start(); } Task.WaitAll(tasks.ToArray()); System.Console.WriteLine(sw.ElapsedMilliseconds); }
public static void TestExceptions() { var dictionary = new NonBlocking.ConcurrentDictionary <string, int>(); Assert.Throws <ArgumentNullException>( () => dictionary.TryAdd(null, 0)); // "TestExceptions: FAILED. TryAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.ContainsKey(null)); // "TestExceptions: FAILED. Contains didn't throw ANE when null key is passed"); int item; Assert.Throws <ArgumentNullException>( () => dictionary.TryRemove(null, out item)); // "TestExceptions: FAILED. TryRemove didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.TryGetValue(null, out item)); // "TestExceptions: FAILED. TryGetValue didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => { var x = dictionary[null]; }); // "TestExceptions: FAILED. this[] didn't throw ANE when null key is passed"); Assert.Throws <KeyNotFoundException>( () => { var x = dictionary["1"]; }); // "TestExceptions: FAILED. this[] TryGetValue didn't throw KeyNotFoundException!"); Assert.Throws <ArgumentNullException>( () => dictionary[null] = 1); // "TestExceptions: FAILED. this[] didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd(null, (k) => 0)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd("1", null)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd(null, 0)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate("1", null, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, null)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); dictionary.TryAdd("1", 1); Assert.Throws <ArgumentException>( () => ((IDictionary <string, int>)dictionary).Add("1", 2)); // "TestExceptions: FAILED. IDictionary didn't throw AE when duplicate key is passed"); }
private static void TestRemove2(int removesPerThread) { NonBlocking.ConcurrentDictionary <int, int> dict = new NonBlocking.ConcurrentDictionary <int, int>(); for (int i = 0; i < removesPerThread; i++) { dict[i] = -i; } // The dictionary contains keys [0..N), each key mapped to a value equal to the key. // Threads will cooperatively remove all even keys. const int SIZE = 2; int running = SIZE; bool[][] seen = new bool[SIZE][]; for (int i = 0; i < SIZE; i++) { seen[i] = new bool[removesPerThread]; } using (ManualResetEvent mre = new ManualResetEvent(false)) { for (int t = 0; t < SIZE; t++) { int thread = t; Task.Run( () => { for (int key = 0; key < removesPerThread; key++) { int value; if (dict.TryRemove(key, out value)) { seen[thread][key] = true; Assert.Equal(-key, value); } } if (Interlocked.Decrement(ref running) == 0) { mre.Set(); } }); } mre.WaitOne(); } Assert.Equal(0, dict.Count); for (int i = 0; i < removesPerThread; i++) { Assert.False(seen[0][i] == seen[1][i], String.Format("> FAILED. Two threads appear to have removed the same element. TestRemove2(removesPerThread={0})", removesPerThread) ); } }
public static void TestClear() { var dictionary = new NonBlocking.ConcurrentDictionary <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 static void TestClear() { var dictionary = new NonBlocking.ConcurrentDictionary <int, int>(); for (int i = 0; i < 10; i++) { dictionary.TryAdd(i, i); } Assert.Equal(10, dictionary.Count); dictionary.Clear(); #pragma warning disable xUnit2013 // Do not use equality check to check for collection size. Assert.Equal(0, dictionary.Count); #pragma warning restore xUnit2013 // Do not use equality check to check for collection size. 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"); }
private static void TestRemove1(int cLevel, int threads, int removesPerThread) { NonBlocking.ConcurrentDictionary <int, int> dict = new NonBlocking.ConcurrentDictionary <int, int>(cLevel, 1); string methodparameters = string.Format("* TestRemove1(cLevel={0}, threads={1}, removesPerThread={2})", cLevel, threads, removesPerThread); int N = 2 * threads * removesPerThread; for (int i = 0; i < N; i++) { dict[i] = -i; } // The dictionary contains keys [0..N), each key mapped to a value equal to the key. // Threads will cooperatively remove all even keys int running = threads; using (ManualResetEvent mre = new ManualResetEvent(false)) { for (int i = 0; i < threads; i++) { int ii = i; Task.Run( () => { for (int j = 0; j < removesPerThread; j++) { int value; int key = 2 * (ii + j * threads); Assert.True(dict.TryRemove(key, out value), "Failed to remove an element! " + methodparameters); Assert.Equal(-key, value); } if (Interlocked.Decrement(ref running) == 0) { mre.Set(); } }); } mre.WaitOne(); } foreach (var pair in dict) { Assert.Equal(pair.Key, -pair.Value); } List <int> gotKeys = new List <int>(); foreach (var pair in dict) { gotKeys.Add(pair.Key); } gotKeys.Sort(); List <int> expectKeys = new List <int>(); for (int i = 0; i < (threads * removesPerThread); i++) { expectKeys.Add(2 * i + 1); } Assert.Equal(expectKeys.Count, gotKeys.Count); for (int i = 0; i < expectKeys.Count; i++) { Assert.True(expectKeys[i].Equals(gotKeys[i]), " > Unexpected key value! " + methodparameters); } // Finally, let's verify that the count is reported correctly. Assert.Equal(expectKeys.Count, dict.Count); Assert.Equal(expectKeys.Count, dict.ToArray().Length); }