public void WriteLock_should_be_exclusive() { var lockId = Guid.NewGuid().ToString(); using (var readWriteLock1 = new TinyReadWriteLock(lockId, 2, TimeSpan.FromMilliseconds(0))) using (var readWriteLock2 = new TinyReadWriteLock(lockId, 2, TimeSpan.FromMilliseconds(0))) { // Aquire the first lock readWriteLock1.AcquireWriteLock(); // The second lock should now throw TimeoutException Assert.Throws <TimeoutException>(() => readWriteLock2.AcquireWriteLock()); // Make sure the expected locks are held Assert.True(readWriteLock1.IsWriterLockHeld); Assert.False(readWriteLock2.IsWriterLockHeld); // By releasing the first lock, the second lock should now be able to be held readWriteLock1.ReleaseWriteLock(); readWriteLock2.AcquireWriteLock(); // Make sure the expected locks are held Assert.False(readWriteLock1.IsWriterLockHeld); Assert.True(readWriteLock2.IsWriterLockHeld); } }
public void Calling_ReleaseWriteLock_without_any_lock_held_should_throw() { using (var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1)) { Assert.Throws <SemaphoreFullException>(() => readWriteLock.ReleaseWriteLock()); } }
public void Calling_ReleaseWriteLock_should_release_locks() { using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 2); readWriteLock.AcquireWriteLock(); Assert.True(readWriteLock.IsWriterLockHeld); readWriteLock.ReleaseWriteLock(); Assert.False(readWriteLock.IsWriterLockHeld); }
public void Calling_AcquireWriteLock_then_AquireReadLock_should_wait_for_other_lock() { var lockId = Guid.NewGuid().ToString(); using (var readWriteLock1 = new TinyReadWriteLock(lockId, 2)) using (var readWriteLock2 = new TinyReadWriteLock(lockId, 2)) { readWriteLock1.AcquireWriteLock(); Task.Factory.StartNew(() => readWriteLock2.AcquireReadLock()); Thread.Sleep(50); Assert.True(readWriteLock1.IsWriterLockHeld); Assert.False(readWriteLock2.IsReaderLockHeld); readWriteLock1.ReleaseWriteLock(); Thread.Sleep(50); Assert.False(readWriteLock1.IsWriterLockHeld); Assert.True(readWriteLock2.IsReaderLockHeld); } }
public void WriteLock_should_be_exclusive() { var lockId = Guid.NewGuid().ToString(); using (var readWriteLock1 = new TinyReadWriteLock(lockId, 2)) using (var readWriteLock2 = new TinyReadWriteLock(lockId, 2)) { readWriteLock1.AcquireWriteLock(); Task.Factory.StartNew(() => readWriteLock2.AcquireWriteLock()); Thread.Sleep(10); Assert.True(readWriteLock1.IsWriterLockHeld); Assert.False(readWriteLock2.IsWriterLockHeld); readWriteLock1.ReleaseWriteLock(); Thread.Sleep(10); Assert.False(readWriteLock1.IsWriterLockHeld); Assert.True(readWriteLock2.IsWriterLockHeld); } }
public void Calling_AcquireWriteLock_then_AquireReadLock_should_wait_for_other_lock() { var lockId = Guid.NewGuid().ToString(); using var readWriteLock1 = new TinyReadWriteLock(lockId, 2); using var readWriteLock2 = new TinyReadWriteLock(lockId, 2); readWriteLock1.AcquireWriteLock(); var readLockTask = Task.Run(() => readWriteLock2.AcquireReadLock()); WaitForTaskToStart(readLockTask); Assert.True(readWriteLock1.IsWriterLockHeld); Assert.False(readWriteLock2.IsReaderLockHeld); readWriteLock1.ReleaseWriteLock(); readLockTask.Wait(); Assert.False(readWriteLock1.IsWriterLockHeld); Assert.True(readWriteLock2.IsReaderLockHeld); }