Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Changed

- The markup of a rendered component is generated when it is read (`Markup`, `Nodes`, `Find`) instead of after every render batch. A component returned to a test was notified once per changed child in a batch and regenerated its full markup each time; on large components this dominated test run time. `OnMarkupUpdated` is still raised after each render that changes the markup.

## [2.15.2] - 2026-11-10

### Added
Expand Down
14 changes: 14 additions & 0 deletions src/bunit/Rendering/BunitRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public sealed class BunitRenderer : Renderer
private TaskCompletionSource<Exception> unhandledExceptionTsc = new(TaskCreationOptions.RunContinuationsAsynchronously);
private Exception? capturedUnhandledException;

/// <summary>
/// Runs <paramref name="read"/> while no render batch can change the render tree, e.g. to generate
/// the markup of a rendered component on demand. Re-entrant: a caller that already holds the lock,
/// such as an <c>OnMarkupUpdated</c> handler invoked from <see cref="UpdateDisplayAsync"/>, runs directly.
/// Returns <paramref name="fallback"/> once the renderer is disposed.
/// </summary>
internal T ReadRenderTree<T>(Func<T> read, T fallback)
{
lock (renderTreeUpdateLock)
{
return disposed ? fallback : read();
}
}

private bool IsBatchInProgress
{
#pragma warning disable S1144 // Unused private types or members should be removed
Expand Down
31 changes: 30 additions & 1 deletion src/bunit/Rendering/RenderedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ internal sealed class RenderedComponent<TComponent> : ComponentState, IRenderedC
private string markup = string.Empty;
private MarkupNodes? latestRenderNodes;

// Set when a render changed this component's subtree; the markup is generated on the next read of
// Markup instead of after every render batch. A component returned to a test (cut, FindComponent)
// is notified once per changed child in a batch, so eager generation rebuilt the same markup many
// times per batch (measured: a 100 KB page regenerated 44-284 times per state change).
private volatile bool markupStale;

/// <summary>
/// Gets the component under test.
/// </summary>
Expand All @@ -45,6 +51,25 @@ public string Markup
get
{
EnsureComponentNotDisposed();

if (markupStale)
{
// Generated under the renderer's render tree lock, like FindComponents, so no batch can
// change the tree while it is read. The flag is re-checked inside the lock: another
// thread may have generated the markup meanwhile.
return renderer.ReadRenderTree(
() =>
{
if (markupStale)
{
UpdateMarkup();
}

return Volatile.Read(ref markup);
},
Volatile.Read(ref markup));
}

// Volatile read is necessary to ensure the updated markup
// is available across CPU cores. Without it, the pointer to the
// markup string can be stored in a CPUs register and not
Expand Down Expand Up @@ -126,7 +151,9 @@ public void UpdateState(bool hasRendered, bool isMarkupGenerationRequired)

if (isMarkupGenerationRequired)
{
UpdateMarkup();
// Only mark the markup as stale; Markup generates it on the next read. Subscribers of
// OnMarkupUpdated (e.g. the WaitFor helpers) read it and so still see the new markup.
markupStale = true;
OnMarkupUpdated?.Invoke(this, EventArgs.Empty);
}

Expand All @@ -142,6 +169,7 @@ public void UpdateState(bool hasRendered, bool isMarkupGenerationRequired)
/// </summary>
private void UpdateMarkup()
{
markupStale = false;
latestRenderNodes = null;
var newMarkup = Htmlizer.GetHtml(ComponentId, renderer);

Expand Down Expand Up @@ -169,6 +197,7 @@ public void Dispose()
return;

IsDisposed = true;
markupStale = false;
markup = string.Empty;
OnAfterRender = null;
OnMarkupUpdated = null;
Expand Down
50 changes: 50 additions & 0 deletions tests/bunit.tests/Rendering/RenderedComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,56 @@ public void Test030()
cut.Find("p").TextContent.ShouldBe("2");
}

[Fact(DisplayName = "Markup returns the same instance on repeated reads when nothing rendered in between")]
public void Test031()
{
var cut = Render<ClickCounter>();

var first = cut.Markup;
var second = cut.Markup;

second.ShouldBeSameAs(first);
}

[Fact(DisplayName = "Markup reflects the last of several renders that were not read in between")]
public void Test032()
{
var cut = Render<ClickCounter>();
var button = cut.Find("button");

button.Click();
button.Click();
button.Click();

cut.Find("p").TextContent.ShouldBe("3");
}

[Fact(DisplayName = "OnMarkupUpdated subscribers read the updated markup")]
public void Test033()
{
var cut = Render<ClickCounter>();
var markupSeenBySubscriber = new List<string>();
cut.OnMarkupUpdated += (_, _) => markupSeenBySubscriber.Add(cut.Markup);

cut.Find("button").Click();

markupSeenBySubscriber.ShouldNotBeEmpty();
markupSeenBySubscriber[^1].ShouldContain("<p>1</p>");
}

[Fact(DisplayName = "Markup of a parent reflects a render of a child component found via FindComponent")]
public void Test034()
{
var cut = Render<Wrapper>(ps => ps.AddChildContent<ClickCounter>());
_ = cut.Markup;
var child = cut.FindComponent<ClickCounter>();

child.Find("button").Click();

child.Find("p").TextContent.ShouldBe("1");
cut.Find("p").TextContent.ShouldBe("1");
}

private class BaseComponent : ComponentBase
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
Expand Down