Skip to content

Commit 12f4e86

Browse files
committed
Fix tests after review. I've added special handling for cancellation token to the IsEquivalent() method
1 parent df88b69 commit 12f4e86

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Gofer.NET.Tests/GivenATaskInfo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Linq.Expressions;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -19,20 +20,23 @@ public void ItPersistsPropertiesWhenSerializedAndDeserialized()
1920
var taskInfos = new[] {
2021
GetTestTask(() => TestMethod1("hello world")),
2122
GetTestTask(() => TestMethod2()),
22-
GetTestTask(() => TestMethod3(default))
23+
GetTestTask(() => TestMethod3(new CancellationToken()))
2324
};
2425

2526
foreach (var taskInfo in taskInfos)
2627
{
2728
var serializedTaskInfo = JsonTaskInfoSerializer.Serialize(taskInfo);
2829
var deserializedTaskInfo = JsonTaskInfoSerializer.Deserialize<TaskInfo>(serializedTaskInfo);
2930

31+
// we do skip cancellation tokens as they are ignored on serialization
32+
var importantTaskArgs = taskInfo.Args.Select(a => a is CancellationToken ? null : a).ToArray();
33+
3034
deserializedTaskInfo.Id.Should().Be(taskInfo.Id);
3135
deserializedTaskInfo.AssemblyName.Should().Be(taskInfo.AssemblyName);
3236
deserializedTaskInfo.TypeName.Should().Be(taskInfo.TypeName);
3337
deserializedTaskInfo.MethodName.Should().Be(taskInfo.MethodName);
3438
deserializedTaskInfo.ReturnType.Should().Be(taskInfo.ReturnType);
35-
deserializedTaskInfo.Args.ShouldAllBeEquivalentTo(taskInfo.Args);
39+
deserializedTaskInfo.Args.ShouldAllBeEquivalentTo(importantTaskArgs);
3640
deserializedTaskInfo.ArgTypes.ShouldAllBeEquivalentTo(taskInfo.ArgTypes);
3741
deserializedTaskInfo.CreatedAtUtc.Should().Be(taskInfo.CreatedAtUtc);
3842

@@ -50,7 +54,7 @@ public void ItProperlyDeterminesEquivalence()
5054
var taskInfo2a = GetTestTask(() => TestMethod2());
5155
var taskInfo2b = GetTestTask(() => TestMethod2());
5256

53-
var taskInfo3a = GetTestTask(() => TestMethod3(default));
57+
var taskInfo3a = GetTestTask(() => TestMethod3(new CancellationToken()));
5458
var taskInfo3b = GetTestTask(() => TestMethod3(new CancellationTokenSource().Token));
5559

5660
var taskInfo4a = GetTestTask(() => Console.WriteLine("hello"));

Gofer.NET.Utils/TaskInfo.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ public bool IsEquivalent(TaskInfo otherTaskInfo)
4343
return false;
4444
}
4545

46-
for (var i=0; i<Args.Length; ++i) {
47-
if (!Equals(Args[i], otherTaskInfo.Args[i]))
46+
for (var i = 0; i < Args.Length; ++i)
47+
{
48+
// comparison ignores cancellation tokens
49+
// as they are ignored on serialization
50+
// and will be replaced with proper value at invocation time
51+
if (!Equals(GetEquivalenceArg(Args[i]), GetEquivalenceArg(otherTaskInfo.Args[i])))
4852
{
4953
return false;
5054
}
@@ -56,6 +60,11 @@ public bool IsEquivalent(TaskInfo otherTaskInfo)
5660
&& ReturnType.Equals(otherTaskInfo.ReturnType);
5761
}
5862

63+
private object GetEquivalenceArg(object value)
64+
{
65+
return value is CancellationToken ? null : value;
66+
}
67+
5968
public void ConvertTypeArgs()
6069
{
6170
for (var i=0;i<Args.Length;++i) {
@@ -68,14 +77,16 @@ public void ConvertTypeArgs()
6877
}
6978
}
7079

71-
public void UnconvertTypeArgs()
80+
public void UnconvertTypeArgs()
7281
{
73-
for (var i=0;i<Args.Length;++i) {
82+
for (var i = 0; i < Args.Length; ++i)
83+
{
7484
if (Args[i] == null)
7585
continue;
76-
77-
if (typeof(TypeWrapper).IsAssignableFrom(Args[i].GetType())) {
78-
Args[i] = ((TypeWrapper) Args[i]).Type;
86+
87+
if (typeof(TypeWrapper).IsAssignableFrom(Args[i].GetType()))
88+
{
89+
Args[i] = ((TypeWrapper)Args[i]).Type;
7990
}
8091
}
8192
}

0 commit comments

Comments
 (0)