Skip to content

Commit 816894e

Browse files
committed
Cleanup of unused sync-over-async RunUtils methods
1 parent 9bd143f commit 816894e

File tree

2 files changed

+92
-164
lines changed

2 files changed

+92
-164
lines changed

src/ZiggyCreatures.FusionCache/Internals/RunUtils.cs

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -151,76 +151,6 @@ public static async Task RunAsyncActionWithTimeoutAsync(Func<CancellationToken,
151151
}
152152
}
153153

154-
/// <summary>
155-
/// Run an async function synchoronously with a timeout and some additional options.
156-
/// </summary>
157-
/// <typeparam name="TResult"></typeparam>
158-
/// <param name="asyncFunc">The async function to execute.</param>
159-
/// <param name="timeout">The timeout to apply.</param>
160-
/// <param name="cancelIfTimeout">Indicates if the action should be cancelled in case of a timeout.</param>
161-
/// <param name="timedOutTaskProcessor">A lambda to process the task representing the eventually timed out function.</param>
162-
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
163-
/// <returns>The value returned from the async function</returns>
164-
public static TResult RunAsyncFuncWithTimeout<TResult>(Func<CancellationToken, Task<TResult>> asyncFunc, TimeSpan timeout, bool cancelIfTimeout = true, Action<Task<TResult>>? timedOutTaskProcessor = null, CancellationToken token = default)
165-
{
166-
token.ThrowIfCancellationRequested();
167-
168-
if (timeout == TimeSpan.Zero || timeout < Timeout.InfiniteTimeSpan)
169-
{
170-
var exc = new SyntheticTimeoutException();
171-
if (cancelIfTimeout == false)
172-
timedOutTaskProcessor?.Invoke(Task.FromException<TResult>(exc));
173-
throw exc;
174-
}
175-
176-
Task<TResult> task;
177-
178-
if (timeout == Timeout.InfiniteTimeSpan && token == CancellationToken.None)
179-
{
180-
task = _taskFactory.StartNew(() => asyncFunc(token), token).Unwrap();
181-
}
182-
else
183-
{
184-
task = _taskFactory.StartNew(() => RunAsyncFuncWithTimeoutAsync(asyncFunc, timeout, cancelIfTimeout, timedOutTaskProcessor, token), token).Unwrap();
185-
}
186-
187-
return task.GetAwaiter().GetResult();
188-
}
189-
190-
/// <summary>
191-
/// Run an async action synchoronously with a timeout and some additional options.
192-
/// </summary>
193-
/// <param name="asyncAction">The async action to execute.</param>
194-
/// <param name="timeout">The timeout to apply.</param>
195-
/// <param name="cancelIfTimeout">Indicates if the action should be cancelled in case of a timeout.</param>
196-
/// <param name="timedOutTaskProcessor">A lambda to process the task representing the eventually timed out action.</param>
197-
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
198-
public static void RunAsyncActionWithTimeout(Func<CancellationToken, Task> asyncAction, TimeSpan timeout, bool cancelIfTimeout = true, Action<Task>? timedOutTaskProcessor = null, CancellationToken token = default)
199-
{
200-
token.ThrowIfCancellationRequested();
201-
202-
if (timeout == TimeSpan.Zero || timeout < Timeout.InfiniteTimeSpan)
203-
{
204-
var exc = new SyntheticTimeoutException();
205-
if (cancelIfTimeout == false)
206-
timedOutTaskProcessor?.Invoke(Task.FromException(exc));
207-
throw exc;
208-
}
209-
210-
Task task;
211-
212-
if (timeout == Timeout.InfiniteTimeSpan && token == CancellationToken.None)
213-
{
214-
task = _taskFactory.StartNew(() => asyncAction(token), token).Unwrap();
215-
}
216-
else
217-
{
218-
task = _taskFactory.StartNew(() => RunAsyncActionWithTimeoutAsync(asyncAction, timeout, cancelIfTimeout, timedOutTaskProcessor, token), token).Unwrap();
219-
}
220-
221-
task.GetAwaiter().GetResult();
222-
}
223-
224154
/// <summary>
225155
/// Run a sync function synchoronously with a timeout and some additional options.
226156
/// </summary>

tests/ZiggyCreatures.FusionCache.Tests/RunUtilsTests_Sync.cs

Lines changed: 92 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,102 @@
1-
using System.Diagnostics;
2-
using FusionCacheTests.Stuff;
1+
using FusionCacheTests.Stuff;
32
using Xunit;
4-
using ZiggyCreatures.Caching.Fusion;
53
using ZiggyCreatures.Caching.Fusion.Internals;
64

75
namespace FusionCacheTests;
86

97
public partial class RunUtilsTests
108
{
11-
[Fact]
12-
public void ZeroTimeoutDoesNotStartAsyncFunc()
13-
{
14-
bool _hasRun = false;
15-
16-
Assert.Throws<SyntheticTimeoutException>(() =>
17-
{
18-
RunUtils.RunAsyncFuncWithTimeout(async ct => { _hasRun = true; return 42; }, TimeSpan.Zero, false, t => { }, TestContext.Current.CancellationToken);
19-
});
20-
Assert.False(_hasRun);
21-
}
22-
23-
[Fact]
24-
public void ZeroTimeoutDoesNotStartAsyncAction()
25-
{
26-
bool _hasRun = false;
27-
28-
Assert.Throws<SyntheticTimeoutException>(() =>
29-
{
30-
RunUtils.RunAsyncActionWithTimeout(async ct => { _hasRun = true; }, TimeSpan.Zero, false, t => { }, TestContext.Current.CancellationToken);
31-
});
32-
Assert.False(_hasRun);
33-
}
34-
35-
[Fact]
36-
public void CanCancelAnAsyncFunc()
37-
{
38-
int res = -1;
39-
var factoryTerminated = false;
40-
var outerCancelDelay = TimeSpan.FromMilliseconds(500);
41-
var innerDelay = TimeSpan.FromSeconds(2);
42-
Assert.ThrowsAny<OperationCanceledException>(() =>
43-
{
44-
var cts = new CancellationTokenSource(outerCancelDelay);
45-
res = RunUtils.RunAsyncFuncWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryTerminated = true; return 42; }, Timeout.InfiniteTimeSpan, true, token: cts.Token);
46-
});
47-
Thread.Sleep(innerDelay.PlusALittleBit());
48-
49-
Assert.Equal(-1, res);
50-
Assert.False(factoryTerminated);
51-
}
52-
53-
[Fact]
54-
public void TimeoutEffectivelyWorks()
55-
{
56-
int res = -1;
57-
var timeout = TimeSpan.FromMilliseconds(500);
58-
var innerDelay = TimeSpan.FromSeconds(2);
59-
var sw = Stopwatch.StartNew();
60-
Assert.ThrowsAny<TimeoutException>(() =>
61-
{
62-
res = RunUtils.RunAsyncFuncWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); return 42; }, timeout, token: TestContext.Current.CancellationToken);
63-
});
64-
sw.Stop();
65-
66-
var elapsedMs = sw.GetElapsedWithSafePad().TotalMilliseconds;
67-
68-
Assert.Equal(-1, res);
69-
Assert.True(elapsedMs >= timeout.TotalMilliseconds, $"Elapsed ({elapsedMs}ms) is less than specified timeout ({timeout.TotalMilliseconds}ms)");
70-
Assert.True(elapsedMs < innerDelay.TotalMilliseconds, $"Elapsed ({elapsedMs}ms) is greater than or equal to inner delay ({innerDelay.TotalMilliseconds}ms)");
71-
}
72-
73-
[Fact]
74-
public void CancelWhenTimeoutActuallyWorks()
75-
{
76-
var factoryCompleted = false;
77-
var timeout = TimeSpan.FromMilliseconds(500);
78-
var innerDelay = TimeSpan.FromSeconds(2);
79-
Assert.ThrowsAny<TimeoutException>(() =>
80-
{
81-
RunUtils.RunAsyncActionWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryCompleted = true; }, timeout, true, token: TestContext.Current.CancellationToken);
82-
});
83-
Thread.Sleep(innerDelay.PlusALittleBit());
84-
85-
Assert.False(factoryCompleted);
86-
}
87-
88-
[Fact]
89-
public void DoNotCancelWhenTimeoutActuallyWorks()
90-
{
91-
var factoryCompleted = false;
92-
var timeout = TimeSpan.FromMilliseconds(100);
93-
var innerDelay = TimeSpan.FromSeconds(2);
94-
Assert.ThrowsAny<TimeoutException>(() =>
95-
{
96-
RunUtils.RunAsyncActionWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryCompleted = true; }, timeout, false, token: TestContext.Current.CancellationToken);
97-
});
98-
Thread.Sleep((innerDelay + timeout).PlusALittleBit());
99-
100-
Assert.True(factoryCompleted);
101-
}
9+
//[Fact]
10+
//public void ZeroTimeoutDoesNotStartAsyncFunc()
11+
//{
12+
// bool _hasRun = false;
13+
14+
// Assert.Throws<SyntheticTimeoutException>(() =>
15+
// {
16+
// RunUtils.RunAsyncFuncWithTimeout(async ct => { _hasRun = true; return 42; }, TimeSpan.Zero, false, t => { }, TestContext.Current.CancellationToken);
17+
// });
18+
// Assert.False(_hasRun);
19+
//}
20+
21+
//[Fact]
22+
//public void ZeroTimeoutDoesNotStartAsyncAction()
23+
//{
24+
// bool _hasRun = false;
25+
26+
// Assert.Throws<SyntheticTimeoutException>(() =>
27+
// {
28+
// RunUtils.RunAsyncActionWithTimeout(async ct => { _hasRun = true; }, TimeSpan.Zero, false, t => { }, TestContext.Current.CancellationToken);
29+
// });
30+
// Assert.False(_hasRun);
31+
//}
32+
33+
//[Fact]
34+
//public void CanCancelAnAsyncFunc()
35+
//{
36+
// int res = -1;
37+
// var factoryTerminated = false;
38+
// var outerCancelDelay = TimeSpan.FromMilliseconds(500);
39+
// var innerDelay = TimeSpan.FromSeconds(2);
40+
// Assert.ThrowsAny<OperationCanceledException>(() =>
41+
// {
42+
// var cts = new CancellationTokenSource(outerCancelDelay);
43+
// res = RunUtils.RunAsyncFuncWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryTerminated = true; return 42; }, Timeout.InfiniteTimeSpan, true, token: cts.Token);
44+
// });
45+
// Thread.Sleep(innerDelay.PlusALittleBit());
46+
47+
// Assert.Equal(-1, res);
48+
// Assert.False(factoryTerminated);
49+
//}
50+
51+
//[Fact]
52+
//public void TimeoutEffectivelyWorks()
53+
//{
54+
// int res = -1;
55+
// var timeout = TimeSpan.FromMilliseconds(500);
56+
// var innerDelay = TimeSpan.FromSeconds(2);
57+
// var sw = Stopwatch.StartNew();
58+
// Assert.ThrowsAny<TimeoutException>(() =>
59+
// {
60+
// res = RunUtils.RunAsyncFuncWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); return 42; }, timeout, token: TestContext.Current.CancellationToken);
61+
// });
62+
// sw.Stop();
63+
64+
// var elapsedMs = sw.GetElapsedWithSafePad().TotalMilliseconds;
65+
66+
// Assert.Equal(-1, res);
67+
// Assert.True(elapsedMs >= timeout.TotalMilliseconds, $"Elapsed ({elapsedMs}ms) is less than specified timeout ({timeout.TotalMilliseconds}ms)");
68+
// Assert.True(elapsedMs < innerDelay.TotalMilliseconds, $"Elapsed ({elapsedMs}ms) is greater than or equal to inner delay ({innerDelay.TotalMilliseconds}ms)");
69+
//}
70+
71+
//[Fact]
72+
//public void CancelWhenTimeoutActuallyWorks()
73+
//{
74+
// var factoryCompleted = false;
75+
// var timeout = TimeSpan.FromMilliseconds(500);
76+
// var innerDelay = TimeSpan.FromSeconds(2);
77+
// Assert.ThrowsAny<TimeoutException>(() =>
78+
// {
79+
// RunUtils.RunAsyncActionWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryCompleted = true; }, timeout, true, token: TestContext.Current.CancellationToken);
80+
// });
81+
// Thread.Sleep(innerDelay.PlusALittleBit());
82+
83+
// Assert.False(factoryCompleted);
84+
//}
85+
86+
//[Fact]
87+
//public void DoNotCancelWhenTimeoutActuallyWorks()
88+
//{
89+
// var factoryCompleted = false;
90+
// var timeout = TimeSpan.FromMilliseconds(100);
91+
// var innerDelay = TimeSpan.FromSeconds(2);
92+
// Assert.ThrowsAny<TimeoutException>(() =>
93+
// {
94+
// RunUtils.RunAsyncActionWithTimeout(async ct => { await Task.Delay(innerDelay); ct.ThrowIfCancellationRequested(); factoryCompleted = true; }, timeout, false, token: TestContext.Current.CancellationToken);
95+
// });
96+
// Thread.Sleep((innerDelay + timeout).PlusALittleBit());
97+
98+
// Assert.True(factoryCompleted);
99+
//}
102100

103101
[Fact]
104102
public void CanCancelASyncFunc()

0 commit comments

Comments
 (0)