Skip to content

Commit c0c2a99

Browse files
author
Alex Peck
committed
add scoped test
1 parent 8886344 commit c0c2a99

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

BitFaster.Caching.UnitTests/Atomic/ScopedAsyncAtomicFactoryTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,49 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner()
156156
winnerCount.Should().Be(1);
157157
}
158158

159+
160+
[Fact]
161+
public async Task WhenCallersRunConcurrentlyWithFailureSameExceptionIsPropagated()
162+
{
163+
var enter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
164+
var resume = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
165+
166+
var atomicFactory = new ScopedAsyncAtomicFactory<int, IntHolder>();
167+
168+
var first = atomicFactory.TryCreateLifetimeAsync(1, async k =>
169+
{
170+
enter.SetResult(true);
171+
await resume.Task;
172+
173+
throw new ArithmeticException("1");
174+
}).AsTask(); ;
175+
176+
var second = atomicFactory.TryCreateLifetimeAsync(1, async k =>
177+
{
178+
enter.SetResult(true);
179+
await resume.Task;
180+
181+
throw new InvalidOperationException("2");
182+
}).AsTask();
183+
184+
await enter.Task;
185+
resume.SetResult(true);
186+
187+
// Both tasks will throw, but the first one to complete will propagate its exception
188+
// Both exceptions should be the same. If they are not, there will be an aggregate exception.
189+
try
190+
{
191+
await Task.WhenAll(first, second)
192+
.TimeoutAfter(TimeSpan.FromSeconds(5), "Tasks did not complete within the expected time. Exceptions are not propagated between callers correctly.");
193+
}
194+
catch (ArithmeticException)
195+
{
196+
}
197+
catch (InvalidOperationException)
198+
{
199+
}
200+
}
201+
159202
[Fact]
160203
public async Task WhenDisposedWhileInitResultIsDisposed()
161204
{

0 commit comments

Comments
 (0)