コード例 #1
0
        public void LockFileInfo_TryAcquire_Test()
        {
            var lockFilePath = Path.Combine(TempDirPath, Guid.NewGuid().ToString());

            using (var lockFile = LockFile.TryAcquire(lockFilePath))
            {
                Assert.That(lockFile, Is.Not.Null);
            }
        }
コード例 #2
0
        public void LockFileInfo_WaitAcquire_Cancellation_Test()
        {
            var lockFilePath = Path.Combine(TempDirPath, Guid.NewGuid().ToString());

            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(0.5)))
                using (LockFile.TryAcquire(lockFilePath))
                {
                    Assert.Throws <OperationCanceledException>(() =>
                                                               LockFile.WaitAcquire(lockFilePath, cts.Token));
                }
        }
コード例 #3
0
        private void EnsureLockFileAcquired()
        {
            // Ensure storage directory exists
            Directory.CreateDirectory(_storageDirPath);

            // Try to acquire lock file if it's not acquired yet
            _lockFile = _lockFile ?? LockFile.TryAcquire(_lockFilePath);

            // If failed to acquire - throw
            if (_lockFile == null)
            {
                throw new LockFileNotAcquiredException();
            }
        }
コード例 #4
0
        public void LockFileInfo_WaitAcquire_Test()
        {
            var lockFilePath = Path.Combine(TempDirPath, Guid.NewGuid().ToString());

            using (var originalLockFile = LockFile.TryAcquire(lockFilePath))
            {
                Task.Delay(TimeSpan.FromSeconds(0.5)).ContinueWith(_ => originalLockFile.Dispose());

                using (var newLockFile = LockFile.WaitAcquire(lockFilePath))
                {
                    Assert.That(newLockFile, Is.Not.Null);
                }
            }
        }