Skip to content

Commit 8886344

Browse files
author
Alex Peck
committed
cleanup
1 parent 035ae54 commit 8886344

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,48 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner()
114114
winnerCount.Should().Be(1);
115115
}
116116

117+
[Fact]
118+
public async Task WhenCallersRunConcurrentlyWithFailureSameExceptionIsPropagated()
119+
{
120+
var enter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
121+
var resume = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
122+
123+
var atomicFactory = new AsyncAtomicFactory<int, int>();
124+
125+
var first = atomicFactory.GetValueAsync(1, async k =>
126+
{
127+
enter.SetResult(true);
128+
await resume.Task;
129+
130+
throw new ArithmeticException("1");
131+
}).AsTask();
132+
133+
var second = atomicFactory.GetValueAsync(1, async k =>
134+
{
135+
enter.SetResult(true);
136+
await resume.Task;
137+
138+
throw new InvalidOperationException("2");
139+
}).AsTask();
140+
141+
await enter.Task;
142+
resume.SetResult(true);
143+
144+
// Both tasks will throw, but the first one to complete will propagate its exception
145+
// Both exceptions should be the same. If they are not, there will be an aggregate exception.
146+
try
147+
{
148+
await Task.WhenAll(first, second)
149+
.TimeoutAfter(TimeSpan.FromSeconds(5), "Tasks did not complete within the expected time. Exceptions are not propagated between callers correctly.");
150+
}
151+
catch (ArithmeticException)
152+
{
153+
}
154+
catch (InvalidOperationException)
155+
{
156+
}
157+
}
158+
117159
[Fact]
118160
public void WhenValueNotCreatedHashCodeIsZero()
119161
{

0 commit comments

Comments
 (0)