Created
February 4, 2014 10:14
-
-
Save joeriks/8801079 to your computer and use it in GitHub Desktop.
Don't forget to test parallell actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Lock dictionary between read and write */ | |
| public T Do(string key, Func<T,T> func){ | |
| lock (_dictionary) { | |
| var originalValue = get<T>(key); | |
| var newValue = func(originalValue); | |
| set(key, newValue); | |
| return newValue; | |
| } | |
| } | |
| /* Test for parallell actions on dictionary */ | |
| public void TestAdd1() | |
| { | |
| var s = EsentKeyValue.GetStore<int>("bar"); | |
| s.Do("counter", t => t + 1); | |
| } | |
| [TestMethod] | |
| public void ParallellTest() | |
| { | |
| var s = EsentKeyValue.GetStore<int>("storename"); | |
| s.Set("counter", 0); | |
| Parallel.Invoke( | |
| () => TestAdd1(), | |
| () => TestAdd1(), | |
| () => TestAdd1()); | |
| var result = s.Get("counter"); | |
| Assert.AreEqual(3, result); /* if I don't lock the dictionary the I can get 1,2 or 3 */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment