Skip to content

Commit dccdfa0

Browse files
committed
fix: TestRenderer resets UnhandledException together with capturedUnhandledException
1 parent 85e6bc0 commit dccdfa0

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public Task DispatchEventAsync(
7676

7777
var result = Dispatcher.InvokeAsync(() =>
7878
{
79+
ResetUnhandledException();
80+
7981
try
8082
{
8183
return base.DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs);
@@ -127,6 +129,8 @@ public void DisposeComponents()
127129
// will only work on IDisposable
128130
var disposeTask = Dispatcher.InvokeAsync(() =>
129131
{
132+
ResetUnhandledException();
133+
130134
foreach (var root in rootComponents)
131135
{
132136
root.Detach();
@@ -142,20 +146,6 @@ public void DisposeComponents()
142146
AssertNoUnhandledExceptions();
143147
}
144148

145-
/// <inheritdoc/>
146-
protected override void HandleException(Exception exception)
147-
{
148-
capturedUnhandledException = exception;
149-
150-
logger.LogUnhandledException(capturedUnhandledException);
151-
152-
if (!unhandledExceptionTsc.TrySetResult(capturedUnhandledException))
153-
{
154-
unhandledExceptionTsc = new TaskCompletionSource<Exception>();
155-
unhandledExceptionTsc.SetResult(capturedUnhandledException);
156-
}
157-
}
158-
159149
/// <inheritdoc/>
160150
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
161151
{
@@ -219,10 +209,10 @@ protected override void Dispose(bool disposing)
219209
private TResult Render<TResult>(RenderFragment renderFragment, Func<int, TResult> activator)
220210
where TResult : IRenderedFragmentBase
221211
{
222-
ResetUnhandledException();
223-
224212
var renderTask = Dispatcher.InvokeAsync(() =>
225213
{
214+
ResetUnhandledException();
215+
226216
var root = new RootComponent(renderFragment);
227217
var rootComponentId = AssignRootComponentId(root);
228218
var result = activator(rootComponentId);
@@ -350,7 +340,30 @@ private ArrayRange<RenderTreeFrame> GetOrLoadRenderTreeFrame(RenderTreeFrameDict
350340
return framesCollection[componentId];
351341
}
352342

353-
private void ResetUnhandledException() => capturedUnhandledException = null;
343+
/// <inheritdoc/>
344+
protected override void HandleException(Exception exception)
345+
{
346+
if (exception is null)
347+
return;
348+
349+
logger.LogUnhandledException(exception);
350+
351+
capturedUnhandledException = exception;
352+
353+
if (!unhandledExceptionTsc.TrySetResult(capturedUnhandledException))
354+
{
355+
unhandledExceptionTsc = new TaskCompletionSource<Exception>();
356+
unhandledExceptionTsc.SetResult(capturedUnhandledException);
357+
}
358+
}
359+
360+
private void ResetUnhandledException()
361+
{
362+
capturedUnhandledException = null;
363+
364+
if (unhandledExceptionTsc.Task.IsCompleted)
365+
unhandledExceptionTsc = new TaskCompletionSource<Exception>();
366+
}
354367

355368
private void AssertNoUnhandledExceptions()
356369
{

tests/bunit.core.tests/Rendering/TestRendererTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,6 @@ public async Task Test202()
391391
RenderComponent<AsyncOperationThrows>(ps => ps.Add(p => p.Awaitable, tsc2.Task));
392392
tsc2.SetException(secondException);
393393

394-
await Task.Delay(50);
395-
396394
var secondExceptionReported = await Renderer.UnhandledException;
397395
secondExceptionReported.ShouldBe(secondException);
398396
firstExceptionReported.ShouldNotBe(secondException);

0 commit comments

Comments
 (0)