public async void TryToAcquireLockAfterItIsReleased(int lockMilliseconds) { var timeout = TimeSpan.FromMilliseconds(lockMilliseconds * 10); var file = new FileInfo(FileLockTestPath.GetTempFileName()); var firstAcquireTask = await Helpers.AcquireLockAndReleaseAfterDelay(file, 0); using (var secondLock = new FileLockWithTimeout(file)) { var secondFileLock = await secondLock.TryAcquireOrTimeout(TimeSpan.FromMilliseconds(lockMilliseconds), timeout); Assert.True(secondFileLock); } Assert.True(firstAcquireTask); }
public async void LockShouldNotWaitTillTimeoutToBeAcquiredIfNotLocked(int lockSeconds) { var file = new FileInfo(FileLockTestPath.GetTempFileName()); var fileLock = new FileLockWithTimeout(file); using (fileLock) { var stopWatch = new Stopwatch(); stopWatch.Start(); await fileLock.TryAcquireOrTimeout(TimeSpan.FromSeconds(lockSeconds), TimeSpan.FromSeconds(lockSeconds)); stopWatch.Stop(); Assert.True(stopWatch.ElapsedMilliseconds < lockSeconds * 1000 / 2); } }
public async void TryToAcquireLockBeforeOfficialRelease(int lockMilliseconds) { var timeToRelease = lockMilliseconds / 4; var maximumTimeToWait = lockMilliseconds - FileLockWithTimeout.MinimumMilliseconds; var file = new FileInfo(FileLockTestPath.GetTempFileName()); var firstAcquireTask = Helpers.AcquireLockAndReleaseAfterDelay(file, timeToRelease); using (var secondLock = new FileLockWithTimeout(file)) { var secondLockTimeout = TimeSpan.FromMilliseconds(lockMilliseconds); var secondLockRetryMs = TimeSpan.FromMilliseconds(FileLockWithTimeout.MinimumMilliseconds); var timeoutMs = TimeSpan.FromMilliseconds(lockMilliseconds); var secondFileLock = secondLock.TryAcquireOrTimeout(secondLockTimeout, timeoutMs, secondLockRetryMs); Assert.True(await Task.WhenAny(secondFileLock, Task.Delay(maximumTimeToWait)) == secondFileLock); } Assert.True(await firstAcquireTask); }