Skip to content

Commit 6c02259

Browse files
authored
Feature/misc helpers-2 (#102)
+ Code.CanBeCanceled() assertion + WaitForCancellationAsync() extension method + TimeoutHelper + WithTimeout() methods
1 parent db2eb35 commit 6c02259

File tree

11 files changed

+1066
-94
lines changed

11 files changed

+1066
-94
lines changed

CodeJam.Main.Tests/TestTools.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
7+
using System.Threading.Tasks;
78

89
using CodeJam.Strings;
910
using CodeJam.Targeting;
@@ -69,6 +70,40 @@ private static void PrintProps([NotNull] string typeName)
6970
Console.WriteLine($"\t * {prop.Name}: {prop.GetValue(null, null)}");
7071
}
7172
}
73+
74+
public static void WaitForResult([NotNull] this Task source)
75+
{
76+
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
77+
source.GetAwaiter().GetResult();
78+
#else
79+
// Workaround for Theraot cancellation logic
80+
try
81+
{
82+
source.GetAwaiter().GetResult();
83+
}
84+
catch (TaskCanceledException ex)
85+
{
86+
throw new OperationCanceledException(ex.Message, ex);
87+
}
88+
#endif
89+
}
90+
91+
public static T WaitForResult<T>([NotNull] this Task<T> source)
92+
{
93+
#if NET45_OR_GREATER || TARGETS_NETCOREAPP
94+
return source.GetAwaiter().GetResult();
95+
#else
96+
// Workaround for Theraot cancellation logic
97+
try
98+
{
99+
return source.GetAwaiter().GetResult();
100+
}
101+
catch (TaskCanceledException ex)
102+
{
103+
throw new OperationCanceledException(ex.Message, ex);
104+
}
105+
#endif
106+
}
72107
}
73108

74109
public class Holder<T>

0 commit comments

Comments
 (0)