Skip to content

Commit

Permalink
Fix flaky test TestWaitUntilResourceAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Apr 3, 2024
1 parent 0983d94 commit c9526d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
32 changes: 20 additions & 12 deletions Snowflake.Data.Tests/UnitTests/Session/WaitingQueueTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Snowflake.Data.Core.Session;
using Snowflake.Data.Tests.Util;

namespace Snowflake.Data.Tests.UnitTests.Session
{
Expand Down Expand Up @@ -48,22 +50,28 @@ public void TestWaitForTheResourceUntilCancellation()
public void TestWaitUntilResourceAvailable()
{
// arrange
var queue = new WaitingQueue();
var watch = new Stopwatch();
Task.Run(() =>
var tests = TestRepeater<Tuple<bool, long>>.Test(3, () =>
{
Thread.Sleep(50);
queue.OnResourceIncrease();
var queue = new WaitingQueue();
var watch = new Stopwatch();
Task.Run(() =>
{
Thread.Sleep(50);
queue.OnResourceIncrease();
});

// act
watch.Start();
var result = queue.Wait(30000, CancellationToken.None);
watch.Stop();
return Tuple.Create(result, watch.ElapsedMilliseconds);
});

// act
watch.Start();
var result = queue.Wait(30000, CancellationToken.None);
watch.Stop();

// assert
Assert.IsTrue(result);
Assert.That(watch.ElapsedMilliseconds, Is.InRange(50, 1500));
tests.ForEach(t => Assert.IsTrue(t.Item1));
tests.ForEach(t => Assert.GreaterOrEqual(t.Item2, 50));
tests.SkipLargest(t => t.Item2) // some execution can be randomly delayed so we skip the largest value
.ForEach(t => Assert.That(t.Item2, Is.InRange(50, 1500)));
}

[Test]
Expand Down
32 changes: 32 additions & 0 deletions Snowflake.Data.Tests/Util/TestRepeater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Snowflake.Data.Tests.Util
{
public class TestRepeater<T>
{
private readonly List<T> _result;

private TestRepeater(List<T> result)
{
_result = result;
}

public void ForEach(Action<T> action) => _result.ForEach(action);

public TestRepeater<T> SkipLargest<TKey>(Func<T, TKey> keySelector)
{
var resultsWithoutLargest = _result.OrderBy(keySelector).SkipLast(1).ToList();

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, GCP)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, GCP)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AWS)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AWS)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AZURE)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AZURE)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AWS)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AWS)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, GCP)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, GCP)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AZURE)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in Snowflake.Data.Tests/Util/TestRepeater.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AZURE)

'IOrderedEnumerable<T>' does not contain a definition for 'SkipLast' and no accessible extension method 'SkipLast' accepting a first argument of type 'IOrderedEnumerable<T>' could be found (are you missing a using directive or an assembly reference?)
return new TestRepeater<T>(resultsWithoutLargest);
}

public static TestRepeater<T> Test(int times, Func<T> testFunction)
{
var resultList = Enumerable.Repeat(0, times)
.Select(_ => testFunction())
.ToList();
return new TestRepeater<T>(resultList);
}
}
}

0 comments on commit c9526d5

Please sign in to comment.