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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad

### Fixed

- `WrapperBase.WrappedElement` could return `null` and throw `NullReferenceException` when an element was accessed concurrently with component re-renders. Reported by [@psu-prog](https://github.com/psu-prog) in #1937. Fixed by [@Nacl-HCl](https://github.com/Nacl-HCl).
- `DisposeComponentsAsync()` could dispose nothing when the renderer's dispatcher was busy, e.g. with a timer calling `InvokeAsync`. Reported by [@xfunc-tim-schneider](https://github.com/xfunc-tim-schneider) in #1939. Fixed by [@linkdotnet](https://github.com/linkdotnet).

## [2.11.3] - 2026-09-13
Expand Down
8 changes: 1 addition & 7 deletions src/bunit.generators.internal/Web.AngleSharp/WrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public abstract class WrapperBase<TElement> : IElementWrapper<TElement>
public TElement WrappedElement
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (element is null)
element = elementFactory.GetElement<TElement>();

return element;
}
get => element ??= elementFactory.GetElement<TElement>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: WrapperBase.g.cs
//HintName: WrapperBase.g.cs
#nullable enable
using System.CodeDom.Compiler;
using System.Diagnostics;
Expand All @@ -25,13 +25,7 @@ public abstract class WrapperBase<TElement> : IElementWrapper<TElement>
public TElement WrappedElement
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (element is null)
element = elementFactory.GetElement<TElement>();

return element;
}
get => element ??= elementFactory.GetElement<TElement>();
}

/// <summary>
Expand Down
47 changes: 47 additions & 0 deletions tests/bunit.tests/Extensions/RefreshingWrappedElementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,53 @@ public void Test031()
Should.Throw<ElementRemovedFromDomException>(() => btn.TextContent);
}

[Fact(DisplayName = "Found element is never null when accessed concurrently with component re-renders")]
public async Task Test040()
{
var cut = Render<Counter>();
var elm = cut.Find("span");
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Xunit.TestContext.Current.CancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(1));
var renders = Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
{
await cut.InvokeAsync(cut.Instance.Increment);
}
}, Xunit.TestContext.Current.CancellationToken);

try
{
while (!cts.IsCancellationRequested)
{
elm.TextContent.ShouldNotBeNull();
}
}
finally
{
await cts.CancelAsync();
await renders;
}
}

private sealed class Counter : ComponentBase
{
private int count;

public void Increment()
{
count++;
StateHasChanged();
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "span");
builder.AddContent(1, count);
builder.CloseElement();
}
}

private sealed class Markup : ComponentBase
{
[Parameter] public string Base { get; set; } = string.Empty;
Expand Down
Loading