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());
 }
Пример #2
0
    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());
 }
Пример #4
0
    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());
    }