Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CircuitGracefulTerminationTests.cs « ServerExecutionTests « E2ETest « test « Components « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 399f90204332a82a829c136dbc0c721617c3186e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BasicTestApp;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Testing;
using OpenQA.Selenium;
using TestServer;
using Xunit.Abstractions;

namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests;

public class CircuitGracefulTerminationTests : ServerTestBase<BasicTestAppServerSiteFixture<ServerStartup>>, IDisposable
{
    public CircuitGracefulTerminationTests(
        BrowserFixture browserFixture,
        BasicTestAppServerSiteFixture<ServerStartup> serverFixture,
        ITestOutputHelper output)
        : base(browserFixture, serverFixture, output)
    {
    }

    public TaskCompletionSource GracefulDisconnectCompletionSource { get; private set; }
    public TestSink Sink { get; private set; }
    public List<(Extensions.Logging.LogLevel level, string eventIdName)> Messages { get; private set; }

    public override async Task InitializeAsync()
    {
        // These tests manipulate the browser in  ways that make it impossible to use the same browser
        // instance across tests (One of the tests closes the browser). For that reason we simply create
        // a new browser instance for every test in this class sos that there are no issues when running
        // them together.
        await base.InitializeAsync(Guid.NewGuid().ToString());
    }

    protected override void InitializeAsyncCore()
    {
        Navigate(ServerPathBase, noReload: false);
        Browser.MountTestComponent<GracefulTermination>();
        Browser.Equal("Graceful Termination", () => Browser.Exists(By.TagName("h1")).Text);

        GracefulDisconnectCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
        Sink = _serverFixture.Host.Services.GetRequiredService<TestSink>();
        Messages = new List<(Extensions.Logging.LogLevel level, string eventIdName)>();
        Sink.MessageLogged += Log;
    }

    [Fact]
    public async Task ReloadingThePage_GracefullyDisconnects_TheCurrentCircuit()
    {
        // Arrange & Act
        Browser.Navigate().Refresh();
        await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    [Fact]
    [QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/44017")]
    public async Task ClosingTheBrowserWindow_GracefullyDisconnects_TheCurrentCircuit()
    {
        // Arrange & Act
        Browser.Close();
        await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.True(GracefulDisconnectCompletionSource.Task.IsCompletedSuccessfully);
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    [Fact]
    public async Task ClosingTheBrowserWindow_GracefullyDisconnects_WhenNavigatingAwayFromThePage()
    {
        // Arrange & Act
        Browser.Navigate().GoToUrl("about:blank");
        var task = await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.Equal(GracefulDisconnectCompletionSource.Task, task);
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    [Fact]
    public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurrentCircuit()
    {
        // Arrange & Act
        var element = Browser.Exists(By.Id("mailto-link"));
        element.Click();
        await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    [Fact]
    public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
    {
        // Arrange & Act
        var element = Browser.Exists(By.Id("download-link"));
        element.Click();
        await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    [Fact]
    public async Task DownloadHref_DoesNotGracefullyDisconnect_TheCurrentCircuit()
    {
        // Arrange & Act
        var element = Browser.Exists(By.Id("download-href"));
        element.Click();
        await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);

        // Assert
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages.ToArray());
        Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages.ToArray());
    }

    private void Log(WriteContext wc)
    {
        if ((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully") == (wc.LogLevel, wc.EventId.Name))
        {
            GracefulDisconnectCompletionSource.TrySetResult();
        }
        Messages.Add((wc.LogLevel, wc.EventId.Name));
    }

    public void Dispose()
    {
        if (Sink != null)
        {
            Sink.MessageLogged -= Log;
        }
    }
}