Revert "Disconnect circuit on 'beforeunload' event" (#26297)

* Add tests for failing disconnect scenarios

* Remove beforeunload call and add public API

* Add additional test case

* Update src/Components/test/testassets/BasicTestApp/GracefulTermination.razor

Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>

Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>
This commit is contained in:
Safia Abdalla 2020-09-25 10:11:26 -07:00 committed by GitHub
parent 284a270583
commit 2e7b294c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -67,7 +67,8 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
}
};
window.addEventListener('beforeunload', cleanup, { capture: false, once: true });
window['Blazor'].disconnect = cleanup;
window.addEventListener('unload', cleanup, { capture: false, once: true });
window['Blazor'].reconnect = reconnect;

View File

@ -44,8 +44,8 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
protected override void InitializeAsyncCore()
{
Navigate(ServerPathBase, noReload: false);
Browser.MountTestComponent<CounterComponent>();
Browser.Equal("Current count: 0", () => Browser.FindElement(By.TagName("p")).Text);
Browser.MountTestComponent<GracefulTermination>();
Browser.Equal("Graceful Termination", () => Browser.FindElement(By.TagName("h1")).Text);
GracefulDisconnectCompletionSource = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Sink = _serverFixture.Host.Services.GetRequiredService<TestSink>();
@ -91,6 +91,45 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
}
[Fact]
public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurrentCircuit()
{
// Arrange & Act
var element = Browser.FindElement(By.Id("mailto-link"));
element.Click();
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
// Assert
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
}
[Fact]
public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
{
// Arrange & Act
var element = Browser.FindElement(By.Id("download-link"));
element.Click();
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
// Assert
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
}
[Fact]
public async Task DownloadHref_DoesNotGracefullyDisconnect_TheCurrentCircuit()
{
// Arrange & Act
var element = Browser.FindElement(By.Id("download-href"));
element.Click();
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
// Assert
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
}
private void Log(WriteContext wc)
{
if ((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully") == (wc.LogLevel, wc.EventId.Name))

View File

@ -0,0 +1,14 @@
@inject NavigationManager navigationManager
<h1>Graceful Termination</h1>
<a href="mailto:test@example.com" id="mailto-link">Send Email</a>
<a href="download" download id="download-href">Download Link</a>
<button @onclick="DownloadFile" id="download-link">Download File</button>
@code {
private void DownloadFile()
{
navigationManager.NavigateTo("/subdir/download", true);
}
}

View File

@ -40,6 +40,7 @@
<option value="BasicTestApp.FormsTest.InputFileComponent">Input file</option>
<option value="BasicTestApp.NavigateOnSubmit">Navigate to submit</option>
<option value="BasicTestApp.GlobalizationBindCases">Globalization Bind Cases</option>
<option value="BasicTestApp.GracefulTermination">Graceful Termination</option>
<option value="BasicTestApp.HierarchicalImportsTest.Subdir.ComponentUsingImports">Imports statement</option>
<option value="BasicTestApp.HtmlBlockChildContent">ChildContent HTML Block</option>
<option value="BasicTestApp.HtmlEncodedChildContent">ChildContent HTML Encoded Block</option>

View File

@ -0,0 +1,22 @@
using System;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Mvc;
namespace Components.TestServer.Controllers
{
public class DownloadController : Controller
{
[HttpGet("~/download")]
public FileStreamResult Download()
{
var buffer = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog.");
var stream = new MemoryStream(buffer);
var result = new FileStreamResult(stream, "text/plain");
result.FileDownloadName = "test.txt";
return result;
}
}
}

View File

@ -39,6 +39,7 @@ namespace TestServer
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapControllerRoute("mvc", "{controller}/{action}");
endpoints.MapFallbackToPage("/_ServerHost");
});
});