예제 #1
0
        public async Task AcquireReleaseLockAsync()
        {
            string sentinelPath = $"{Path.GetTempPath()}\\{Guid.NewGuid():N}";

            IClock clock           = new RealClock();
            var    holdLockTimeout = TimeSpan.FromMilliseconds(250);
            var    lockTimeout     = TimeSpan.FromMilliseconds(2000);

            // Lock the sentinel file.
            var lockFileTask = Task.Run(async() =>
            {
                using (var stream = File.Open(
                           sentinelPath,
                           FileMode.CreateNew,
                           FileAccess.ReadWrite,
                           FileShare.Write))
                {
                    await Task.Delay(holdLockTimeout);

                    stream.Close();

                    File.Delete(sentinelPath);
                }
            });

            bool acquiredLock = false;

            // Wait for a lock on the sentinel file.
            var acquiredLockTask = FileHelper.AcquireFileLockAsync(
                sentinelPath,
                clock,
                lockTimeout);

            try
            {
                await lockFileTask;
                acquiredLock = await acquiredLockTask;
            }
            finally
            {
                FileHelper.ReleaseFileLock(sentinelPath);
            }

            Assert.True(acquiredLock);
            // Cleaned up?
            Assert.True(!File.Exists(sentinelPath));
        }