Exemple #1
0
    public static async Task ReceiveDeltas(HotReloadAgent hotReloadAgent)
    {
        Log("Attempting to receive deltas.");

        // This value is configured by dotnet-watch when the app is to be launched.
        var namedPipeName = Environment.GetEnvironmentVariable("DOTNET_HOTRELOAD_NAMEDPIPE_NAME") ??
                            throw new InvalidOperationException("DOTNET_HOTRELOAD_NAMEDPIPE_NAME was not specified.");

        using var pipeClient = new NamedPipeClientStream(".", namedPipeName, PipeDirection.InOut, PipeOptions.CurrentUserOnly | PipeOptions.Asynchronous);
        try
        {
            await pipeClient.ConnectAsync(5000);

            Log("Connected.");
        }
        catch (TimeoutException)
        {
            Log("Unable to connect to hot-reload server.");
            return;
        }

        while (pipeClient.IsConnected)
        {
            var update = await UpdatePayload.ReadAsync(pipeClient, default);

            Log("Attempting to apply deltas.");

            hotReloadAgent.ApplyDeltas(update.Deltas);
            pipeClient.WriteByte((byte)ApplyResult.Success);
        }
        Log("Stopped received delta updates. Server is no longer connected.");
    }
Exemple #2
0
        public void GetHandlerActions_DiscoversActionsOnTypeWithBothActions()
        {
            // Arrange
            var actions = new HotReloadAgent.UpdateHandlerActions();
            var log     = new List <string>();
            var agent   = new HotReloadAgent(message => log.Add(message));

            agent.GetHandlerActions(actions, typeof(HandlerWithBothActions));

            Assert.Empty(log);
            Assert.Single(actions.ClearCache);
            Assert.Single(actions.UpdateApplication);
        }
Exemple #3
0
        public void TopologicalSort_IgnoresUnknownReferencedAssemblies()
        {
            // Arrange
            var assembly1 = GetAssembly("System.Private.CoreLib", Array.Empty <AssemblyName>());
            var assembly2 = GetAssembly("System.Text.Json", new[] { new AssemblyName("netstandard"), new AssemblyName("System.Private.CoreLib"), });
            var assembly3 = GetAssembly("Microsoft.AspNetCore.Components", new[] { new AssemblyName("System.Private.CoreLib"), new AssemblyName("Microsoft.Extensions.DependencyInjection"), });
            var assembly4 = GetAssembly("Microsoft.AspNetCore.Components.Web", new[] { new AssemblyName("Microsoft.AspNetCore.Components"), new AssemblyName("System.Text.Json"), });

            var sortedList = HotReloadAgent.TopologicalSort(new[] { assembly2, assembly4, assembly1, assembly3 });

            // Assert
            Assert.Equal(new[] { assembly1, assembly2, assembly3, assembly4 }, sortedList);
        }
Exemple #4
0
        public void TopologicalSort_Works()
        {
            // Arrange
            var assembly1 = GetAssembly("System.Private.CoreLib", Array.Empty <AssemblyName>());
            var assembly2 = GetAssembly("System.Text.Json", new[] { new AssemblyName("System.Private.CoreLib"), });
            var assembly3 = GetAssembly("Microsoft.AspNetCore.Components", new[] { new AssemblyName("System.Private.CoreLib"), });
            var assembly4 = GetAssembly("Microsoft.AspNetCore.Components.Web", new[] { new AssemblyName("Microsoft.AspNetCore.Components"), new AssemblyName("System.Text.Json"), });

            var sortedList = HotReloadAgent.TopologicalSort(new[] { assembly2, assembly4, assembly1, assembly3 });

            // Assert
            Assert.Equal(new[] { assembly1, assembly2, assembly3, assembly4 }, sortedList);
        }
Exemple #5
0
        public void TopologicalSort_WithCycles()
        {
            // Arrange
            var assembly1 = GetAssembly("System.Private.CoreLib", Array.Empty <AssemblyName>());
            var assembly2 = GetAssembly("System.Text.Json", new[] { new AssemblyName("System.Collections.Immutable"), new AssemblyName("System.Private.CoreLib"), });
            var assembly3 = GetAssembly("System.Collections.Immutable", new[] { new AssemblyName("System.Text.Json"), new AssemblyName("System.Private.CoreLib"), });
            var assembly4 = GetAssembly("Microsoft.AspNetCore.Components", new[] { new AssemblyName("System.Private.CoreLib"), new AssemblyName("Microsoft.Extensions.DependencyInjection"), });
            var assembly5 = GetAssembly("Microsoft.AspNetCore.Components.Web", new[] { new AssemblyName("Microsoft.AspNetCore.Components"), new AssemblyName("System.Text.Json"), });

            var sortedList = HotReloadAgent.TopologicalSort(new[] { assembly2, assembly4, assembly1, assembly3, assembly5 });

            // Assert
            Assert.Equal(new[] { assembly1, assembly3, assembly2, assembly4, assembly5 }, sortedList);
        }
Exemple #6
0
 public static void Initialize()
 {
     Task.Run(async() =>
     {
         using var hotReloadAgent = new HotReloadAgent(Log);
         try
         {
             await ReceiveDeltas(hotReloadAgent);
         }
         catch (Exception ex)
         {
             Log(ex.Message);
         }
     });
 }
Exemple #7
0
        public void GetHandlerActions_LogsMessageIfMethodHasIncorrectSignature()
        {
            // Arrange
            var actions     = new HotReloadAgent.UpdateHandlerActions();
            var log         = new List <string>();
            var agent       = new HotReloadAgent(message => log.Add(message));
            var handlerType = typeof(HandlerWithIncorrectSignature);

            agent.GetHandlerActions(actions, handlerType);

            var message = Assert.Single(log);

            Assert.Equal($"Type '{handlerType}' has method 'Void ClearCache()' that does not match the required signature.", message);
            Assert.Empty(actions.ClearCache);
            Assert.Single(actions.UpdateApplication);
        }
Exemple #8
0
    public static void Initialize()
    {
        ClearHotReloadEnvironmentVariables(Environment.GetEnvironmentVariable, Environment.SetEnvironmentVariable);

        Task.Run(async() =>
        {
            using var hotReloadAgent = new HotReloadAgent(Log);
            try
            {
                await ReceiveDeltas(hotReloadAgent);
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }
        });
    }
Exemple #9
0
        public void GetHandlerActions_LogsMessageIfNoActionsAreDiscovered()
        {
            // Arrange
            var actions     = new HotReloadAgent.UpdateHandlerActions();
            var log         = new List <string>();
            var agent       = new HotReloadAgent(message => log.Add(message));
            var handlerType = typeof(HandlerWithNoActions);

            agent.GetHandlerActions(actions, handlerType);

            var message = Assert.Single(log);

            Assert.Equal($"No invokable methods found on metadata handler type '{handlerType}'. " +
                         $"Allowed methods are ClearCache, UpdateApplication", message);
            Assert.Empty(actions.ClearCache);
            Assert.Empty(actions.UpdateApplication);
        }
Exemple #10
0
        public void WebAssemblyHotReload_DiscoversMetadataHandlers_FromHot()
        {
            // Arrange
            var hotReloadManager = typeof(Renderer).Assembly.GetType("Microsoft.AspNetCore.Components.HotReload.HotReloadManager");

            Assert.NotNull(hotReloadManager);

            var handlerActions = new HotReloadAgent.UpdateHandlerActions();
            var logs           = new List <string>();
            var hotReloadAgent = new HotReloadAgent(logs.Add);

            // Act
            hotReloadAgent.GetHandlerActions(handlerActions, hotReloadManager);

            // Assert
            Assert.Empty(handlerActions.ClearCache);
            Assert.Single(handlerActions.UpdateApplication);
        }