Skip to content

Commit

Permalink
fix: remove capture and dispatch to user sync context
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Jul 15, 2023
1 parent fd85b8d commit 4cb7458
Showing 1 changed file with 1 addition and 27 deletions.
28 changes: 1 addition & 27 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class TestRenderer : Renderer, ITestRenderer
private static readonly MethodInfo GetRequiredComponentStateMethod = RendererType.GetMethod("GetRequiredComponentState", BindingFlags.Instance | BindingFlags.NonPublic)!;

private readonly object renderTreeUpdateLock = new();
private readonly SynchronizationContext? usersSyncContext = SynchronizationContext.Current;
private readonly Dictionary<int, IRenderedFragmentBase> renderedComponents = new();
private readonly List<RootComponent> rootComponents = new();
private readonly ILogger<TestRenderer> logger;
Expand Down Expand Up @@ -264,7 +263,7 @@ protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
{
var renderEvent = new RenderEvent();
PrepareRenderEvent(renderBatch);
InvokeApplyRenderEvent();
ApplyRenderEvent(renderEvent);

return Task.CompletedTask;

Expand Down Expand Up @@ -332,31 +331,6 @@ void LoadChangesIntoRenderEvent(int componentId)
}
}
}

void InvokeApplyRenderEvent()
{
if (usersSyncContext is not null && usersSyncContext != SynchronizationContext.Current)
{
// The users' sync context, typically one established by
// xUnit or another testing framework is used to update any
// rendered fragments/dom trees and trigger WaitForX handlers.
// This ensures that changes to DOM observed inside a WaitForX handler
// will also be visible outside a WaitForX handler, since
// they will be running in the same sync context. The theory is that
// this should mitigate the issues where Blazor's dispatcher/thread is used
// to verify an assertion inside a WaitForX handler, and another thread is
// used again to access the DOM/repeat the assertion, where the change
// may not be visible yet (another theory about why that may happen is different
// CPU cache updates not happening immediately).
//
// There is no guarantee a caller/test framework has set a sync context.
usersSyncContext.Send(_ => ApplyRenderEvent(renderEvent), null);
}
else
{
ApplyRenderEvent(renderEvent);
}
}
}

private void ApplyRenderEvent(RenderEvent renderEvent)
Expand Down

0 comments on commit 4cb7458

Please sign in to comment.