Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created February 4, 2014 10:14
Show Gist options
  • Select an option

  • Save joeriks/8801079 to your computer and use it in GitHub Desktop.

Select an option

Save joeriks/8801079 to your computer and use it in GitHub Desktop.
Don't forget to test parallell actions
/* 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