public void TestTakeItemTimeoutFreed() { using (var cache = new DisposableCache <IDisposable>(1)) { var item = new Mock <IDisposable>().Object; cache.CacheItem(item); var delay = TimeSpan.FromMilliseconds(50); var timeout = TimeSpan.FromMilliseconds(5000); // return the first item after a delay var handle1 = cache.TakeItem(); Task.Run(() => { using (handle1) { Task.Delay(delay).Wait(); } }); // take a second item and verify it succeeds after the delay has passed var stopwatch = Stopwatch.StartNew(); using (var handle2 = cache.TakeItem(timeout)) { stopwatch.Stop(); Assert.IsTrue(stopwatch.Elapsed >= delay); } } }
public void TestTakeItemTimeout() { using (var cache = new DisposableCache <IDisposable>(1)) { var item = new Mock <IDisposable>().Object; cache.CacheItem(item); using (var handle1 = cache.TakeItem()) { var timeout = TimeSpan.FromMilliseconds(250); var stopwatch = Stopwatch.StartNew(); try { using (var handle2 = cache.TakeItem(timeout)) { } Assert.Fail(); } catch (TimeoutException) { /*expected*/ } stopwatch.Stop(); // verify the timout period was respected before the exception was thrown Assert.IsTrue(stopwatch.Elapsed >= timeout); } } }
public void TestTakeItemExhausted() { using (var cache = new DisposableCache <IDisposable>(1)) { var item = new Mock <IDisposable>().Object; cache.CacheItem(item); using (var handle1 = cache.TakeItem()) { using (var handle2 = cache.TakeItem()) { } } } }
public void TestTakeItemExhausted() { using (var cache = new DisposableCache <IDisposable>(1)) using (var taker1Taken = new AutoResetEvent(false)) using (var taker1Finish = new AutoResetEvent(false)) using (var taker2Started = new AutoResetEvent(false)) using (var taker2Taken = new AutoResetEvent(false)) { var item = new Mock <IDisposable>().Object; cache.CacheItem(item); var taker1 = Task.Run(() => { using (var handle1 = cache.TakeItem()) { taker1Taken.Set(); taker1Finish.WaitOne(); } }); // wait for taker 1 to retrieve a cached item taker1Taken.WaitOne(); var taker2 = Task.Run(() => { taker2Started.Set(); using (var handle2 = cache.TakeItem()) { taker2Taken.Set(); } }); // wait for taker 2 to start Assert.IsTrue(taker2Started.WaitOne(2000)); // verify taker 2 hasn't been able to retrieve a cached item Assert.IsFalse(taker2Taken.WaitOne(500)); // allow taker 1 to return its cached item taker1Finish.Set(); Assert.IsTrue(taker1.Wait(2000)); // verify taker 2 then retrieves a cached item Assert.IsTrue(taker2Taken.WaitOne(2000)); Assert.IsTrue(taker2.Wait(2000)); } }
public void TestTakeItemExhausted() { using (var cache = new DisposableCache<IDisposable>(1)) using (var taker1Taken = new AutoResetEvent(false)) using (var taker1Finish = new AutoResetEvent(false)) using (var taker2Started = new AutoResetEvent(false)) using (var taker2Taken = new AutoResetEvent(false)) { var item = new Mock<IDisposable>().Object; cache.CacheItem(item); var taker1 = Task.Run(() => { using (var handle1 = cache.TakeItem()) { taker1Taken.Set(); taker1Finish.WaitOne(); } }); // wait for taker 1 to retrieve a cached item taker1Taken.WaitOne(); var taker2 = Task.Run(() => { taker2Started.Set(); using (var handle2 = cache.TakeItem()) { taker2Taken.Set(); } }); // wait for taker 2 to start Assert.IsTrue(taker2Started.WaitOne(2000)); // verify taker 2 hasn't been able to retrieve a cached item Assert.IsFalse(taker2Taken.WaitOne(500)); // allow taker 1 to return its cached item taker1Finish.Set(); Assert.IsTrue(taker1.Wait(2000)); // verify taker 2 then retrieves a cached item Assert.IsTrue(taker2Taken.WaitOne(2000)); Assert.IsTrue(taker2.Wait(2000)); } }
public void TestTakeItemTimeoutFreed() { using (var cache = new DisposableCache<IDisposable>(1)) { var item = new Mock<IDisposable>().Object; cache.CacheItem(item); var delay = TimeSpan.FromMilliseconds(50); var timeout = TimeSpan.FromMilliseconds(5000); // return the first item after a delay var handle1 = cache.TakeItem(); Task.Run(() => { using (handle1) { Task.Delay(delay).Wait(); } }); // take a second item and verify it succeeds after the delay has passed var stopwatch = Stopwatch.StartNew(); using (var handle2 = cache.TakeItem(timeout)) { stopwatch.Stop(); Assert.IsTrue(stopwatch.Elapsed >= delay); } } }
public void TestTakeItemTimeout() { using (var cache = new DisposableCache<IDisposable>(1)) { var item = new Mock<IDisposable>().Object; cache.CacheItem(item); using (var handle1 = cache.TakeItem()) { var timeout = TimeSpan.FromMilliseconds(250); var stopwatch = Stopwatch.StartNew(); try { using (var handle2 = cache.TakeItem(timeout)) { } Assert.Fail(); } catch (TimeoutException) { /*expected*/ } stopwatch.Stop(); // verify the timout period was respected before the exception was thrown Assert.IsTrue(stopwatch.Elapsed >= timeout); } } }