< Summary

Class:Azure.Identity.SentinelFileLock
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\SentinelFileLock.cs
Covered lines:0
Uncovered lines:20
Coverable lines:20
Total lines:60
Line coverage:0% (0 of 20)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
AcquireAsync()-0%0%
Dispose()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\SentinelFileLock.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Text;
 8using System.Threading.Tasks;
 9
 10namespace Azure.Identity
 11{
 12    internal class SentinelFileLock : IDisposable
 13    {
 14        private FileStream _lockFileStream;
 15        private const int DefaultFileBufferSize = 4096;
 16
 017        private SentinelFileLock(FileStream lockFileStream)
 18        {
 019            _lockFileStream = lockFileStream;
 020        }
 21
 22        public static async Task<SentinelFileLock> AcquireAsync(string lockfilePath, int lockFileRetryCount, TimeSpan lo
 23        {
 024            Exception exception = null;
 025            FileStream fileStream = null;
 26
 27            // Create lock file dir if it doesn't already exist
 028            Directory.CreateDirectory(Path.GetDirectoryName(lockfilePath));
 29
 030            for (int tryCount = 0; tryCount < lockFileRetryCount; tryCount++)
 31            {
 32                try
 33                {
 34                    // We are using the file locking to synchronize the store, do not allow multiple writers for the fil
 35                    // Note: this only works on windows if we extend to work on unix systems we need to set FileShare.No
 036                    fileStream = new FileStream(lockfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Rea
 37
 038                    using (var writer = new StreamWriter(fileStream, Encoding.UTF8, DefaultFileBufferSize, leaveOpen: tr
 39                    {
 040                        await writer.WriteLineAsync($"{Process.GetCurrentProcess().Id} {Process.GetCurrentProcess().Proc
 041                    }
 042                    break;
 43                }
 044                catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
 45                {
 046                    exception = ex;
 047                    await Task.Delay(lockFileRetryDelay).ConfigureAwait(false);
 48                }
 49            }
 50
 051            return (fileStream != null) ? new SentinelFileLock(fileStream) : throw new InvalidOperationException("Could 
 052        }
 53
 54        public void Dispose()
 55        {
 056            _lockFileStream?.Dispose();
 057            _lockFileStream = null;
 058        }
 59    }
 60}

Methods/Properties

.ctor(...)
AcquireAsync()
Dispose()