public void CacheCanCloseItemsEarly() { var resourceStore = new ResourceStore <string, Disposable>(name => new Disposable(name)); Disposable item1 = null; Disposable item2 = null; resourceStore.Access("foo", it => item1 = it); resourceStore.Access("bar", it => item2 = it); Assert.AreEqual(0, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); resourceStore.Close("foo"); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); Assert.Throws <ObjectDisposedException>(() => resourceStore.Access("foo", it => { })); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); }
public void CacheReturnsSameInstanceWhenCalledTwice() { var resourceStore = new ResourceStore<string, Disposable>(name => new Disposable(name)); Disposable firstItem = null; resourceStore.Access("foo", item => firstItem = item); Assert.AreEqual("foo", firstItem.Name); Disposable secondItem = null; resourceStore.Access("foo", item => secondItem = item); Assert.AreSame(firstItem, secondItem); }
public void CacheReturnsSameInstanceWhenCalledTwice() { var resourceStore = new ResourceStore <string, Disposable>(name => new Disposable(name)); Disposable firstItem = null; resourceStore.Access("foo", item => firstItem = item); Assert.AreEqual("foo", firstItem.Name); Disposable secondItem = null; resourceStore.Access("foo", item => secondItem = item); Assert.AreSame(firstItem, secondItem); }
private static int RunAccessesConcurrently(string[] keys) { var resourceStore = new ResourceStore <string, Disposable>(name => new Disposable(name)); var lck = new object(); var concurrentCallCount = 0; var maxCallCount = 0; var threads = new List <Thread>(); foreach (var key in keys) { var thisKey = key; var thread = new Thread(() => resourceStore.Access(thisKey, item => { lock (lck) { concurrentCallCount++; maxCallCount = Math.Max(maxCallCount, concurrentCallCount); } Thread.Sleep(250); lock (lck) { concurrentCallCount--; } })); thread.Start(); threads.Add(thread); } foreach (var thread in threads) { thread.Join(); } return(maxCallCount); }
private static int RunAccessesConcurrently(string[] keys) { var resourceStore = new ResourceStore<string, Disposable>(name => new Disposable(name)); var lck = new object(); var concurrentCallCount = 0; var maxCallCount = 0; var threads = new List<Thread>(); foreach (var key in keys) { var thisKey = key; var thread = new Thread(() => resourceStore.Access(thisKey, item => { lock (lck) { concurrentCallCount++; maxCallCount = Math.Max(maxCallCount, concurrentCallCount); } Thread.Sleep(250); lock (lck) { concurrentCallCount--; } })); thread.Start(); threads.Add(thread); } foreach (var thread in threads) { thread.Join(); } return maxCallCount; }
public void CacheCanCloseBeforeGet() { var resourceStore = new ResourceStore<string, Disposable>(name => new Disposable(name)); resourceStore.Close("foo"); Assert.Throws<ObjectDisposedException>(() => resourceStore.Access("foo", it => { })); }
public void CacheCanCloseBeforeGet() { var resourceStore = new ResourceStore <string, Disposable>(name => new Disposable(name)); resourceStore.Close("foo"); Assert.Throws <ObjectDisposedException>(() => resourceStore.Access("foo", it => { })); }
public void CacheDisposesAllItemsExactlyOnce() { var resourceStore = new ResourceStore <string, Disposable>(name => new Disposable(name)); Disposable item1 = null; Disposable item2 = null; resourceStore.Access("foo", it => item1 = it); resourceStore.Access("bar", it => item2 = it); Assert.AreEqual(0, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); }
public void CacheCanCloseItemsEarly() { var resourceStore = new ResourceStore<string, Disposable>(name => new Disposable(name)); Disposable item1 = null; Disposable item2 = null; resourceStore.Access("foo", it => item1 = it); resourceStore.Access("bar", it => item2 = it); Assert.AreEqual(0, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); resourceStore.Close("foo"); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); Assert.Throws<ObjectDisposedException>(() => resourceStore.Access("foo", it => { })); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); }
public void CacheDisposesAllItemsExactlyOnce() { var resourceStore = new ResourceStore<string, Disposable>(name => new Disposable(name)); Disposable item1 = null; Disposable item2 = null; resourceStore.Access("foo", it => item1 = it); resourceStore.Access("bar", it => item2 = it); Assert.AreEqual(0, item1.DisposeCallCount); Assert.AreEqual(0, item2.DisposeCallCount); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); resourceStore.Dispose(); Assert.AreEqual(1, item1.DisposeCallCount); Assert.AreEqual(1, item2.DisposeCallCount); }