Exemplo n.º 1
0
        public void ReleasesLockOnDispose()
        {
            var semaphoreCreator = Substitute.For <ICreateSemaphores>();
            var name             = Guid.NewGuid().ToString();
            var semaphore        = Substitute.For <ISemaphore>();

            semaphoreCreator.Create(name, TimeSpan.FromMinutes(2)).Returns(semaphore);
            var sempahoreManager = new FileBasedSempahoreManager(Substitute.For <ILog>(), TimeSpan.FromMilliseconds(200), semaphoreCreator);

            using (sempahoreManager.Acquire(name, "wait message")) { }
            semaphore.Received().ReleaseLock();
        }
Exemplo n.º 2
0
        public void AttemptsToGetLockWithTimeoutThenWaitsIndefinitely()
        {
            var semaphoreCreator = Substitute.For <ICreateSemaphores>();
            var name             = Guid.NewGuid().ToString();
            var semaphore        = Substitute.For <ISemaphore>();

            semaphore.WaitOne(200).Returns(false);
            semaphoreCreator.Create(name, TimeSpan.FromMinutes(2)).Returns(semaphore);
            var sempahoreManager = new FileBasedSempahoreManager(Substitute.For <ILog>(), TimeSpan.FromMilliseconds(200), semaphoreCreator);

            using (sempahoreManager.Acquire(name, "wait message")) { }
            semaphore.Received().WaitOne(200);
            semaphore.Received().WaitOne();
        }
Exemplo n.º 3
0
        public void WritesMessageToLogIfCannotGetSemaphore()
        {
            var log = new InMemoryLog();
            var semaphoreCreator = Substitute.For <ICreateSemaphores>();
            var name             = Guid.NewGuid().ToString();
            var semaphore        = Substitute.For <ISemaphore>();

            semaphore.WaitOne(200).Returns(false);
            semaphoreCreator.Create(name, TimeSpan.FromSeconds(60)).Returns(semaphore);
            var sempahoreManager = new FileBasedSempahoreManager(log, TimeSpan.FromMilliseconds(200), semaphoreCreator);

            using (sempahoreManager.Acquire(name, "wait message")) { }

            log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Verbose && m.FormattedMessage == "wait message");
        }