diff --git a/Snowflake.Data.Tests/UnitTests/Tools/DirectoryInformationTest.cs b/Snowflake.Data.Tests/UnitTests/Tools/DirectoryInformationTest.cs index 16a87a619..b8baa2e68 100644 --- a/Snowflake.Data.Tests/UnitTests/Tools/DirectoryInformationTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Tools/DirectoryInformationTest.cs @@ -10,13 +10,13 @@ public class DirectoryInformationTest { [Test] [TestCaseSource(nameof(OldCreatingDatesTestCases))] - public void TestIsCreatedEarlierThanSeconds(DateTime? createdDate) + public void TestIsCreatedEarlierThanSeconds(DateTime? createdDate, DateTime utcNow) { // arrange var directoryInformation = new DirectoryInformation(true, createdDate); // act - var result = directoryInformation.IsCreatedEarlierThanSeconds(60); + var result = directoryInformation.IsCreatedEarlierThanSeconds(60, utcNow); // assert Assert.AreEqual(true, result); @@ -24,30 +24,38 @@ public void TestIsCreatedEarlierThanSeconds(DateTime? createdDate) [Test] [TestCaseSource(nameof(NewCreatingDatesTestCases))] - public void TestIsNotCreatedEarlierThanSeconds(bool dirExists, DateTime? createdDate) + public void TestIsNotCreatedEarlierThanSeconds(bool dirExists, DateTime? createdDate, DateTime utcNow) { // arrange var directoryInformation = new DirectoryInformation(dirExists, createdDate); // act - var result = directoryInformation.IsCreatedEarlierThanSeconds(60); + var result = directoryInformation.IsCreatedEarlierThanSeconds(60, utcNow); // assert Assert.AreEqual(false, result); } + [Test] + public void TestCompareDates() // for debugging + { + var condition = DateTime.UtcNow.AddSeconds(-30).AddSeconds(60) < DateTime.UtcNow; + Assert.IsFalse(condition); + } + internal static IEnumerable OldCreatingDatesTestCases() { - yield return new object[] { DateTime.UtcNow.AddMinutes(-2) }; - yield return new object[] { DateTime.UtcNow.AddSeconds(-61) }; + yield return new object[] { DateTime.UtcNow.AddMinutes(-2), DateTime.UtcNow }; + yield return new object[] { DateTime.UtcNow.AddSeconds(-61), DateTime.UtcNow }; } internal static IEnumerable NewCreatingDatesTestCases() { - yield return new object[] { true, DateTime.UtcNow.AddSeconds(-30) }; - yield return new object[] { true, DateTime.UtcNow.AddSeconds(30) }; - yield return new object[] { true, DateTime.UtcNow }; - yield return new object[] { false, null }; + yield return new object[] { true, DateTime.UtcNow.AddSeconds(-30), DateTime.UtcNow }; + yield return new object[] { true, DateTime.UtcNow.AddSeconds(30), DateTime.UtcNow }; + yield return new object[] { true, DateTime.UtcNow.AddSeconds(1000), DateTime.UtcNow }; + yield return new object[] { true, DateTime.UtcNow, DateTime.UtcNow }; + yield return new object[] { false, null, DateTime.UtcNow }; } } } diff --git a/Snowflake.Data/Core/CredentialManager/Infrastructure/SFCredentialManagerFileImpl.cs b/Snowflake.Data/Core/CredentialManager/Infrastructure/SFCredentialManagerFileImpl.cs index 4ba4b251a..2c7e4e8c4 100644 --- a/Snowflake.Data/Core/CredentialManager/Infrastructure/SFCredentialManagerFileImpl.cs +++ b/Snowflake.Data/Core/CredentialManager/Infrastructure/SFCredentialManagerFileImpl.cs @@ -220,7 +220,7 @@ private bool AcquireLock() _directoryOperations.CreateDirectory(_jsonCacheDirectory); } var lockDirectoryInfo = _directoryOperations.GetDirectoryInfo(_jsonCacheLockPath); - if (lockDirectoryInfo.IsCreatedEarlierThanSeconds(CredentialCacheLockDurationSeconds)) + if (lockDirectoryInfo.IsCreatedEarlierThanSeconds(CredentialCacheLockDurationSeconds, DateTime.UtcNow)) { s_logger.Warn($"File cache lock directory {_jsonCacheLockPath} created more than {CredentialCacheLockDurationSeconds} seconds ago. Removing the lock directory."); ReleaseLock(); diff --git a/Snowflake.Data/Core/Tools/DirectoryInformation.cs b/Snowflake.Data/Core/Tools/DirectoryInformation.cs index 3a00a132e..84a242ed5 100644 --- a/Snowflake.Data/Core/Tools/DirectoryInformation.cs +++ b/Snowflake.Data/Core/Tools/DirectoryInformation.cs @@ -1,10 +1,13 @@ using System; using System.IO; +using Snowflake.Data.Log; namespace Snowflake.Data.Core.Tools { public class DirectoryInformation { + private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); + private readonly bool _exists; private readonly DateTime? _creationTimeUtc; @@ -21,7 +24,14 @@ internal DirectoryInformation(bool exists, DateTime? creationTimeUtc) _creationTimeUtc = creationTimeUtc; } - public bool IsCreatedEarlierThanSeconds(int seconds) => _exists && _creationTimeUtc?.AddSeconds(seconds) < DateTime.UtcNow; + public bool IsCreatedEarlierThanSeconds(int seconds, DateTime utcNow) + { + s_logger.Warn($"Now is {utcNow}"); + s_logger.Warn($"CreationTimeUtc is {_creationTimeUtc}"); + s_logger.Warn($"CreationTimeUtc + {60} seconds is {_creationTimeUtc?.AddSeconds(seconds)}"); + s_logger.Warn($"Result of date comparison is {_creationTimeUtc?.AddSeconds(seconds) < utcNow}"); + return _exists && _creationTimeUtc?.AddSeconds(seconds) < utcNow; + } public bool Exists() => _exists; }