Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUnit render component to string deadlocks when using in a Blazor context > 1.18.4 #1144

Closed
biohazard999 opened this issue Jul 7, 2023 · 9 comments
Labels
investigate This issue require further investigation before closing.

Comments

@biohazard999
Copy link

I use BUnit to render a component to a string inside a blazor application to support rendering markdown.

Currently i'm using it somewhat like this:

var context = new TestContext();

context.Services.AddFallbackServiceProvider(ServiceProvider);

var rendered = context.Render(ChildContent);
var markup = MarkdownStringUtils.TrimLeftSpaces(rendered.Markup);

var html = Markdown.ToHtml(markup, Pipeline);
_markupString = new MarkupString(html);

All versions after 1.18.4 end in a deadlock rendering the component to html

I guess the crucket is here: 7c15692

I tried to wrap my code into a InvokeAsync etc but still get deadlocked (the render pipeline just never passes Render(ChildContent)).

I know this is not the primary use case for bunit, and i am waiting for the native net8.0 implementation to render a component to a string from microsoft.

Are there any workarounds i could use?

I could provide a small repro if needed.

@egil
Copy link
Member

egil commented Jul 7, 2023

I could provide a small repro if needed.

Yes please. I'd be very curious to see what is in the RenderFragment.

You can also try the latest preview 1.22.xx. It attempts to solve the #1064 issue which may be related.

@egil egil added the investigate This issue require further investigation before closing. label Jul 7, 2023
@biohazard999
Copy link
Author

@egil here you go: https://github.com/biohazard999/bunit-repro-1144

does not change with the latest preview. Double checked downgrade to 1.18.4 works as expected.

@egil
Copy link
Member

egil commented Jul 8, 2023

Thanks. I'll try to take a look in the coming days.

@biohazard999
Copy link
Author

This allows me to write markdown in blazor components (and also do some nice stuff related to interpreting the markdown)

image

Thank you! :) If it's out of scope for bunit, a hint how to do this on my own is also very appreciated.

@egil
Copy link
Member

egil commented Jul 8, 2023

I think it is sort of out of scope, but it should also work without problems, so I want to understand what's going on because it could also affect others in test scenarios.

That said, as part of net 8, the functionality you need will be built-in to the framework. Look for the blog post for preview 2 or 3 for details.

@biohazard999
Copy link
Author

biohazard999 commented Jul 8, 2023

Given you are on net8.0 preview 3 you can define a MarkdownRenderer.razor component:

@ChildContent
@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; } = default!;
}

After that it's possible to workaround like so:

ILoggerFactory loggerFactory = ServiceProvider.GetRequiredService<ILoggerFactory>();

await using var htmlRenderer = new HtmlRenderer(ServiceProvider, loggerFactory);

var markup = await htmlRenderer.Dispatcher.InvokeAsync(async () =>
{
    var parameters = ParameterView.FromDictionary(new Dictionary<string, object?>
    {
        [nameof(MarkdownRenderer.ChildContent)] = ChildContent
    });

    var output = await htmlRenderer.RenderComponentAsync<MarkdownRenderer>(parameters);
    return output.ToHtmlString();
});

markup = MarkdownStringUtils.TrimLeftSpaces(markup);

var html = Markdown.ToHtml(markup, Pipeline);
_markupString = new MarkupString(html);

This should be a hint for other's how to workaround this problem.

I will stick with 1.18.4 for my net6.0 impl and drop bunit in production for > net8.0.

Thanks @egil :)

@egil
Copy link
Member

egil commented Jul 15, 2023

@biohazard999 can you try again with the 1.22.16-preview release and see if that solves the issue for you.

@biohazard999
Copy link
Author

@egil works 👏👏

@egil egil closed this as completed Jul 18, 2023
@egil
Copy link
Member

egil commented Jul 18, 2023

Great. Still have a few other issues to solve before we make a none-preview release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate This issue require further investigation before closing.
Projects
None yet
Development

No branches or pull requests

2 participants