public async Task LockAcquisitionResultContainsInformationAboutOtherProcess() { string componentName = "component1"; var context = new Context(Logger); var timeout = TimeSpan.FromMilliseconds(100); using (var testDirectory = new DisposableDirectory(FileSystem)) using (var lock1 = new DirectoryLock(testDirectory.Path / "lock1", FileSystem, TimeSpan.FromMinutes(30), componentName)) using (var lock2 = new DirectoryLock(testDirectory.Path / "lock1", FileSystem, timeout, componentName)) { (await lock1.AcquireAsync(context)).LockAcquired.Should().BeTrue(); var failure = await lock2.AcquireAsync(context); failure.LockAcquired.Should().BeFalse(); failure.Timeout.Should().Be(timeout); // Fix for InMemoryFileSystem. Bug #1334691 if (!BuildXL.Utilities.OperatingSystemHelper.IsUnixOS && FileSystem is PassThroughFileSystem) { // Used to be a flaky check. Enable it because it is important. failure.CompetingProcessId.HasValue.Should().BeTrue(); Assert.NotNull(failure.CompetingProcessName); } } }
public async Task SameBlocks(string componentName) { var context = new Context(Logger); using (var testDirectory = new DisposableDirectory(FileSystem)) using (var lock1 = new DirectoryLock(testDirectory.Path / "dir1", FileSystem, TimeSpan.FromMinutes(30), componentName)) using (var lock2 = new DirectoryLock(testDirectory.Path / "dir1", FileSystem, TimeSpan.FromMilliseconds(100), componentName)) { (await lock1.AcquireAsync(context)).LockAcquired.Should().BeTrue(); (await lock2.AcquireAsync(context)).LockAcquired.Should().BeFalse(); } }
[InlineData("dir1", "dir2", "TestComponent1", null)] // Different paths, one null component public async Task DifferentNoBlocking(string directoryName1, string directoryName2, string componentName1, string componentName2) { var context = new Context(Logger); using (var testDirectory = new DisposableDirectory(FileSystem)) using (var lock1 = new DirectoryLock(testDirectory.Path / directoryName1, FileSystem, TimeSpan.FromMinutes(30), componentName1)) using (var lock2 = new DirectoryLock(testDirectory.Path / directoryName2, FileSystem, TimeSpan.FromMinutes(30), componentName2)) { (await lock1.AcquireAsync(context)).LockAcquired.Should().BeTrue(); (await lock2.AcquireAsync(context)).LockAcquired.Should().BeTrue(); } }
/// <inheritdoc /> public async Task <BoolResult> AcquireDirectoryLockAsync(Context context) { var aquisitingResult = await _directoryLock.AcquireAsync(context).ConfigureAwait(false); if (aquisitingResult.LockAcquired) { return(BoolResult.Success); } var errorMessage = aquisitingResult.GetErrorMessage(Component); return(new BoolResult(errorMessage)); }
public async Task HoldingLockFileDoesNotWedge(string componentName) { var context = new Context(Logger); using (var testDirectory = new DisposableDirectory(FileSystem)) using (var lockFile = new DirectoryLockFile(FileSystem, testDirectory.Path / "dir1" / $"{componentName ?? string.Empty}.lock", TimeSpan.FromMilliseconds(100))) using (var lock1 = new DirectoryLock(testDirectory.Path / "dir1", FileSystem, TimeSpan.FromSeconds(1), componentName)) using (var lock2 = new DirectoryLock(testDirectory.Path / "dir1", FileSystem, TimeSpan.FromSeconds(1), componentName)) { (await lockFile.AcquireAsync(context, TimeSpan.FromMinutes(1))).LockAcquired.Should().BeTrue(); (await lock1.AcquireAsync(context)).LockAcquired.Should().BeFalse(); lockFile.Dispose(); (await lock2.AcquireAsync(context)).LockAcquired.Should().BeTrue(); } }
public async Task SameObjectAcquiresMultipleTimes(string componentName) { var context = new Context(Logger); using (var testDirectory = new DisposableDirectory(FileSystem)) using (var lock1 = new DirectoryLock(testDirectory.Path / "dir1", FileSystem, TimeSpan.FromSeconds(1), componentName)) { var failedToAcquire = false; var actionBlock = new ActionBlock <ValueUnit>(async _ => { if (!(await lock1.AcquireAsync(context)).LockAcquired) { failedToAcquire = true; } }); await actionBlock.PostAllAndComplete(Enumerable.Repeat(ValueUnit.Void, 5)); failedToAcquire.Should().BeFalse(); } }