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)); }
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>()); }
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>()); }
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); }
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>()); }
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); }
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)); }
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)); }
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); }
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); }