Skip to content

Commit adf989b

Browse files
committed
fix: improve exception message to wait for issue
1 parent d25af3e commit adf989b

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

src/bunit.core/Extensions/WaitForHelpers/WaitForFailedException.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,36 @@ public WaitForFailedException(string? errorMessage, Exception? innerException =
1515
}
1616

1717
internal WaitForFailedException(string errorMessage, int checkCount, int componentRenderCount, int totalRenderCount, Exception? innerException = null)
18-
: base(errorMessage + $" Check count: {checkCount}. Component render count: {componentRenderCount}. Total render count: {totalRenderCount}.", innerException)
18+
: base(CreateMessage(errorMessage, checkCount, componentRenderCount, totalRenderCount), innerException)
1919
{
2020
}
2121

22+
private static string CreateMessage(
23+
string errorMessage,
24+
int checkCount,
25+
int componentRenderCount,
26+
int totalRenderCount)
27+
{
28+
return $"""
29+
{errorMessage}
30+
31+
If this test does not fail consistently, the reason may be that
32+
the wait timeout is too short, and the runtime did not have enough
33+
time to complete the necessary number of renders of the component under test.
34+
This can happen on highly utilized or slower hardware, for example.
35+
36+
To determine if this is the cause, compare the check and render count(s) below
37+
and see if they match what is expected. If they do not,
38+
consider increasing the timeout, either at the individual
39+
method call level, e.g. WaitForElement("div", TimeSpan.FromSeconds(15)),
40+
or via the static TestContext.DefaultWaitTimeout property.
41+
42+
Check count: {checkCount}.
43+
Component render count: {componentRenderCount}.
44+
Total render count across all components: {totalRenderCount}.
45+
""";
46+
}
47+
2248
private WaitForFailedException(SerializationInfo info, StreamingContext context)
2349
: base(info, context) { }
2450
}

src/bunit.core/Extensions/WaitForHelpers/WaitForHelper.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,24 @@ protected WaitForHelper(
5959
.GetRequiredService<TestContextBase>()
6060
.Renderer;
6161
checkPassedCompletionSource = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
62-
timer = new Timer(_ =>
63-
{
64-
logger.LogWaiterTimedOut(renderedFragment.ComponentId);
65-
checkPassedCompletionSource.TrySetException(
66-
new WaitForFailedException(
67-
TimeoutErrorMessage ?? string.Empty,
68-
checkCount,
69-
renderedFragment.RenderCount,
70-
renderer.RenderCount,
71-
capturedException));
72-
});
62+
7363
WaitTask = CreateWaitTask();
74-
timer.Change(GetRuntimeTimeout(timeout), Timeout.InfiniteTimeSpan);
64+
timer = new Timer(
65+
static (state) =>
66+
{
67+
var @this = (WaitForHelper<T>)state!;
68+
@this.logger.LogWaiterTimedOut(@this.renderedFragment.ComponentId);
69+
@this.checkPassedCompletionSource.TrySetException(
70+
new WaitForFailedException(
71+
@this.TimeoutErrorMessage ?? string.Empty,
72+
@this.checkCount,
73+
@this.renderedFragment.RenderCount,
74+
@this.renderer.RenderCount,
75+
@this.capturedException));
76+
},
77+
this,
78+
GetRuntimeTimeout(timeout),
79+
Timeout.InfiniteTimeSpan);
7580

7681
InitializeWaiting();
7782
}

tests/bunit.web.tests/Extensions/WaitForHelpers/RenderedFragmentWaitForElementsHelperExtensions.Async.Test.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public async Task Test022()
6161
expected.InnerException.ShouldBeNull();
6262
}
6363

64-
[Fact(DisplayName = "WaitForElements with specific count N throws exception after timeout when cssSelector does not result in N matching elements",
65-
Skip = "Need to figure out how to make this deterministic.")]
64+
[Fact(DisplayName = "WaitForElements with specific count N throws exception after timeout when cssSelector does not result in N matching elements")]
6665
[Trait("Category", "async")]
6766
public async Task Test023()
6867
{

tests/bunit.web.tests/Extensions/WaitForHelpers/RenderedFragmentWaitForElementsHelperExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void Test022()
6161
expected.InnerException.ShouldBeNull();
6262
}
6363

64-
[Fact(DisplayName = "WaitForElements with specific count N throws exception after timeout when cssSelector does not result in N matching elements", Skip = "Need to figure out how to make this deterministic.")]
64+
[Fact(DisplayName = "WaitForElements with specific count N throws exception after timeout when cssSelector does not result in N matching elements")]
6565
[Trait("Category", "sync")]
6666
public void Test023()
6767
{

0 commit comments

Comments
 (0)