Skip to content

Commit

Permalink
BUG: Only pass the token to Task.Run for tests which are checking for…
Browse files Browse the repository at this point in the history
… that.

The risk is that the token is cancelled before the task has been started, then we cannot see what's happened in the task. Toggling this gives us control of what were testing without relying on time.
  • Loading branch information
Automation51D authored and Automation51D committed Sep 12, 2023
1 parent abe1678 commit d31baf7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
6 changes: 5 additions & 1 deletion FiftyOne.Caching.Tests/Loaders/ReturnKeyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ internal class ReturnKeyLoader<T> : TrackingLoaderBase<T, T>
{
public ReturnKeyLoader()
{
}

public ReturnKeyLoader(int delayMillis) : base(delayMillis)
{
}

public ReturnKeyLoader(int delayMillis) : base(delayMillis)
public ReturnKeyLoader(int delayMillis, bool runWithToken) : base(delayMillis, runWithToken)
{
}

Expand Down
15 changes: 8 additions & 7 deletions FiftyOne.Caching.Tests/Loaders/TrackingLoaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,34 @@ internal abstract class TrackingLoaderBase<TKey, TValue> : IValueTaskLoader<TKey

private volatile int _completeWaits = 0;

private volatile int _taskStarts = 0;
private volatile bool _runWithToken;

public int Calls => _calls;

public int Cancels => _cancels;

public int CompleteWaits => _completeWaits;

public int TaskStarts => _taskStarts;

public TrackingLoaderBase() : this(0)
{

}

public TrackingLoaderBase(int delayMillis)
public TrackingLoaderBase(int delayMillis) : this(delayMillis, false)
{
_delayMillis = delayMillis;
}

public TrackingLoaderBase(int delayMillis, bool runWithToken)
{
_delayMillis = delayMillis;
_runWithToken = runWithToken;
}

public Task<TValue> Load(TKey key, CancellationToken token)
{
Interlocked.Increment(ref _calls);
return Task.Run(() =>
{
Interlocked.Increment(ref _taskStarts);
if (_delayMillis > 0)
{
var start = DateTime.Now;
Expand All @@ -84,7 +85,7 @@ public Task<TValue> Load(TKey key, CancellationToken token)
}
}
return GetValue(key);
}, token);
}, _runWithToken ? token : new CancellationToken());
}

public TValue Load(TKey key)
Expand Down
39 changes: 18 additions & 21 deletions FiftyOne.Caching.Tests/LoadingDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void LoadingDictionary_GetCancelled()
{
// Arrange

var millis = 1000;
var millis = 5000;
var value = "teststring";
var loader = new ReturnKeyLoader<string>(millis);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
Expand All @@ -392,7 +392,6 @@ public void LoadingDictionary_GetCancelled()

// Assert

Assert.AreEqual(1, loader.TaskStarts);
Assert.AreEqual(0, loader.CompleteWaits);
Assert.AreEqual(1, loader.Cancels);
Assert.IsNotNull(exception);
Expand All @@ -410,9 +409,9 @@ public void LoadingDictionary_GetAlreadyCancelled()
{
// Arrange

var millis = 1000;
var millis = 5000;
var value = "teststring";
var loader = new ReturnKeyLoader<string>(millis);
var loader = new ReturnKeyLoader<string>(millis, true);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
OperationCanceledException exception = null;

Expand All @@ -430,7 +429,6 @@ public void LoadingDictionary_GetAlreadyCancelled()

// Assert

Assert.AreEqual(0, loader.TaskStarts);
Assert.AreEqual(0, loader.CompleteWaits);
Assert.AreEqual(0, loader.Cancels);
Assert.IsNotNull(exception);
Expand All @@ -447,7 +445,7 @@ public void LoadingDictionary_TryGetCancelled()
{
// Arrange

var millis = 1000;
var millis = 5000;
var value = "teststring";
var loader = new ReturnKeyLoader<string>(millis);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
Expand All @@ -472,7 +470,6 @@ public void LoadingDictionary_TryGetCancelled()

// Assert

Assert.AreEqual(1, loader.TaskStarts);
Assert.AreEqual(0, loader.CompleteWaits);
Assert.AreEqual(1, loader.Cancels);
Assert.IsNotNull(exception);
Expand All @@ -490,9 +487,9 @@ public void LoadingDictionary_TryGetAlreadyCancelled()
{
// Arrange

var millis = 1000;
var millis = 5000;
var value = "teststring";
var loader = new ReturnKeyLoader<string>(millis);
var loader = new ReturnKeyLoader<string>(millis, true);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
OperationCanceledException exception = null;

Expand All @@ -510,7 +507,6 @@ public void LoadingDictionary_TryGetAlreadyCancelled()

// Assert

Assert.AreEqual(0, loader.TaskStarts);
Assert.AreEqual(0, loader.CompleteWaits);
Assert.AreEqual(0, loader.Cancels);
Assert.IsNotNull(exception);
Expand Down Expand Up @@ -661,25 +657,26 @@ public void LoadingDictionary_GetCanceledIsNotReused()
// Arrange

var value = "testvalue";
var millis = 1000;
var millis = 5000;
var loader = new ReturnKeyLoader<string>(millis);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
OperationCanceledException exception = null;
Exception exception = null;
var count = 2;

// Act

for (int i = 0; i < count; i++)
{
_token.Cancel();
var getter = Task.Run(() => _ = dict[value, _token.Token]);
_token.Cancel();

try
{
_ = dict[value, _token.Token];
_ = getter.Result;
}
catch (OperationCanceledException e)
catch (AggregateException e)
{
exception = e;
exception = e.InnerException;
}
Assert.IsNotNull(exception);
exception = null;
Expand Down Expand Up @@ -736,24 +733,24 @@ public void LoadingDictionary_TryGetCanceledIsRemoved()
// Arrange

var value = "testvalue";
var millis = 1000;
var millis = 5000;
var loader = new ReturnKeyLoader<string>(millis);
var dict = new LoadingDictionary<string, string>(_logger.Object, loader);
var count = 2;

// Act

for (int i = 0; i < count; i++)
{
{
var getter = Task.Run(() => dict.TryGet(value, _token.Token, out _));
_token.Cancel();

Assert.ThrowsException<TaskCanceledException>(() =>
Assert.ThrowsException<AggregateException>(() =>
{
_ = dict.TryGet(value, _token.Token, out _);
_ = getter.Result;
});

_token = new CancellationTokenSource();
Thread.Sleep(100);
}

// Assert
Expand Down

0 comments on commit d31baf7

Please sign in to comment.