Skip to content

Commit

Permalink
trying to fix a test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Dec 13, 2024
1 parent d41d854 commit 1865330
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
28 changes: 18 additions & 10 deletions Snowflake.Data.Tests/UnitTests/Tools/DirectoryInformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,52 @@ 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);
}

[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<object[]> 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<object[]> 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 };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 11 additions & 1 deletion Snowflake.Data/Core/Tools/DirectoryInformation.cs
Original file line number Diff line number Diff line change
@@ -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<DirectoryInformation>();

private readonly bool _exists;

private readonly DateTime? _creationTimeUtc;
Expand All @@ -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;
}
Expand Down

0 comments on commit 1865330

Please sign in to comment.