Skip to content

Commit

Permalink
tests for isExpired
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Apr 19, 2024
1 parent 7840223 commit 4f0f1a3
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions Snowflake.Data.Tests/UnitTests/Tools/TimeoutHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void TestInfinity(TimeSpan infiniteTimeout)
{
// act
var isInfinite = TimeoutHelper.IsInfinite(infiniteTimeout);

// assert
Assert.IsTrue(isInfinite);
}
Expand All @@ -25,7 +25,7 @@ public void TestFiniteValue(TimeSpan finiteTimeout)
{
// act
var isInfinite = TimeoutHelper.IsInfinite(finiteTimeout);

// assert
Assert.IsFalse(isInfinite);
}
Expand All @@ -36,7 +36,7 @@ public void TestZeroLength(TimeSpan zeroTimeout)
{
// act
var isZeroLength = TimeoutHelper.IsZeroLength(zeroTimeout);

// assert
Assert.IsTrue(isZeroLength);
}
Expand All @@ -47,19 +47,19 @@ public void TestNonZeroLength(TimeSpan nonZeroTimeout)
{
// act
var isZeroLength = TimeoutHelper.IsZeroLength(nonZeroTimeout);

// assert
Assert.IsFalse(isZeroLength);
}

[Test]
[TestCase(1000, 1000)]
[TestCase(1000, 2000)]
public void TestInfiniteTimeoutDoesNotExpire(long startedAtMillis, long nowMillis)
{
// act
var isExpired = TimeoutHelper.IsExpired(startedAtMillis, nowMillis, TimeoutHelper.Infinity());

// assert
Assert.IsFalse(isExpired);
}
Expand All @@ -74,26 +74,54 @@ public void TestExpiredTimeout(long startedAtMillis, long nowMillis, long timeou
{
// arrange
var timeout = TimeSpan.FromMilliseconds(timeoutMillis);

// act
var isExpired = TimeoutHelper.IsExpired(startedAtMillis, nowMillis, timeout);

// assert
Assert.AreEqual(expectedIsExpired, isExpired);
}

[Test]
public void TestInfiniteTimeoutNeverExpires()
{
// act
var isExpired = TimeoutHelper.IsExpired(1000, TimeoutHelper.Infinity());

// assert
Assert.IsFalse(isExpired);
}

[Test]
[TestCase(0, 0, true)]
[TestCase(1000, 0, true)]
[TestCase(100, 100, true)]
[TestCase(99, 100, false)]
[TestCase(1000, 100, true)]
public void TestExpiredTimeoutByDuration(long durationMillis, long timeoutMillis, bool expectedIsExpired)
{
// arrange
var timeout = TimeSpan.FromMilliseconds(timeoutMillis);

// act
var isExpired = TimeoutHelper.IsExpired(durationMillis, timeout);

// assert
Assert.AreEqual(expectedIsExpired, isExpired);
}

[Test]
public void TestFiniteTimeoutLeftFailsForInfiniteTimeout()
{
// act
var thrown = Assert.Throws<Exception>(() =>
TimeoutHelper.FiniteTimeoutLeftMillis(1000, 2000, TimeoutHelper.Infinity()));

// assert
Assert.That(thrown.Message, Does.Contain("Infinite timeout cannot be used to determine milliseconds left"));
}


[Test]
[TestCase(1000, 1000, 0, 0)]
[TestCase(1000, 2000, 0, 0)]
Expand All @@ -103,20 +131,20 @@ public void TestFiniteTimeoutLeft(long startedAtMillis, long nowMillis, long tim
{
// arrange
var timeout = TimeSpan.FromMilliseconds(timeoutMillis);

// act
var millisLeft = TimeoutHelper.FiniteTimeoutLeftMillis(startedAtMillis, nowMillis, timeout);

// assert
Assert.AreEqual(expectedMillisLeft, millisLeft);
}
}

public static IEnumerable<TimeSpan> InfiniteTimeouts()
{
yield return TimeoutHelper.Infinity();
yield return TimeSpan.FromMilliseconds(-1);
}

public static IEnumerable<TimeSpan> FiniteTimeouts()
{
yield return TimeSpan.Zero;
Expand All @@ -130,7 +158,7 @@ public static IEnumerable<TimeSpan> ZeroLengthTimeouts()
yield return TimeSpan.FromMilliseconds(0);
yield return TimeSpan.FromSeconds(0);
}

public static IEnumerable<TimeSpan> NonZeroLengthTimeouts()
{
yield return TimeoutHelper.Infinity();
Expand Down

0 comments on commit 4f0f1a3

Please sign in to comment.