Beispiel #1
0
        public void WaitOneWithTimeoutTimesOut()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new FileLock
            {
                ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id,
                ThreadId  = Thread.CurrentThread.ManagedThreadId
            };

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            //failed to write the lock file (ie, someone else got in first)
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(false);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = semaphore.WaitOne(300);

            Assert.That(result, Is.False);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.GreaterThan(300));
        }
Beispiel #2
0
        public void DoesNotAttemptToAquireLockIfWeCantDeserialise()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new UnableToDeserialiseLockFile(DateTime.Now);

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AquireLockAction.DontAquireLock));
        }
Beispiel #3
0
        public void DoesNotAttemptToAquireLockIfLockFileIsLockedByOtherProcess()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new OtherProcessHasExclusiveLockOnFileLock();

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AquireLockAction.DontAquireLock));
        }
        public void AttemptsToAcquireLockIfItBelongsToUs()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>(), new InMemoryLog());
            var fileLock  = new FileLock(System.Diagnostics.Process.GetCurrentProcess().Id, Guid.NewGuid().ToString(), Thread.CurrentThread.ManagedThreadId, DateTime.Now.Ticks);

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAcquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AcquireLockAction.AcquireLock));
        }
Beispiel #5
0
        public void AttemptsToAquireLockIfLockFileDoesNotExist()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new FileLock();

            lockIo.LockExists(Arg.Any <string>()).Returns(false);
            var result = semaphore.ShouldAquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AquireLockAction.AquireLock));
        }
        public void CanReleaseLockIfWeCanAcquireIt()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>(), new InMemoryLog());
            var fileLock  = new FileLock(System.Diagnostics.Process.GetCurrentProcess().Id, Guid.NewGuid().ToString(), Thread.CurrentThread.ManagedThreadId, DateTime.Now.Ticks);

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(true);
            semaphore.ReleaseLock();
            lockIo.Received().DeleteLock(Arg.Any <string>());
        }
Beispiel #7
0
        public void AttemptsToAquireLockIfWeCantFindLockFile()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new MissingFileLock();

            //when we check for the lock file, it exists
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            //but when we read it, its been released
            var result = semaphore.ShouldAquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AquireLockAction.AquireLock));
        }
Beispiel #8
0
        public void AttemptsToAquireLockIfWeCantDeserialiseButFileIsOlderThanLockTimeout()
        {
            var log       = new InMemoryLog();
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>(), log);
            var fileLock  = new UnableToDeserialiseLockFile(DateTime.Now.Subtract(TimeSpan.FromMinutes(5)));

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AquireLockAction.AquireLock));
            log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Warn && m.FormattedMessage == "Lock file existed but was not readable, and has existed for longer than lock timeout. Taking lock.");
        }
        public void DoesNotAttemptToAcquireLockIfOwnedBySomeoneElseAndLockHasNotTimedOut()
        {
            var lockIo        = Substitute.For <ILockIo>();
            var name          = Guid.NewGuid().ToString();
            var processFinder = Substitute.For <IProcessFinder>();

            processFinder.ProcessIsRunning(Arg.Any <int>(), Arg.Any <string>()).Returns(true);
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, processFinder, new InMemoryLog());
            var fileLock  = new FileLock(0, Guid.NewGuid().ToString(), 0, DateTime.Now.Ticks);

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAcquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AcquireLockAction.DontAcquireLock));
        }
        public void AttemptsToForciblyAcquireLockIfOwnedBySomeoneElseAndLockHasTimedOut()
        {
            var log           = new InMemoryLog();
            var lockIo        = Substitute.For <ILockIo>();
            var name          = Guid.NewGuid().ToString();
            var processFinder = Substitute.For <IProcessFinder>();

            processFinder.ProcessIsRunning(Arg.Any <int>(), Arg.Any <string>()).Returns(true);
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, processFinder, log);
            var fileLock  = new FileLock(0, Guid.NewGuid().ToString(), 0, DateTime.Now.Subtract(TimeSpan.FromMinutes(5)).Ticks);

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAcquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AcquireLockAction.ForciblyAcquireLock));
            log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Warn && m.FormattedMessage == "Forcibly taking lock from process 0, thread 0 as lock has timed out. If this happens regularly, please contact Octopus Support.");
        }
        public void AttemptsToAcquireLockIfOtherProcessIsNoLongerRunning()
        {
            var log           = new InMemoryLog();
            var lockIo        = Substitute.For <ILockIo>();
            var name          = Guid.NewGuid().ToString();
            var processFinder = Substitute.For <IProcessFinder>();

            processFinder.ProcessIsRunning(Arg.Any <int>(), Arg.Any <string>()).Returns(false);
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, processFinder, log);
            var fileLock  = new FileLock(-1, Guid.NewGuid().ToString(), -2, DateTime.Now.Ticks);

            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            var result = semaphore.ShouldAcquireLock(fileLock);

            Assert.That(result, Is.EqualTo(LockFileBasedSemaphore.AcquireLockAction.AcquireLock));
            log.Messages.Should().Contain(m => m.Level == InMemoryLog.Level.Warn && m.FormattedMessage == "Process -1, thread -2 had lock, but appears to have crashed. Taking lock.");
        }
Beispiel #12
0
        public void CannotReleaseLockIfWeCannotAquireIt()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new FileLock
            {
                ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id,
                ThreadId  = Thread.CurrentThread.ManagedThreadId
            };

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            //failed to write the lock file (ie, someone else got in first)
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(false);
            semaphore.ReleaseLock();
            lockIo.DidNotReceive().DeleteLock(Arg.Any <string>());
        }
        public void WritesLockFile()
        {
            var lockIo        = Substitute.For <ILockIo>();
            var name          = Guid.NewGuid().ToString();
            var processFinder = Substitute.For <IProcessFinder>();

            processFinder.ProcessIsRunning(Arg.Any <int>(), Arg.Any <string>()).Returns(true);
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, processFinder, new InMemoryLog());
            var fileLock  = new FileLock(System.Diagnostics.Process.GetCurrentProcess().Id, Guid.NewGuid().ToString(), Thread.CurrentThread.ManagedThreadId, DateTime.Now.Ticks);

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(true);
            var result = semaphore.TryAcquireLock();

            Assert.That(result, Is.True);
            lockIo.DidNotReceive().DeleteLock(Arg.Any <string>());
            lockIo.Received().WriteLock(Arg.Any <string>(), Arg.Any <FileLock>());
        }
        public void WaitOneReturnsAfterAquiringLock()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>(), new InMemoryLog());
            var fileLock  = new FileLock(System.Diagnostics.Process.GetCurrentProcess().Id, Guid.NewGuid().ToString(), Thread.CurrentThread.ManagedThreadId, DateTime.Now.Ticks);

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(false, false, true);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = semaphore.WaitOne();

            Assert.That(result, Is.True);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.GreaterThan(175));
        }
        public void DeletesLockIfOwnedBySomeoneElseAndLockHasTimedOut()
        {
            var lockIo        = Substitute.For <ILockIo>();
            var name          = Guid.NewGuid().ToString();
            var processFinder = Substitute.For <IProcessFinder>();

            processFinder.ProcessIsRunning(Arg.Any <int>(), Arg.Any <string>()).Returns(true);
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, processFinder, new InMemoryLog());
            //not setting processid/threadid, therefore its someone elses
            var fileLock = new FileLock(0, Guid.NewGuid().ToString(), 0, DateTime.Now.Subtract(TimeSpan.FromMinutes(5)).Ticks);

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(true);
            var result = semaphore.TryAcquireLock();

            Assert.That(result, Is.True);
            lockIo.Received().DeleteLock(Arg.Any <string>());
            lockIo.Received().WriteLock(Arg.Any <string>(), Arg.Any <FileLock>());
        }
Beispiel #16
0
        public void WaitOneWithTimeoutDoesNotWaitIfCanAquireLock()
        {
            var lockIo    = Substitute.For <ILockIo>();
            var name      = Guid.NewGuid().ToString();
            var semaphore = new LockFileBasedSemaphore(name, TimeSpan.FromSeconds(30), lockIo, Substitute.For <IProcessFinder>());
            var fileLock  = new FileLock
            {
                ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id,
                ThreadId  = Thread.CurrentThread.ManagedThreadId
            };

            lockIo.ReadLock(Arg.Any <string>()).Returns(fileLock);
            lockIo.LockExists(Arg.Any <string>()).Returns(true);
            lockIo.WriteLock(Arg.Any <string>(), Arg.Any <FileLock>()).Returns(true);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = semaphore.WaitOne(300);

            Assert.That(result, Is.True);
            Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(100));
        }