public void Only_one_thread_can_acquire_the_lock_at_a_time() { var l = new Lock <int>(1); var trigger = new ManualResetEvent(false); var first = AsyncUtil.Fork(() => { trigger.WaitOne(); Thread.Sleep(200); l.Capture(new Result()).Wait(); }, new Result()); Result secondInternal = null; var second = AsyncUtil.Fork(() => { trigger.WaitOne(); secondInternal = l.Capture(new Result()); }, new Result()); trigger.Set(); Assert.IsFalse(first.HasValue); second.Wait(); l.Release(); Assert.IsTrue(secondInternal.HasValue); Assert.IsFalse(first.HasValue); secondInternal.Wait(); first.Wait(); Assert.IsTrue(first.HasValue); }
public void WhenDone_is_called_when_lock_is_acquired() { var l = new Lock <int>(1); var first = l.Capture(new Result()); var second = l.Capture(new Result()); Assert.IsTrue(first.HasValue); Assert.IsFalse(second.HasValue); var reset = new ManualResetEvent(false); second.WhenDone(_ => reset.Set()); l.Release(); Assert.IsTrue(reset.WaitOne(TimeSpan.FromSeconds(10), true)); Assert.IsTrue(second.HasValue); l.Release(); }