Skip to content

Commit f1aeb79

Browse files
Fix LocationChangingHandlers_CannotCancelTheNavigationAsynchronously_UntilReturning (#54388)
1 parent 5d61ff1 commit f1aeb79

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Components/Components/test/NavigationManagerTest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ public async Task LocationChangingHandlers_CannotCancelTheNavigationAsynchronous
805805
// Arrange
806806
var baseUri = "scheme://host/";
807807
var navigationManager = new TestNavigationManager(baseUri);
808+
var blockPreventNavigationTcs = new TaskCompletionSource();
808809
var navigationPreventedTcs = new TaskCompletionSource();
809810
var completeHandlerTcs = new TaskCompletionSource();
810811
LocationChangingContext currentContext = null;
@@ -814,10 +815,14 @@ public async Task LocationChangingHandlers_CannotCancelTheNavigationAsynchronous
814815
// Act/Assert
815816
var navigation1 = navigationManager.RunNotifyLocationChangingAsync($"{baseUri}/subdir1", null, false);
816817

818+
// Unblock the location changing handler to let it cancel the navigation, now that we know the
819+
// navigation wasn't canceled synchronously
820+
blockPreventNavigationTcs.SetResult();
821+
817822
// Wait for the navigation to be prevented asynchronously
818823
await navigationPreventedTcs.Task.WaitAsync(Timeout);
819824

820-
// Assert that we have prevented the navigation but the cancellation token has requested cancellation
825+
// Assert that we have prevented the navigation but the cancellation token has not requested cancellation
821826
Assert.True(currentContext.DidPreventNavigation);
822827
Assert.False(currentContext.CancellationToken.IsCancellationRequested);
823828

@@ -835,7 +840,7 @@ async ValueTask HandleLocationChanging(LocationChangingContext context)
835840
currentContext = context;
836841

837842
// Force the navigation to be prevented asynchronously
838-
await Task.Yield();
843+
await blockPreventNavigationTcs.Task;
839844

840845
context.PreventNavigation();
841846
navigationPreventedTcs.SetResult();

0 commit comments

Comments
 (0)