Exemple #1
0
        public void DeleteLockSwallowsExceptions()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

            fileSystem.When(x => x.DeleteFile(lockFilePath)).Do(x => { throw new Exception("failed to delete file"); });
            Assert.DoesNotThrow(() => lockIo.DeleteLock(lockFilePath));
        }
Exemple #2
0
        public void ReadLockReturnsOtherProcessHasExclusiveLockIfIoException()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => { throw new IOException("Sharing violation"); });
            var lockIo = new LockIo(fileSystem);
            var result = lockIo.ReadLock(lockFilePath);

            Assert.That(result, Is.InstanceOf <OtherProcessHasExclusiveLockOnFileLock>());
        }
Exemple #3
0
        public void ReadLockReturnsMissingFileLockIfFileNotFound()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => { throw new FileNotFoundException(); });
            var lockIo = new LockIo(fileSystem);
            var result = lockIo.ReadLock(lockFilePath);

            Assert.That(result, Is.InstanceOf <MissingFileLock>());
        }
Exemple #4
0
        public void WriteLockReturnsFalseIfUnknownException()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

            fileSystem.FileExists(lockFilePath).Returns(true);
            fileSystem.OpenFileExclusively(lockFilePath, Arg.Any <FileMode>(), Arg.Any <FileAccess>())
            .Returns(x => { throw new Exception("Unknown exception"); });
            var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            var result         = lockIo.WriteLock(lockFilePath, new FileLock(currentProcess.Id, currentProcess.ProcessName, Thread.CurrentThread.ManagedThreadId, 636114372739676700));

            Assert.That(result, Is.False);
        }
Exemple #5
0
        public void ReadLockReturnsOtherProcessHasExclusiveLockIfUnknownException()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

#if NET40
            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => { throw new ApplicationException(); });
#else
            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => { throw new Exception(); });
#endif
            var result = lockIo.ReadLock(lockFilePath);
            Assert.That(result, Is.InstanceOf <OtherProcessHasExclusiveLockOnFileLock>());
        }
Exemple #6
0
        public void WriteLockDoesNotOverwriteLockFileIfItsIdenticalToWhatWeAreWantingToWrite()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

            fileSystem.FileExists(lockFilePath).Returns(true);
            var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            var fileContent    = $"{{\"__type\":\"FileLock:#Calamari.Integration.Processes.Semaphores\",\"ProcessId\":{currentProcess.Id},\"ProcessName\":\"{currentProcess.ProcessName}\",\"ThreadId\":{Thread.CurrentThread.ManagedThreadId},\"Timestamp\":636114372739676700}}";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => new MemoryStream(Encoding.UTF8.GetBytes(fileContent)));
            var lockFile = lockIo.ReadLock(lockFilePath);

            lockIo.WriteLock(lockFilePath, lockFile);
        }
Exemple #7
0
        public void ReadLockReturnsUnableToDeserialiseWhenDeserialisationFails()
        {
            var fileSystem       = Substitute.For <ICalamariFileSystem>();
            var lockFilePath     = "fake path";
            var fileCreationTime = DateTime.Now;

            fileSystem.GetCreationTime(lockFilePath).Returns(fileCreationTime);

            var lockIo = new LockIo(fileSystem);

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => { throw new JsonReaderException(); });
            var result = lockIo.ReadLock(lockFilePath);

            Assert.That(result, Is.InstanceOf <UnableToDeserialiseLockFile>());
            Assert.That(((UnableToDeserialiseLockFile)result).CreationTime, Is.EqualTo(fileCreationTime));
        }
Exemple #8
0
        public void ReadLockReturnsLockDetailsIfLockBelongsToUs()
        {
            var fileSystem     = Substitute.For <ICalamariFileSystem>();
            var lockFilePath   = "fake path";
            var lockIo         = new LockIo(fileSystem);
            var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            var fileContent    = $"{{\"__type\":\"FileLock:#Calamari.Integration.Processes.Semaphores\",\"ProcessId\":{currentProcess.Id},\"ProcessName\":\"{currentProcess.ProcessName}\",\"ThreadId\":{Thread.CurrentThread.ManagedThreadId},\"Timestamp\":636114372739676700}}";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => new MemoryStream(Encoding.UTF8.GetBytes(fileContent)));
            var result = lockIo.ReadLock(lockFilePath);

            Assert.That(result, Is.InstanceOf <FileLock>());
            Assert.That(result.ProcessId, Is.EqualTo(currentProcess.Id));
            Assert.That(result.ProcessName, Is.EqualTo(currentProcess.ProcessName));
            Assert.That(result.ThreadId, Is.EqualTo(Thread.CurrentThread.ManagedThreadId));
            Assert.That(result.Timestamp, Is.EqualTo(636114372739676700));
        }
Exemple #9
0
        public void WriteLockDeletesLockIfUnableToDeserialise()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

            fileSystem.FileExists(lockFilePath).Returns(true);
            var currentProcess      = System.Diagnostics.Process.GetCurrentProcess();
            var expectedFileContent = $"{{\"__type\":\"FileLock:#Calamari.Integration.Processes.Semaphores\",\"ProcessId\":{currentProcess.Id},\"ProcessName\":\"{currentProcess.ProcessName}\",\"ThreadId\":{Thread.CurrentThread.ManagedThreadId},\"Timestamp\":636114372739676700}}";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => new MemoryStream(Encoding.UTF8.GetBytes("non deserialisable content")), x => new MemoryStream(Encoding.UTF8.GetBytes(expectedFileContent)));
            fileSystem.OpenFileExclusively(lockFilePath, FileMode.CreateNew, FileAccess.Write)
            .Returns(x => new MemoryStream());
            var result = lockIo.WriteLock(lockFilePath, new FileLock(currentProcess.Id, currentProcess.ProcessName, Thread.CurrentThread.ManagedThreadId, 636114372739676700));

            fileSystem.Received().DeleteFile(lockFilePath);
            Assert.That(result, Is.True);
        }
Exemple #10
0
        public void WriteLockOverwritesLockFileIfTimestampIsDifferent()
        {
            var fileSystem   = Substitute.For <ICalamariFileSystem>();
            var lockFilePath = "fake path";
            var lockIo       = new LockIo(fileSystem);

            fileSystem.FileExists(lockFilePath).Returns(true);
            var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            var fileContent    = $"{{\"__type\":\"FileLock:#Calamari.Integration.Processes.Semaphores\",\"ProcessId\":{currentProcess.Id},\"ProcessName\":\"{currentProcess.ProcessName}\",\"ThreadId\":{Thread.CurrentThread.ManagedThreadId},\"Timestamp\":636114372739676700}}";

            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read)
            .Returns(x => new MemoryStream(Encoding.UTF8.GetBytes(fileContent)));
            var lockFile = (FileLock)lockIo.ReadLock(lockFilePath);

            lockFile.Timestamp = lockFile.Timestamp + 1;
            fileSystem.OpenFileExclusively(lockFilePath, FileMode.Create, FileAccess.Write)
            .Returns(x => new MemoryStream());
            var result = lockIo.WriteLock(lockFilePath, lockFile);

            Assert.That(result, Is.True);
        }