Skip to content

Commit

Permalink
refactor: Remove SyncContext anc create new renderer instance
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Jul 14, 2023
1 parent 4d0d5a3 commit 4321ffc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 85 deletions.
23 changes: 13 additions & 10 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 SynchronizationContext? usersSyncContext;
private readonly Dictionary<int, IRenderedFragmentBase> renderedComponents = new();
private readonly List<RootComponent> rootComponents = new();
private readonly ILogger<TestRenderer> logger;
Expand Down Expand Up @@ -47,21 +47,23 @@ private bool IsBatchInProgress
/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, bool useSynchronizationContext = true)
: base(services, loggerFactory)
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
activator = renderedComponentActivator;
usersSyncContext = useSynchronizationContext ? SynchronizationContext.Current : null;
}
#elif NET5_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, bool useSynchronizationContext = true)
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
activator = renderedComponentActivator;
usersSyncContext = useSynchronizationContext ? SynchronizationContext.Current : null;
}

/// <summary>
Expand All @@ -71,7 +73,8 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
activator = renderedComponentActivator;
usersSyncContext = SynchronizationContext.Current;
}
#endif

Expand Down Expand Up @@ -342,11 +345,11 @@ void InvokeApplyRenderEvent()
// 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
// 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
// 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.
Expand Down
9 changes: 8 additions & 1 deletion src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Rendering;
using Microsoft.Extensions.Logging;

namespace Bunit;

/// <summary>
Expand Down Expand Up @@ -298,7 +300,12 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
if (expected is null)
throw new ArgumentNullException(nameof(expected));

var detachedRenderer = actual.Services.GetRequiredService<MarkupMatchesRenderer>();
using var detachedRenderer = new TestRenderer(
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
actual.Services.GetRequiredService<TestServiceProvider>(),
actual.Services.GetRequiredService<ILoggerFactory>(),
useSynchronizationContext: false);

var renderedFragment = (IRenderedFragment)detachedRenderer.RenderFragment(expected);
MarkupMatches(actual, renderedFragment, userMessage);
}
Expand Down
1 change: 0 additions & 1 deletion src/bunit.web/Extensions/TestServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
// bUnit specific services
services.AddScoped<TestContextBase>(_ => testContext);
services.AddScoped<WebTestRenderer>();
services.AddScoped<MarkupMatchesRenderer>();
services.AddScoped<TestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
services.AddScoped<Renderer>(s => s.GetRequiredService<WebTestRenderer>());
services.AddScoped<ITestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
Expand Down
23 changes: 0 additions & 23 deletions src/bunit.web/Rendering/Internal/MarkupMatchesRenderer.cs

This file was deleted.

50 changes: 0 additions & 50 deletions src/bunit.web/Rendering/Internal/NullDispatcher.cs

This file was deleted.

0 comments on commit 4321ffc

Please sign in to comment.