-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncTemplateMethodTests.cs
More file actions
235 lines (194 loc) · 8.48 KB
/
AsyncTemplateMethodTests.cs
File metadata and controls
235 lines (194 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using PatternKit.Behavioral.Template;
using TinyBDD;
using TinyBDD.Xunit;
using Xunit.Abstractions;
namespace PatternKit.Tests.Behavioral;
[Feature("Behavioral - AsyncTemplateMethod<TContext,TResult> (async skeleton with hooks)")]
public sealed class AsyncTemplateMethodTests(ITestOutputHelper output) : TinyBddXunitBase(output)
{
private sealed class SampleAsyncTemplate : AsyncTemplateMethod<int, string>
{
public int BeforeCount;
public int AfterCount;
public int Concurrent;
public int MaxConcurrent;
private readonly int _delayMs;
private readonly bool _sync;
public SampleAsyncTemplate(int delayMs = 10, bool sync = false)
{
_delayMs = delayMs;
_sync = sync;
}
protected override bool Synchronized => _sync;
protected override async ValueTask OnBeforeAsync(int context, CancellationToken cancellationToken)
{
Interlocked.Increment(ref BeforeCount);
var c = Interlocked.Increment(ref Concurrent);
Volatile.Write(ref MaxConcurrent, Math.Max(Volatile.Read(ref MaxConcurrent), c));
await Task.Yield();
}
protected override async ValueTask<string> StepAsync(int context, CancellationToken cancellationToken)
{
await Task.Delay(_delayMs, cancellationToken);
return $"R-{context}";
}
protected override async ValueTask OnAfterAsync(int context, string result, CancellationToken cancellationToken)
{
Interlocked.Decrement(ref Concurrent);
Interlocked.Increment(ref AfterCount);
await Task.Yield();
}
}
[Scenario("ExecuteAsync runs hooks and returns result")]
[Fact]
public Task ExecuteAsync_RunsHooks_And_Returns()
=> Given("a sample async template", () => new SampleAsyncTemplate())
.When("ExecuteAsync(7)", t => (tpl: t, res: t.ExecuteAsync(7).GetAwaiter().GetResult()))
.Then("result is R-7", r => r.res == "R-7")
.And("before called once", r => r.tpl.BeforeCount == 1)
.And("after called once", r => r.tpl.AfterCount == 1)
.AssertPassed();
[Scenario("Not synchronized allows concurrency")]
[Fact(Timeout = 30_000)]
public Task Allows_Concurrency_When_Not_Synchronized()
=> Given("a non-synchronized template with delay", () => new SampleAsyncTemplate(delayMs: 25, sync: false))
.When("ExecuteAsync on 1..4 in parallel", t =>
{
var tasks = new[] { t.ExecuteAsync(1), t.ExecuteAsync(2), t.ExecuteAsync(3), t.ExecuteAsync(4) };
var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
return (tpl: t, results);
})
.Then("contains R-1", r => r.results.Contains("R-1"))
.And("max concurrency > 1", r => r.tpl.MaxConcurrent > 1)
.AssertPassed();
[Scenario("Synchronized serializes ExecuteAsync calls")]
[Fact]
public Task Serializes_When_Synchronized()
=> Given("a synchronized template with delay", () => new SampleAsyncTemplate(delayMs: 20, sync: true))
.When("ExecuteAsync on 1..4 in parallel", t =>
{
var tasks = new[] { t.ExecuteAsync(1), t.ExecuteAsync(2), t.ExecuteAsync(3), t.ExecuteAsync(4) };
var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
return (tpl: t, results);
})
.Then("contains R-1", r => r.results.Contains("R-1"))
.And("max concurrency == 1", r => r.tpl.MaxConcurrent == 1)
.AssertPassed();
[Scenario("Cancellation is observed")]
[Fact]
public async Task Cancellation_Observed()
{
// Use a long delay with a pre-cancelled token to avoid timing races
var template = new SampleAsyncTemplate(delayMs: 5000);
using var cts = new CancellationTokenSource();
cts.Cancel(); // pre-cancel so cancellation is immediate and deterministic
// Assert.ThrowsAnyAsync verifies that OperationCanceledException or derived types are thrown
await Assert.ThrowsAnyAsync<OperationCanceledException>(
async () => await template.ExecuteAsync(1, cts.Token));
}
}
#region Additional AsyncTemplateMethod Tests
public sealed class AsyncTemplateMethodEdgeCaseTests
{
private sealed class MinimalAsyncTemplate : AsyncTemplateMethod<int, string>
{
protected override ValueTask<string> StepAsync(int context, CancellationToken cancellationToken)
=> new($"Result-{context}");
}
private sealed class HooksAsyncTemplate : AsyncTemplateMethod<int, string>
{
public List<string> Log { get; } = [];
protected override async ValueTask OnBeforeAsync(int context, CancellationToken cancellationToken)
{
await Task.Yield();
Log.Add($"before:{context}");
}
protected override ValueTask<string> StepAsync(int context, CancellationToken cancellationToken)
=> new($"step:{context}");
protected override async ValueTask OnAfterAsync(int context, string result, CancellationToken cancellationToken)
{
await Task.Yield();
Log.Add($"after:{result}");
}
}
private sealed class ThrowingStepTemplate : AsyncTemplateMethod<int, string>
{
public int BeforeCount;
public int AfterCount;
public bool Sync;
protected override bool Synchronized => Sync;
protected override ValueTask OnBeforeAsync(int context, CancellationToken cancellationToken)
{
BeforeCount++;
return default;
}
protected override ValueTask<string> StepAsync(int context, CancellationToken cancellationToken)
=> throw new InvalidOperationException("Step failed");
protected override ValueTask OnAfterAsync(int context, string result, CancellationToken cancellationToken)
{
AfterCount++;
return default;
}
}
[Fact]
public async Task MinimalTemplate_NoHooks_Works()
{
var template = new MinimalAsyncTemplate();
var result = await template.ExecuteAsync(42);
Assert.Equal("Result-42", result);
}
[Fact]
public async Task HooksTemplate_ExecutesInOrder()
{
var template = new HooksAsyncTemplate();
var result = await template.ExecuteAsync(5);
Assert.Equal("step:5", result);
Assert.Equal(2, template.Log.Count);
Assert.Equal("before:5", template.Log[0]);
Assert.Equal("after:step:5", template.Log[1]);
}
[Fact]
public async Task ThrowingStep_NotSynchronized_DoesNotCallAfter()
{
var template = new ThrowingStepTemplate { Sync = false };
await Assert.ThrowsAsync<InvalidOperationException>(() => template.ExecuteAsync(1));
Assert.Equal(1, template.BeforeCount);
Assert.Equal(0, template.AfterCount);
}
[Fact]
public async Task ThrowingStep_Synchronized_MutexIsReleased()
{
var template = new ThrowingStepTemplate { Sync = true };
await Assert.ThrowsAsync<InvalidOperationException>(() => template.ExecuteAsync(1));
// The mutex should be released, so a second call should not deadlock
await Assert.ThrowsAsync<InvalidOperationException>(() => template.ExecuteAsync(2));
Assert.Equal(2, template.BeforeCount);
}
[Fact]
public async Task Synchronized_Cancellation_DuringMutexWait()
{
var template = new SynchronizedSlowTemplate();
// Start first execution that holds mutex
var first = template.ExecuteAsync(1);
// Try to acquire with immediate cancellation
var cts = new CancellationTokenSource();
cts.Cancel();
// TaskCanceledException is a subclass of OperationCanceledException
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => template.ExecuteAsync(2, cts.Token));
Assert.True(ex is OperationCanceledException or TaskCanceledException);
// First should still complete
var result = await first;
Assert.Equal("slow:1", result);
}
private sealed class SynchronizedSlowTemplate : AsyncTemplateMethod<int, string>
{
protected override bool Synchronized => true;
protected override async ValueTask<string> StepAsync(int context, CancellationToken cancellationToken)
{
await Task.Delay(50, cancellationToken);
return $"slow:{context}";
}
}
}
#endregion