|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Components.Rendering; |
| 5 | +using Microsoft.AspNetCore.Components.QuickGrid; |
| 6 | +using Microsoft.AspNetCore.Components.Test.Helpers; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.JSInterop; |
| 9 | +using Xunit.Sdk; |
| 10 | + |
| 11 | +namespace Microsoft.AspNetCore.Components.QuickGrid.Tests; |
| 12 | + |
| 13 | +public class GridRaceConditionTest |
| 14 | +{ |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public async Task CanCorrectlyDisposeAsync() |
| 18 | + { |
| 19 | + var moduleLoadCompletion = new TaskCompletionSource(); |
| 20 | + var moduleImportStarted = new TaskCompletionSource(); |
| 21 | + var testJsRuntime = new TestJsRuntime(moduleLoadCompletion, moduleImportStarted); |
| 22 | + var serviceProvider = new ServiceCollection() |
| 23 | + .AddSingleton<IJSRuntime>(testJsRuntime) |
| 24 | + .BuildServiceProvider(); |
| 25 | + var renderer = new TestRenderer(serviceProvider); |
| 26 | + |
| 27 | + var testComponent = new SimpleTestComponent(); |
| 28 | + |
| 29 | + var componentId = renderer.AssignRootComponentId(testComponent); |
| 30 | + renderer.RenderRootComponent(componentId); |
| 31 | + |
| 32 | + // Wait until JS import has started but not completed |
| 33 | + await moduleImportStarted.Task; |
| 34 | + |
| 35 | + // Dispose component while JS module loading is pending |
| 36 | + testJsRuntime.MarkDisposed(); |
| 37 | + await renderer.DisposeAsync(); |
| 38 | + |
| 39 | + // Complete the JS module loading |
| 40 | + moduleLoadCompletion.SetResult(); |
| 41 | + |
| 42 | + // Wait until after OnAfterRenderAsync has completed to test the disposal of the jsModule |
| 43 | + var notFailingGrid = testComponent.NotFailingGrid; |
| 44 | + await notFailingGrid.OnAfterRenderCompleted; |
| 45 | + |
| 46 | + // Assert that init was not called after disposal and JsModule was disposed of |
| 47 | + Assert.False(testJsRuntime.InitWasCalledAfterDisposal, |
| 48 | + "Init should not be called on a disposed component."); |
| 49 | + Assert.True(testJsRuntime.JsModuleDisposed); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task FailingQuickGridCallsInitAfterDisposal() |
| 54 | + { |
| 55 | + var moduleLoadCompletion = new TaskCompletionSource(); |
| 56 | + var moduleImportStarted = new TaskCompletionSource(); |
| 57 | + var testJsRuntime = new TestJsRuntime(moduleLoadCompletion, moduleImportStarted); |
| 58 | + var serviceProvider = new ServiceCollection() |
| 59 | + .AddSingleton<IJSRuntime>(testJsRuntime) |
| 60 | + .BuildServiceProvider(); |
| 61 | + var renderer = new TestRenderer(serviceProvider); |
| 62 | + |
| 63 | + var testComponent = new FailingGridTestComponent(); |
| 64 | + |
| 65 | + var componentId = renderer.AssignRootComponentId(testComponent); |
| 66 | + renderer.RenderRootComponent(componentId); |
| 67 | + |
| 68 | + // Wait until JS import has started but not completed |
| 69 | + await moduleImportStarted.Task; |
| 70 | + |
| 71 | + // Dispose component while JS module loading is pending |
| 72 | + testJsRuntime.MarkDisposed(); |
| 73 | + await renderer.DisposeAsync(); |
| 74 | + |
| 75 | + // Complete the JS module loading - this allows the FailingQuickGrid's OnAfterRenderAsync to continue |
| 76 | + // and demonstrate the race condition by calling init after disposal |
| 77 | + moduleLoadCompletion.SetResult(); |
| 78 | + |
| 79 | + // Wait until after OnAfterRenderAsync has completed, to make sure jsmodule import started and the reported issue is reproduced |
| 80 | + var failingGrid = testComponent.FailingQuickGrid; |
| 81 | + await failingGrid.OnAfterRenderCompleted; |
| 82 | + |
| 83 | + // Assert that init WAS called after disposal |
| 84 | + // The FailingQuickGrid's OnAfterRenderAsync should have called init despite being disposed |
| 85 | + // The FailingQuickGrid should not have disposed of JsModule |
| 86 | + Assert.True(testJsRuntime.InitWasCalledAfterDisposal, |
| 87 | + $"FailingQuickGrid should call init after disposal, demonstrating the race condition bug. " + |
| 88 | + $"InitWasCalledAfterDisposal: {testJsRuntime.InitWasCalledAfterDisposal}, " + |
| 89 | + $"DisposeAsyncWasCalled: {failingGrid.DisposeAsyncWasCalled}, " + |
| 90 | + $"_disposeBool is false: {failingGrid.IsWasDisposedFalse()}"); |
| 91 | + Assert.False(testJsRuntime.JsModuleDisposed); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +internal class Person |
| 96 | +{ |
| 97 | + public int Id { get; set; } |
| 98 | + public string Name { get; set; } = string.Empty; |
| 99 | +} |
| 100 | + |
| 101 | +internal abstract class BaseTestComponent<TGrid> : ComponentBase |
| 102 | + where TGrid : ComponentBase |
| 103 | +{ |
| 104 | + [Inject] public IJSRuntime JSRuntime { get; set; } = default!; |
| 105 | + |
| 106 | + protected TGrid _grid; |
| 107 | + public TGrid Grid => _grid; |
| 108 | + |
| 109 | + private readonly List<Person> _people = [ |
| 110 | + new() { Id = 1, Name = "John" }, |
| 111 | + new() { Id = 2, Name = "Jane" } |
| 112 | + ]; |
| 113 | + |
| 114 | + protected override void BuildRenderTree(RenderTreeBuilder builder) |
| 115 | + { |
| 116 | + builder.OpenComponent<TGrid>(0); |
| 117 | + builder.AddAttribute(1, "Items", _people.AsQueryable()); |
| 118 | + builder.AddAttribute(2, "ChildContent", (RenderFragment)(b => |
| 119 | + { |
| 120 | + b.OpenComponent<PropertyColumn<Person, int>>(0); |
| 121 | + b.AddAttribute(1, "Property", (System.Linq.Expressions.Expression<Func<Person, int>>)(p => p.Id)); |
| 122 | + b.CloseComponent(); |
| 123 | + })); |
| 124 | + builder.AddComponentReferenceCapture(3, component => _grid = (TGrid)component); |
| 125 | + builder.CloseComponent(); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +internal class SimpleTestComponent : BaseTestComponent<NotFailingGrid<Person>> |
| 130 | +{ |
| 131 | + public NotFailingGrid<Person> NotFailingGrid => Grid; |
| 132 | +} |
| 133 | + |
| 134 | +internal class FailingGridTestComponent : BaseTestComponent<FailingQuickGrid<Person>> |
| 135 | +{ |
| 136 | + public FailingQuickGrid<Person> FailingQuickGrid => Grid; |
| 137 | +} |
| 138 | + |
| 139 | +internal class TestJsRuntime(TaskCompletionSource moduleCompletion, TaskCompletionSource importStarted) : IJSRuntime |
| 140 | +{ |
| 141 | + private readonly TaskCompletionSource _moduleCompletion = moduleCompletion; |
| 142 | + private readonly TaskCompletionSource _importStarted = importStarted; |
| 143 | + private bool _disposed; |
| 144 | + |
| 145 | + public bool JsModuleDisposed { get; private set; } |
| 146 | + |
| 147 | + public bool InitWasCalledAfterDisposal { get; private set; } |
| 148 | + |
| 149 | + public async ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args = null) |
| 150 | + { |
| 151 | + if (identifier == "import" && args?.Length > 0 && args[0] is string modulePath && |
| 152 | + modulePath == "./_content/Microsoft.AspNetCore.Components.QuickGrid/QuickGrid.razor.js") |
| 153 | + { |
| 154 | + // Signal that import has started |
| 155 | + _importStarted.TrySetResult(); |
| 156 | + |
| 157 | + // Wait for test to control when import completes |
| 158 | + await _moduleCompletion.Task; |
| 159 | + return (TValue)(object)new TestJSObjectReference(this); |
| 160 | + } |
| 161 | + throw new InvalidOperationException($"Unexpected JS call: {identifier}"); |
| 162 | + } |
| 163 | + |
| 164 | + public void MarkDisposed() => _disposed = true; |
| 165 | + |
| 166 | + public void MarkJsModuleDisposed() => JsModuleDisposed = true; |
| 167 | + |
| 168 | + public void RecordInitCall() |
| 169 | + { |
| 170 | + if (_disposed) |
| 171 | + { |
| 172 | + InitWasCalledAfterDisposal = true; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args) => |
| 177 | + InvokeAsync<TValue>(identifier, args); |
| 178 | +} |
| 179 | + |
| 180 | +internal class TestJSObjectReference(TestJsRuntime jsRuntime) : IJSObjectReference |
| 181 | +{ |
| 182 | + private readonly TestJsRuntime _jsRuntime = jsRuntime; |
| 183 | + |
| 184 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) |
| 185 | + { |
| 186 | + if (identifier == "init") |
| 187 | + { |
| 188 | + _jsRuntime.RecordInitCall(); |
| 189 | + } |
| 190 | + return default!; |
| 191 | + } |
| 192 | + |
| 193 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args) => |
| 194 | + InvokeAsync<TValue>(identifier, args); |
| 195 | + |
| 196 | + public ValueTask DisposeAsync() { |
| 197 | + _jsRuntime.MarkJsModuleDisposed(); |
| 198 | + return default!; |
| 199 | + } |
| 200 | +} |
0 commit comments