public void AsyncLockFreeDictionaryIntItShouldBePossibleToDisposeEnumeratorsConcurrently() { var dict = new AsyncLockFreeDictionary <string, string>(); dict.Add("1", "v1"); dict.Add("2", "v2"); dict.Add("3", "v3"); Thread.Sleep(100); var enumerator1 = dict.GetEnumerator(); var enumerator2 = dict.GetEnumerator(); enumerator1.MoveNext(); enumerator2.MoveNext(); enumerator2.Dispose(); var current = enumerator1.Current; Assert.AreEqual("v1", current.Value); dict.Remove("2"); Thread.Sleep(100); enumerator1.MoveNext(); current = enumerator1.Current; Assert.AreEqual("v3", current.Value); }
public void AsyncLockFreeDictionaryIntItShouldBePossibleToReadConcurrently() { ResetData(); for (int i = 0; i < 100; i++) { _dict.Add(i.ToString(CultureInfo.InvariantCulture), i.ToString(CultureInfo.InvariantCulture)); } var signal = new ManualResetEvent(false); var threads = new List <TestThread>(); for (int j = 0; j < 100; j++) { var tt = CreateReadThread(j, signal); threads.Add(tt); } signal.Set(); Thread.Sleep(1000); foreach (var th in threads) { th.Stop(); } Thread.Sleep(1000); foreach (var th in threads) { Assert.IsNull(th.ExceptionThrown); } }
public void LockFreeDictionaryTryAddKeyValuePair() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); Thread.Sleep(100); dict.TryAdd(new KeyValuePair <int, int>(1, 100), (preExisting, newKeyValuePair) => new KeyValuePair <int, int>(newKeyValuePair.Key, newKeyValuePair.Value * 2)); Thread.Sleep(100); Assert.AreEqual(2, dict.Count); Assert.AreEqual(200, dict[1]); Assert.AreEqual(2, dict[2]); }
public void LockFreeDictionaryContainsKeyValuePair() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); dict.Add(3, 3); dict.Add(4, 4); dict.Add(5, 5); Thread.Sleep(100); Assert.AreEqual(5, dict.Count); Assert.IsFalse(dict.Contains(new KeyValuePair <int, int>(1, 9))); Assert.IsTrue(dict.Contains(new KeyValuePair <int, int>(1, 1))); }
public void LockFreeDictionaryAddKeyValuePair() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(new KeyValuePair <int, int>(1, 1)); dict.Add(new KeyValuePair <int, int>(2, 2)); dict.Add(new KeyValuePair <int, int>(3, 3)); dict.Add(new KeyValuePair <int, int>(4, 4)); dict.Add(new KeyValuePair <int, int>(5, 5)); Thread.Sleep(100); Assert.AreEqual(5, dict.Count); Assert.AreEqual(1, dict[1]); Assert.AreEqual(2, dict[2]); Assert.AreEqual(3, dict[3]); Assert.AreEqual(4, dict[4]); Assert.AreEqual(5, dict[5]); }
public void LockFreeDictionaryAdd() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); dict.Add(3, 3); dict.Add(4, 4); dict.Add(5, 5); Thread.Sleep(100); Assert.AreEqual(5, dict.Count); Assert.AreEqual(1, dict[1]); Assert.AreEqual(2, dict[2]); Assert.AreEqual(3, dict[3]); Assert.AreEqual(4, dict[4]); Assert.AreEqual(5, dict[5]); }
public void LockFreeDictionaryContainsKey() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); dict.Add(3, 3); dict.Add(4, 4); dict.Add(5, 5); Thread.Sleep(100); Assert.AreEqual(5, dict.Count); Assert.IsTrue(dict.ContainsKey(1)); Assert.IsTrue(dict.ContainsKey(2)); Assert.IsTrue(dict.ContainsKey(3)); Assert.IsTrue(dict.ContainsKey(4)); Assert.IsTrue(dict.ContainsKey(5)); }
public void LockFreeDictionaryEnumerator() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); dict.Add(3, 3); dict.Add(4, 4); dict.Add(5, 5); Thread.Sleep(100); int compare = 1; foreach (var item in dict) { var result = item.Value; Assert.AreEqual(compare, result); compare++; } }
public void LockFreeDictionaryRemoveKeyValuePair() { var dict = new AsyncLockFreeDictionary <int, int>(); Thread.Sleep(100); dict.Add(1, 1); dict.Add(2, 2); dict.Add(3, 3); dict.Add(4, 4); dict.Add(5, 5); Thread.Sleep(100); dict.Remove(new KeyValuePair <int, int>(4, 0)); Thread.Sleep(100); Assert.AreEqual(4, dict.Count); Assert.IsTrue(dict.ContainsKey(1)); Assert.IsTrue(dict.ContainsKey(2)); Assert.IsTrue(dict.ContainsKey(3)); Assert.IsTrue(dict.ContainsKey(5)); }