|
1 | | -using System.Diagnostics; |
2 | | -using FusionCacheTests.Stuff; |
| 1 | +using FusionCacheTests.Stuff; |
3 | 2 | using Xunit; |
4 | | -using ZiggyCreatures.Caching.Fusion; |
5 | 3 | using ZiggyCreatures.Caching.Fusion.Internals; |
6 | 4 |
|
7 | 5 | namespace FusionCacheTests; |
8 | 6 |
|
9 | 7 | public partial class RunUtilsTests |
10 | 8 | { |
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 | + //} |
102 | 100 |
|
103 | 101 | [Fact] |
104 | 102 | public void CanCancelASyncFunc() |
|
0 commit comments