Ejemplo n.º 1
0
        public async void AcquireSecondLockAfterRelease()
        {
            var   file     = new FileInfo(Path.GetTempFileName());
            ILock fileLock = new FileLock(file);
            await fileLock.TryAcquire(TimeSpan.FromSeconds(1));

            Thread.Sleep(1500);
            await fileLock.TryAcquire(TimeSpan.FromSeconds(10));

            Assert.NotNull(fileLock);
        }
Ejemplo n.º 2
0
        public async void GetTimeReturnsCurrentReleaseDate()
        {
            var file     = new FileInfo(Path.GetTempFileName());
            var fileLock = new FileLock(file);
            await fileLock.TryAcquire(TimeSpan.FromHours(1));

            DateTime dateTime = await fileLock.GetReleaseDate();

            Assert.NotEqual(DateTime.MaxValue, dateTime);
        }
Ejemplo n.º 3
0
        public async void ManyProcess()
        {
            var file     = new FileInfo(Path.GetTempFileName());
            var fileLock = new FileLock(file);
            await fileLock.TryAcquire(TimeSpan.FromHours(1));

            fileLock.AddTime(TimeSpan.FromHours(1));

            var fileDate = new DateTime(long.Parse(File.ReadAllText(Path.ChangeExtension(file.FullName, Extension))));

            Assert.True(fileDate - DateTime.UtcNow - TimeSpan.FromHours(2) < _timeVariable);
        }
Ejemplo n.º 4
0
        public async void Dispose()
        {
            var   file     = new FileInfo(Path.GetTempFileName());
            ILock fileLock = new FileLock(file);

            if (await fileLock.TryAcquire(TimeSpan.FromHours(1)))
            {
                using (fileLock)
                {
                    Assert.True(File.Exists(Path.ChangeExtension(file.FullName, Extension)));
                    fileLock.Dispose();
                }
            }

            Assert.False(File.Exists(Path.ChangeExtension(file.FullName, Extension)));
        }
Ejemplo n.º 5
0
        public async void CannotWriteToFile()
        {
            var   file     = new FileInfo(Path.GetTempFileName());
            ILock fileLock = new FileLock(file);

            if (await fileLock.TryAcquire(TimeSpan.FromHours(1)))
            {
                using (fileLock)
                {
                    string pathToLock = Path.ChangeExtension(file.FullName, Extension);
                    using (FileStream stream = File.Open(pathToLock, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
                    {
                        await fileLock.AddTime(TimeSpan.FromHours(1));

                        Assert.False(await new FileLock(file).TryAcquire(TimeSpan.MaxValue, false));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void AddStageEntry([NotNull] StageEntry se, [NotNull] RunningConfig config)
        {
            if (!config.MakeStageEntries)
            {
                return;
            }

            Console.WriteLine("Config in stage: " + JsonConvert.SerializeObject(config, Formatting.Indented));
            if (!string.IsNullOrWhiteSpace(se.DevelopmentStatus))
            {
                se.ImplementationFinished = false;
            }

            // ReSharper disable once InconsistentlySynchronizedField
            string stagePath = Path.Combine(_config.Directories.BaseProcessingDirectory, "stages.xlsx");
            string lockPath  = stagePath + ".lock";
            Random rnd       = new Random();

            try {
                using (ILock fileLock = new FileLock(lockPath)) {
                    try {
                        bool lockAquired = false;
                        while (!lockAquired)
                        {
                            try {
                                fileLock.TryAcquire(TimeSpan.FromSeconds(5));
                                lockAquired = true;
                            }
#pragma warning disable CA1031 // Do not catch general exception types
                            catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                                Console.WriteLine("Locking failed for file: " + stagePath + ": " + ex.Message);
                                Thread.Sleep(rnd.Next(500));
                            }
                        }

                        WriteXlsFileContent(stagePath, se);
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch (Exception ex1) {
#pragma warning restore CA1031 // Do not catch general exception types
                        Console.WriteLine("trying to log result file, exception : " + ex1.Message);
                    }
                    finally {
                        try {
                            fileLock.Dispose();
                        }
#pragma warning disable CA1031 // Do not catch general exception types
                        catch (Exception x) {
#pragma warning restore CA1031 // Do not catch general exception types
                            Console.WriteLine("failed to release lock, exception : " + x.Message);
                        }
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
#pragma warning restore CA1031 // Do not catch general exception types
                Console.WriteLine("Locking release failed for file: " + stagePath + ": " + ex.Message);
                Thread.Sleep(rnd.Next(500));
            }
        }