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());
     }
 }
Пример #3
0
        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);
        }
Пример #4
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();
        }
Пример #5
0
        public async Task All_primitives_should_be_configurable()
        {
            var name           = "Example";
            var maxReaderCount = TinyReadWriteLock.DefaultMaxReaderCount;
            var maxFileSize    = TinyMemoryMappedFile.DefaultMaxFileSize;
            var waitTimeout    = TinyReadWriteLock.DefaultWaitTimeout;

            // Create underlying primitives first so they can be configured
            using var lockMutex        = TinyReadWriteLock.CreateMutex(name);
            using var lockSemaphore    = TinyReadWriteLock.CreateSemaphore(name, maxReaderCount);
            using var memoryMappedFile = TinyMemoryMappedFile.CreateOrOpenMemoryMappedFile(name, maxFileSize);
            using var eventWaitHandle  = TinyMemoryMappedFile.CreateEventWaitHandle(name);

            // Create the actual message bus
            using var tinyReadWriteLock    = new TinyReadWriteLock(lockMutex, lockSemaphore, maxReaderCount, waitTimeout);
            using var tinyMemoryMappedFile = new TinyMemoryMappedFile(memoryMappedFile, eventWaitHandle, maxFileSize, tinyReadWriteLock, disposeLock: true);

            using var messageBus = new TinyMessageBus(tinyMemoryMappedFile, disposeFile: true);
            await messageBus.PublishAsync(Encoding.UTF8.GetBytes("lorem"));

            await messageBus.PublishAsync(Encoding.UTF8.GetBytes("ipsum"));
        }
Пример #6
0
        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);
                }
        }
Пример #7
0
        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);
                }
        }
Пример #8
0
        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);
        }