public void Calling_ReleaseReadLock_should_release_lock() { using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1); readWriteLock.AcquireReadLock(); Assert.True(readWriteLock.IsReaderLockHeld); readWriteLock.ReleaseReadLock(); Assert.False(readWriteLock.IsReaderLockHeld); }
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 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); }