From 7e58b3080c830f5f0f30ccc3d8ba55bbf63b595b Mon Sep 17 00:00:00 2001
From: Pranav K <prkrishn@hotmail.com>
Date: Thu, 12 Aug 2021 12:36:35 -0700
Subject: [PATCH] Add a test to verify HotReload events are not subscribed to
 as part of regular rendering (#35299)

Verification for https://github.com/dotnet/aspnetcore/issues/34414
---
 .../src/HotReload/HotReloadManager.cs         | 14 ++++++----
 .../ServerExecutionTests/PrerenderingTest.cs  | 28 +++++++++++++++----
 .../Controllers/ReloadController.cs           |  2 +-
 .../TestServer/PrerenderedStartup.cs          | 10 +++----
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/src/Components/Components/src/HotReload/HotReloadManager.cs b/src/Components/Components/src/HotReload/HotReloadManager.cs
index 699b20a0831..3361cf4e25a 100644
--- a/src/Components/Components/src/HotReload/HotReloadManager.cs
+++ b/src/Components/Components/src/HotReload/HotReloadManager.cs
@@ -1,7 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System;
 using System.Reflection.Metadata;
 using Microsoft.AspNetCore.Components.HotReload;
 
@@ -11,13 +10,16 @@ namespace Microsoft.AspNetCore.Components.HotReload
 {
     internal static class HotReloadManager
     {
-       internal static event Action? OnDeltaApplied;
+        public static event Action? OnDeltaApplied;
 
-        public static void DeltaApplied()
-        {
-            OnDeltaApplied?.Invoke();
-        }
+        /// <summary>
+        /// Gets a value that determines if OnDeltaApplied is subscribed to.
+        /// </summary>
+        public static bool IsSubscribedTo => OnDeltaApplied is not null;
 
+        /// <summary>
+        /// MetadataUpdateHandler event. This is invoked by the hot reload host via reflection.
+        /// </summary>
         public static void UpdateApplication(Type[]? _) => OnDeltaApplied?.Invoke();
     }
 }
diff --git a/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs b/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs
index 8036f0eee7e..f66890396a5 100644
--- a/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs
+++ b/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs
@@ -1,15 +1,11 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-using System;
 using System.Net;
-using System.Net.Http;
-using System.Threading.Tasks;
-using BasicTestApp;
+using System.Net.Http.Json;
 using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
 using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
 using Microsoft.AspNetCore.E2ETesting;
-using Microsoft.AspNetCore.Testing;
 using OpenQA.Selenium;
 using TestServer;
 using Xunit;
@@ -138,6 +134,28 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
             Browser.Equal($"Hello, {interactiveUsername ?? "anonymous"}!", () => Browser.Exists(By.TagName("h1")).Text);
         }
 
+        [Fact]
+        public async Task NoHotReloadListenersAreOrdinarilyRegistered()
+        {
+            Navigate("/prerendered/prerendered-transition");
+
+            // Prerendered output shows "not connected"
+            Browser.Equal("not connected", () => Browser.Exists(By.Id("connected-state")).Text);
+
+            // Once connected, output changes
+            BeginInteractivity();
+            Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text);
+
+            // Once connected, output changes
+            BeginInteractivity();
+            Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text);
+
+            // Now query the hot reload manager and verify nothing is still wired up by default.
+            var httpClient = new HttpClient { BaseAddress = _serverFixture.RootUri };
+            var hasEventHandlers = await httpClient.GetFromJsonAsync<bool>("/prerendered/ishotreloadsubscribedto");
+            Assert.False(hasEventHandlers);
+        }
+
         private void BeginInteractivity()
         {
             Browser.Exists(By.Id("load-boot-script")).Click();
diff --git a/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs b/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs
index cf71bbed366..87cc90cfd38 100644
--- a/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs
+++ b/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs
@@ -12,7 +12,7 @@ namespace ComponentsApp.Server
         [HttpGet("/rerender")]
         public IActionResult Rerender()
         {
-            HotReloadManager.DeltaApplied();
+            HotReloadManager.UpdateApplication(default);
 
             return Ok();
         }
diff --git a/src/Components/test/testassets/TestServer/PrerenderedStartup.cs b/src/Components/test/testassets/TestServer/PrerenderedStartup.cs
index c4c10b25bfc..8b2339fa946 100644
--- a/src/Components/test/testassets/TestServer/PrerenderedStartup.cs
+++ b/src/Components/test/testassets/TestServer/PrerenderedStartup.cs
@@ -1,14 +1,10 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Globalization;
 using Microsoft.AspNetCore.Authentication.Cookies;
-using Microsoft.AspNetCore.Builder;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
+using Microsoft.AspNetCore.Components.HotReload;
 using Microsoft.AspNetCore.Components.WebAssembly.Services;
-using System.Globalization;
 
 namespace TestServer
 {
@@ -51,6 +47,8 @@ namespace TestServer
                 app.UseRouting();
                 app.UseEndpoints(endpoints =>
                 {
+                    endpoints.MapGet("ishotreloadsubscribedto", () => HotReloadManager.IsSubscribedTo);
+
                     endpoints.MapRazorPages();
                     endpoints.MapFallbackToPage("/PrerenderedHost");
                     endpoints.MapBlazorHub();
-- 
GitLab