public void TryAcquire() { // TryAcquired acquires the lock once var r = new OnetimeLock(); Assert.IsTrue(r.TryAcquire()); Assert.IsTrue(!r.TryAcquire()); Assert.IsTrue(!r.TryAcquire()); }
public void IsAcquired() { // IsAcquired determines if the lock is acquired var r = new OnetimeLock(); Assert.IsTrue(!r.IsAcquired()); Assert.IsTrue(r.TryAcquire()); Assert.IsTrue(r.IsAcquired()); }
public void TryAcquire_Race() { // racing TryAcquires: only one wins var r = new OnetimeLock(); var x = Task.WhenAll( Enumerable.Range(0, 10).Select( e => Task.Factory.StartNew( () => r.TryAcquire(), TaskCreationOptions.LongRunning))).AssertRanToCompletion(); Assert.IsTrue(x.Where(e => e).Count() == 1); Assert.IsTrue(r.IsAcquired()); }