コード例 #1
0
        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);
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
        }