Esempio n. 1
0
        public void Calling_ReleaseReadLock_should_release_lock()
        {
            using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1);

            readWriteLock.AcquireReadLock();
            readWriteLock.IsReaderLockHeld.ShouldBeTrue();

            readWriteLock.ReleaseReadLock();
            readWriteLock.IsReaderLockHeld.ShouldBeFalse();
        }
Esempio n. 2
0
        public void Calling_AcquireReadLock_then_AquireWriteLock_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.AcquireReadLock();

            var writeLockTask = Task.Run(() => readWriteLock2.AcquireWriteLock());

            WaitForTaskToStart(writeLockTask);

            readWriteLock1.IsReaderLockHeld.ShouldBeTrue();
            readWriteLock2.IsWriterLockHeld.ShouldBeFalse();

            readWriteLock1.ReleaseReadLock();

            writeLockTask.Wait();

            readWriteLock1.IsReaderLockHeld.ShouldBeFalse();
            readWriteLock2.IsWriterLockHeld.ShouldBeTrue();
        }
Esempio n. 3
0
        public void Calling_ReleaseReadLock_without_any_lock_held_should_throw()
        {
            using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1);

            Should.Throw <SemaphoreFullException>(() => readWriteLock.ReleaseReadLock());
        }