From 3cff757627093a4178c8fc09c5d8d22ff942366e Mon Sep 17 00:00:00 2001 From: ScarletKuro Date: Mon, 12 Oct 2026 01:26:23 +0300 Subject: [PATCH] Switch to MTPv2 --- .github/workflows/build.yml | 12 +- Directory.Packages.props | 4 +- global.json | 5 +- src/SuperSocket.MCP/README.md | 2 +- .../AspNetIntegrationTest.cs | 16 +-- test/SuperSocket.Tests/AutofacTest.cs | 62 +++++----- .../BeginEndMarkProtocolTest.cs | 6 +- test/SuperSocket.Tests/ClientTest.cs | 30 ++--- test/SuperSocket.Tests/CommandTest.cs | 112 +++++++++--------- .../Http/HttpKeepAliveTest.cs | 8 +- .../Http/HttpPipelineFilterTest.cs | 6 +- .../Http/ServerSentEventsTest.cs | 24 ++-- .../PackageHandlingContextAccessorTest.cs | 16 +-- .../PipelineFilterRegistrationTest.cs | 12 +- test/SuperSocket.Tests/ProtocolTestBase.cs | 20 ++-- test/SuperSocket.Tests/ProxyProtocolTest.cs | 6 +- test/SuperSocket.Tests/ServerOptionsTest.cs | 10 +- .../SuperSocket.Tests/SessionContainerTest.cs | 10 +- test/SuperSocket.Tests/SessionTest.cs | 48 ++++---- .../SuperSocket.Tests.csproj | 2 - test/SuperSocket.Tests/SwitchProtocolTest.cs | 6 +- .../WebSocket/WebSocketBasicTest.cs | 92 +++++++------- 22 files changed, 251 insertions(+), 258 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 92e9a0df5..642fe6855 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,16 +44,10 @@ jobs: run: dotnet build -c Release samples/samples.sln - name: Test - Linux if: runner.os == 'Linux' - run: | - cd test/SuperSocket.Tests - dotnet test --logger "console;verbosity=detailed" + run: dotnet test --project test/SuperSocket.Tests/SuperSocket.Tests.csproj --output Detailed - name: Test - macOS if: runner.os == 'macOS' - run: | - cd test/SuperSocket.Tests - dotnet test --filter FullyQualifiedName\!~TestQuicSupport --logger "console;verbosity=detailed" + run: dotnet test --project test/SuperSocket.Tests/SuperSocket.Tests.csproj --filter-not-method "*.TestQuicSupport" --output Detailed - name: Test - Windows if: runner.os == 'Windows' - run: | - cd test/SuperSocket.Tests - dotnet test --filter FullyQualifiedName!~TestQuicSupport --logger "console;verbosity=detailed" \ No newline at end of file + run: dotnet test --project test/SuperSocket.Tests/SuperSocket.Tests.csproj --filter-not-method "*.TestQuicSupport" --output Detailed \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index fa6b1c121..600354ee9 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -22,9 +22,7 @@ - - - + diff --git a/global.json b/global.json index 859752a83..211f00285 100644 --- a/global.json +++ b/global.json @@ -2,5 +2,8 @@ "sdk": { "version": "10.0.0", "rollForward": "latestFeature" + }, + "test": { + "runner": "Microsoft.Testing.Platform" } -} \ No newline at end of file +} diff --git a/src/SuperSocket.MCP/README.md b/src/SuperSocket.MCP/README.md index bfc5f0d3e..547bba4fa 100644 --- a/src/SuperSocket.MCP/README.md +++ b/src/SuperSocket.MCP/README.md @@ -395,7 +395,7 @@ The package includes comprehensive tests covering: Run tests with: ```bash -dotnet test test/SuperSocket.MCP.Tests/ +dotnet test --project test/SuperSocket.Tests/SuperSocket.Tests.csproj --filter-namespace "SuperSocket.Tests.Mcp" ``` ## Examples diff --git a/test/SuperSocket.Tests/AspNetIntegrationTest.cs b/test/SuperSocket.Tests/AspNetIntegrationTest.cs index 25280ce99..b2b887d33 100644 --- a/test/SuperSocket.Tests/AspNetIntegrationTest.cs +++ b/test/SuperSocket.Tests/AspNetIntegrationTest.cs @@ -93,7 +93,7 @@ public async Task TestSingleHostServiceAccess() using (var host = builder.Build()) { - await host.StartAsync(); + await host.StartAsync(TestContext.Current.CancellationToken); var server = host.AsServer(); Assert.IsType(server.ServiceProvider.GetService()); @@ -103,7 +103,7 @@ public async Task TestSingleHostServiceAccess() Assert.IsType(scope.ServiceProvider.GetService()); } - await host.StopAsync(); + await host.StopAsync(TestContext.Current.CancellationToken); } } @@ -137,7 +137,7 @@ public async Task TestMultipleHostServiceAccess() using (var host = builder.Build()) { - await host.StartAsync(); + await host.StartAsync(TestContext.Current.CancellationToken); var server = host.AsServer(); Assert.IsType(server.ServiceProvider.GetService()); @@ -147,7 +147,7 @@ public async Task TestMultipleHostServiceAccess() Assert.IsType(scope.ServiceProvider.GetService()); } - await host.StopAsync(); + await host.StopAsync(TestContext.Current.CancellationToken); } } @@ -209,14 +209,14 @@ public async Task TestMultipleHostServiceAccess2() using (var host = builder.Build()) { - await host.StartAsync(); + await host.StartAsync(TestContext.Current.CancellationToken); var server = host.AsServer(); using(var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { - await client.ConnectAsync(GetDefaultServerEndPoint()); - await Task.Delay(1000); + await client.ConnectAsync(GetDefaultServerEndPoint(), TestContext.Current.CancellationToken); + await Task.Delay(1000, TestContext.Current.CancellationToken); Assert.NotNull(session); Assert.NotNull(session.TestService as TestService); @@ -225,7 +225,7 @@ public async Task TestMultipleHostServiceAccess2() client.Close(); } - await host.StopAsync(); + await host.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/AutofacTest.cs b/test/SuperSocket.Tests/AutofacTest.cs index d47e032d8..066c953a5 100644 --- a/test/SuperSocket.Tests/AutofacTest.cs +++ b/test/SuperSocket.Tests/AutofacTest.cs @@ -55,12 +55,12 @@ public async Task TestCommands(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -68,22 +68,22 @@ public async Task TestCommands(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("10", line); await streamWriter.WriteAsync("SUB 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -110,12 +110,12 @@ public async Task TestCommandsWithCustomSession(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -123,27 +123,27 @@ public async Task TestCommandsWithCustomSession(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("10", line); await streamWriter.WriteAsync("SUB 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("DIV 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("4", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -180,11 +180,11 @@ public async Task TestCommandsWithCustomSessionMultipleServerHost() Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(GetDefaultServerEndPoint()); + await client.ConnectAsync(GetDefaultServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = new NetworkStream(client)) @@ -192,27 +192,27 @@ public async Task TestCommandsWithCustomSessionMultipleServerHost() using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("10", line); await streamWriter.WriteAsync("SUB 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("DIV 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("4", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/BeginEndMarkProtocolTest.cs b/test/SuperSocket.Tests/BeginEndMarkProtocolTest.cs index 314035f35..11d8c874e 100644 --- a/test/SuperSocket.Tests/BeginEndMarkProtocolTest.cs +++ b/test/SuperSocket.Tests/BeginEndMarkProtocolTest.cs @@ -274,7 +274,7 @@ public async Task TestBreakRequest2(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - await server.StartAsync(); + await server.StartAsync(TestContext.Current.CancellationToken); using (var socket = CreateClient(hostConfigurator)) { @@ -296,13 +296,13 @@ public async Task TestBreakRequest2(Type hostConfiguratorType) for (var i = 0; i < size; i++) { - var receivedLine = await reader.ReadLineAsync(); + var receivedLine = await reader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal(lines[i], receivedLine); } } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/ClientTest.cs b/test/SuperSocket.Tests/ClientTest.cs index 72992a77c..11957c9d2 100644 --- a/test/SuperSocket.Tests/ClientTest.cs +++ b/test/SuperSocket.Tests/ClientTest.cs @@ -65,7 +65,7 @@ public async Task TestEcho(Type hostConfiguratorType, bool clientReadAsDemand) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var options = new ConnectionOptions @@ -76,7 +76,7 @@ public async Task TestEcho(Type hostConfiguratorType, bool clientReadAsDemand) var client = hostConfigurator.ConfigureEasyClient(new LinePipelineFilter(), options); - var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); Assert.True(connected); @@ -94,7 +94,7 @@ public async Task TestEcho(Type hostConfiguratorType, bool clientReadAsDemand) await client.CloseAsync(); Assert.True(serverSessionEvent.WaitOne(1000)); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -133,7 +133,7 @@ public async Task TestBindLocalEndPoint(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var pipelineFilter = new CommandLinePipelineFilter @@ -157,7 +157,7 @@ public async Task TestBindLocalEndPoint(Type hostConfiguratorType) try { - connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); } catch (SocketException e) { @@ -172,13 +172,13 @@ public async Task TestBindLocalEndPoint(Type hostConfiguratorType) Assert.True(connected); - await Task.Delay(500); + await Task.Delay(500, TestContext.Current.CancellationToken); Assert.NotNull(session); Assert.Equal(localPort, (session.RemoteEndPoint as IPEndPoint).Port); await client.CloseAsync(); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -228,7 +228,7 @@ public async Task TestCommandLine(Type hostConfiguratorType) { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var pipelineFilter = new CommandLinePipelineFilter @@ -251,7 +251,7 @@ public async Task TestCommandLine(Type hostConfiguratorType) await Task.CompletedTask; }; - var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); Assert.True(connected); @@ -269,7 +269,7 @@ public async Task TestCommandLine(Type hostConfiguratorType) } await client.CloseAsync(); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -287,7 +287,7 @@ public async Task TestDetachableConnection() { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); using (var socket = hostConfigurator.CreateClient()) @@ -303,7 +303,7 @@ public async Task TestDetachableConnection() }), () => socket.Connected); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } using (var server = CreateSocketServerBuilder(hostConfigurator) @@ -315,11 +315,11 @@ public async Task TestDetachableConnection() { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var connectionFactory = server.ServiceProvider .GetRequiredService(); - await using (var context = await connectionFactory.ConnectAsync(hostConfigurator.GetServerEndPoint())) + await using (var context = await connectionFactory.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken)) { await TestDetachableConnectionInternal(hostConfigurator, server, ser => new KestrelPipeConnection( context, @@ -331,7 +331,7 @@ public async Task TestDetachableConnection() ), () => !context.ConnectionClosed.IsCancellationRequested); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } diff --git a/test/SuperSocket.Tests/CommandTest.cs b/test/SuperSocket.Tests/CommandTest.cs index abb4388b9..798d1716f 100644 --- a/test/SuperSocket.Tests/CommandTest.cs +++ b/test/SuperSocket.Tests/CommandTest.cs @@ -56,12 +56,12 @@ public async Task TestCommands(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -69,22 +69,22 @@ public async Task TestCommands(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("10", line); await streamWriter.WriteAsync("SUB 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -112,12 +112,12 @@ public async Task TestUnknownCommands(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -125,17 +125,17 @@ public async Task TestUnknownCommands(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("X", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -155,11 +155,11 @@ public async Task TestCommandsFromAssembly(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -167,12 +167,12 @@ public async Task TestCommandsFromAssembly(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("MIN 8 6 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("3", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -192,11 +192,11 @@ public async Task TestCommandsFromConfigAssembly(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -204,12 +204,12 @@ public async Task TestCommandsFromConfigAssembly(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("MIN 8 6 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("3", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -232,12 +232,12 @@ public async Task TestJsonCommands(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -245,17 +245,17 @@ public async Task TestJsonCommands(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("POW { \"x\": 2, \"y\": 2 }\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("4", line); await streamWriter.WriteAsync("MAX { \"numbers\": [ 45, 77, 6, 88, 46 ] }\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("88", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -284,12 +284,12 @@ public async Task TestCommandsWithCustomSession(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -297,27 +297,27 @@ public async Task TestCommandsWithCustomSession(Type hostConfiguratorType) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("ADD 1 2 3\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("MULT 2 5\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("10", line); await streamWriter.WriteAsync("SUB 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("6", line); await streamWriter.WriteAsync("DIV 8 2\r\n"); - await streamWriter.FlushAsync(); - line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("4", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -434,12 +434,12 @@ public async Task TestCommandFilter(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -451,8 +451,8 @@ public async Task TestCommandFilter(Type hostConfiguratorType) for (var i = 1; i <= 100; i++) { await streamWriter.WriteAsync("COUNT\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal(i, sessionState.ExecutionCount); } @@ -462,8 +462,8 @@ public async Task TestCommandFilter(Type hostConfiguratorType) for (var i = 99; i >= 0; i--) { await streamWriter.WriteAsync("COUNTDOWN\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal(i, sessionState.ExecutionCount); } @@ -471,7 +471,7 @@ public async Task TestCommandFilter(Type hostConfiguratorType) Assert.Equal(currentTotal + 200, HitCountCommandFilterAttribute.Total); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -495,7 +495,7 @@ public async Task TestCommandWithDependencyInjection(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var dataStore = server.ServiceProvider.GetService(); @@ -510,7 +510,7 @@ public async Task TestCommandWithDependencyInjection(Type hostConfiguratorType) { dataStore.ResetTask(); await streamWriter.WriteAsync("DOUBLE\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); var newValue = await dataStore.WaitNewValue(); @@ -518,7 +518,7 @@ public async Task TestCommandWithDependencyInjection(Type hostConfiguratorType) dataStore.ResetTask(); await streamWriter.WriteAsync("DOUBLE\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); newValue = await dataStore.WaitNewValue(); @@ -526,14 +526,14 @@ public async Task TestCommandWithDependencyInjection(Type hostConfiguratorType) dataStore.ResetTask(); await streamWriter.WriteAsync("DOUBLE\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); newValue = await dataStore.WaitNewValue(); Assert.Equal(8, newValue); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } diff --git a/test/SuperSocket.Tests/Http/HttpKeepAliveTest.cs b/test/SuperSocket.Tests/Http/HttpKeepAliveTest.cs index 1f94a47b2..1c6983b05 100644 --- a/test/SuperSocket.Tests/Http/HttpKeepAliveTest.cs +++ b/test/SuperSocket.Tests/Http/HttpKeepAliveTest.cs @@ -63,7 +63,7 @@ public async Task TestKeepAliveConnection(Type hostConfiguratorType) } }).BuildAsServer()) { - Assert.True(await server.StartAsync(CancellationToken)); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var services = new ServiceCollection(); @@ -143,7 +143,7 @@ public async Task TestHttpResponse(Type hostConfiguratorType) await s.CloseAsync(CloseReason.LocalClosing); }).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var services = new ServiceCollection(); @@ -159,7 +159,7 @@ public async Task TestHttpResponse(Type hostConfiguratorType) using (var client = new EasyClient(new HttpPipelineFilter(), logger).AsClient()) { - var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); Assert.True(connected); var request = "GET /api/test HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"; @@ -173,7 +173,7 @@ public async Task TestHttpResponse(Type hostConfiguratorType) await client.CloseAsync(); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/Http/HttpPipelineFilterTest.cs b/test/SuperSocket.Tests/Http/HttpPipelineFilterTest.cs index a28fb3ede..af5b2ca90 100644 --- a/test/SuperSocket.Tests/Http/HttpPipelineFilterTest.cs +++ b/test/SuperSocket.Tests/Http/HttpPipelineFilterTest.cs @@ -87,7 +87,7 @@ public async Task TestEcho(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); IEasyClient client; @@ -109,7 +109,7 @@ public async Task TestEcho(Type hostConfiguratorType) else client = new EasyClient(new HttpPipelineFilter(), logger).AsClient(); - var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); Assert.True(connected); @@ -122,7 +122,7 @@ public async Task TestEcho(Type hostConfiguratorType) Assert.Equal("Hello World!", response.Body); await client.CloseAsync(); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/Http/ServerSentEventsTest.cs b/test/SuperSocket.Tests/Http/ServerSentEventsTest.cs index 27728a76f..5f6dc678f 100644 --- a/test/SuperSocket.Tests/Http/ServerSentEventsTest.cs +++ b/test/SuperSocket.Tests/Http/ServerSentEventsTest.cs @@ -65,13 +65,13 @@ public async Task TestSSEBasicEvent(Type hostConfiguratorType) } }).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); // Use raw TCP client to test SSE since HttpRequest doesn't parse SSE responses using (var tcpClient = new TcpClient()) { - await tcpClient.ConnectAsync(IPAddress.Loopback, hostConfigurator.Listener.Port); + await tcpClient.ConnectAsync(IPAddress.Loopback, hostConfigurator.Listener.Port, TestContext.Current.CancellationToken); var stream = tcpClient.GetStream(); // Send SSE request @@ -81,7 +81,7 @@ public async Task TestSSEBasicEvent(Type hostConfiguratorType) "Connection: keep-alive\r\n" + "\r\n"; var requestBytes = Encoding.UTF8.GetBytes(request); - await stream.WriteAsync(requestBytes, 0, requestBytes.Length); + await stream.WriteAsync(requestBytes, 0, requestBytes.Length, TestContext.Current.CancellationToken); // Read response var buffer = new byte[8192]; @@ -90,7 +90,7 @@ public async Task TestSSEBasicEvent(Type hostConfiguratorType) // Read in chunks to get the full SSE response for (int i = 0; i < 10; i++) // Limit iterations to avoid infinite loop { - var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); + var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, TestContext.Current.CancellationToken); if (bytesRead > 0) { var chunk = Encoding.UTF8.GetString(buffer, 0, bytesRead); @@ -101,7 +101,7 @@ public async Task TestSSEBasicEvent(Type hostConfiguratorType) break; } - await Task.Delay(50); // Small delay between reads + await Task.Delay(50, TestContext.Current.CancellationToken); // Small delay between reads } var response = totalReceived.ToString(); @@ -128,7 +128,7 @@ public async Task TestSSEBasicEvent(Type hostConfiguratorType) Assert.Contains("data: close", response); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -152,12 +152,12 @@ public async Task TestSSEHeartbeat(Type hostConfiguratorType) } }).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); using (var tcpClient = new TcpClient()) { - await tcpClient.ConnectAsync(IPAddress.Loopback, hostConfigurator.Listener.Port); + await tcpClient.ConnectAsync(IPAddress.Loopback, hostConfigurator.Listener.Port, TestContext.Current.CancellationToken); var stream = tcpClient.GetStream(); var request = "GET /heartbeat HTTP/1.1\r\n" + @@ -165,7 +165,7 @@ public async Task TestSSEHeartbeat(Type hostConfiguratorType) "Accept: text/event-stream\r\n" + "\r\n"; var requestBytes = Encoding.UTF8.GetBytes(request); - await stream.WriteAsync(requestBytes, 0, requestBytes.Length); + await stream.WriteAsync(requestBytes, 0, requestBytes.Length, TestContext.Current.CancellationToken); var buffer = new byte[4096]; var totalReceived = new StringBuilder(); @@ -173,7 +173,7 @@ public async Task TestSSEHeartbeat(Type hostConfiguratorType) // Read in chunks to get the full SSE response for (int i = 0; i < 10; i++) // Limit iterations to avoid infinite loop { - var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); + var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, TestContext.Current.CancellationToken); if (bytesRead > 0) { var chunk = Encoding.UTF8.GetString(buffer, 0, bytesRead); @@ -185,7 +185,7 @@ public async Task TestSSEHeartbeat(Type hostConfiguratorType) break; } - await Task.Delay(50); // Small delay between reads + await Task.Delay(50, TestContext.Current.CancellationToken); // Small delay between reads } var response = totalReceived.ToString(); @@ -198,7 +198,7 @@ public async Task TestSSEHeartbeat(Type hostConfiguratorType) Assert.Contains("data: After heartbeat", response); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/PackageHandlingContextAccessorTest.cs b/test/SuperSocket.Tests/PackageHandlingContextAccessorTest.cs index 1e01d5752..770a5b170 100644 --- a/test/SuperSocket.Tests/PackageHandlingContextAccessorTest.cs +++ b/test/SuperSocket.Tests/PackageHandlingContextAccessorTest.cs @@ -40,35 +40,35 @@ public async Task TestUsePackageHandlingContextAccessor() Assert.NotNull(packageHandlingContextAccessor); Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(GetDefaultServerEndPoint()); + await client.ConnectAsync(GetDefaultServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); - await Task.Delay(1000); + await Task.Delay(1000, TestContext.Current.CancellationToken); using (var stream = new NetworkStream(client)) using (var streamReader = new StreamReader(stream, Utf8Encoding, true)) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("TestCommand Package 1\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); OutputHelper.WriteLine("send 'Package 1'."); - var line = await streamReader.ReadLineAsync(); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("Package 1", line); await streamWriter.WriteAsync("TestCommand Package 2\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); OutputHelper.WriteLine("send 'Package 2'."); - line = await streamReader.ReadLineAsync(); + line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("Package 2", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } diff --git a/test/SuperSocket.Tests/PipelineFilterRegistrationTest.cs b/test/SuperSocket.Tests/PipelineFilterRegistrationTest.cs index 405b4b512..52a46145c 100644 --- a/test/SuperSocket.Tests/PipelineFilterRegistrationTest.cs +++ b/test/SuperSocket.Tests/PipelineFilterRegistrationTest.cs @@ -123,12 +123,12 @@ public async Task TestFilterWithDependencyInjection() .BuildAsServer()) { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, DefaultServerPort)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, DefaultServerPort), TestContext.Current.CancellationToken); Assert.True(serverSessionEvent.WaitOne(1000)); // Send test message @@ -149,7 +149,7 @@ public async Task TestFilterWithDependencyInjection() Assert.True(serverSessionEvent.WaitOne(1000)); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -178,12 +178,12 @@ public async Task TestFilterWithDefaultConstructor() .BuildAsServer()) { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, DefaultServerPort)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, DefaultServerPort), TestContext.Current.CancellationToken); Assert.True(serverSessionEvent.WaitOne(1000)); // Send test message @@ -204,7 +204,7 @@ public async Task TestFilterWithDefaultConstructor() Assert.True(serverSessionEvent.WaitOne(1000)); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/ProtocolTestBase.cs b/test/SuperSocket.Tests/ProtocolTestBase.cs index ff1e2593e..006ec22d7 100644 --- a/test/SuperSocket.Tests/ProtocolTestBase.cs +++ b/test/SuperSocket.Tests/ProtocolTestBase.cs @@ -44,7 +44,7 @@ public virtual async Task TestNormalRequest(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("The server has been started."); @@ -63,7 +63,7 @@ public virtual async Task TestNormalRequest(Type hostConfiguratorType) } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -80,7 +80,7 @@ public virtual async Task TestMiddleBreak(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("The server has been started."); @@ -101,7 +101,7 @@ public virtual async Task TestMiddleBreak(Type hostConfiguratorType) } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -118,7 +118,7 @@ public virtual async Task TestFragmentRequest(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("The server has been started."); @@ -144,7 +144,7 @@ public virtual async Task TestFragmentRequest(Type hostConfiguratorType) } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -161,7 +161,7 @@ public virtual async Task TestBatchRequest(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("The server has been started."); @@ -195,7 +195,7 @@ public virtual async Task TestBatchRequest(Type hostConfiguratorType) } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -212,7 +212,7 @@ public virtual async Task TestBreakRequest(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("The server has been started."); @@ -273,7 +273,7 @@ public virtual async Task TestBreakRequest(Type hostConfiguratorType) } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/ProxyProtocolTest.cs b/test/SuperSocket.Tests/ProxyProtocolTest.cs index d96d61e3b..ea3adac15 100644 --- a/test/SuperSocket.Tests/ProxyProtocolTest.cs +++ b/test/SuperSocket.Tests/ProxyProtocolTest.cs @@ -98,7 +98,7 @@ public async Task TestProxyEndPoints(Type hostConfiguratorType, int proxyProtoco configBuilder.AddInMemoryCollection(LoadMemoryConfig(new Dictionary())); }).BuildAsServer() as IServer) { - await server.StartAsync(); + await server.StartAsync(TestContext.Current.CancellationToken); using (var socket = CreateClient(hostConfigurator)) { @@ -110,7 +110,7 @@ public async Task TestProxyEndPoints(Type hostConfiguratorType, int proxyProtoco writer.Write(CreateRequest(line)); writer.Flush(); - var session = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromMinutes(1)); + var session = await taskCompletionSource.Task.WaitAsync(TimeSpan.FromMinutes(1), TestContext.Current.CancellationToken); Assert.NotNull(session.Connection.ProxyInfo); @@ -122,7 +122,7 @@ public async Task TestProxyEndPoints(Type hostConfiguratorType, int proxyProtoco } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/ServerOptionsTest.cs b/test/SuperSocket.Tests/ServerOptionsTest.cs index 2669363c2..7ae850821 100644 --- a/test/SuperSocket.Tests/ServerOptionsTest.cs +++ b/test/SuperSocket.Tests/ServerOptionsTest.cs @@ -49,12 +49,12 @@ public async Task MaxPackageLength(Type hostConfiguratorType) Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); using (var stream = await hostConfigurator.GetClientStream(client)) @@ -67,16 +67,16 @@ public async Task MaxPackageLength(Type hostConfiguratorType) } await streamWriter.WriteAsync("\r\n"); - await streamWriter.FlushAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); Thread.Sleep(1000); - var line = await streamReader.ReadLineAsync(); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Null(line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/SessionContainerTest.cs b/test/SuperSocket.Tests/SessionContainerTest.cs index d3ca14ba8..d589bc0ab 100644 --- a/test/SuperSocket.Tests/SessionContainerTest.cs +++ b/test/SuperSocket.Tests/SessionContainerTest.cs @@ -107,11 +107,11 @@ public async Task TestInProcSessionContainer() Assert.Same(sessionContainerDependentService.SessionContainer, asyncSessionContainer.SessionContainer); Assert.Same(sessionContainer, sessionContainerDependentService.SessionContainer); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); Assert.True(sessionStateEvent.WaitOne(1000)); @@ -125,8 +125,8 @@ public async Task TestInProcSessionContainer() using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("SESS\r\n"); - await streamWriter.FlushAsync(); - sessionID = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + sessionID = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.False(string.IsNullOrEmpty(sessionID)); @@ -142,7 +142,7 @@ public async Task TestInProcSessionContainer() Assert.Null(sessionContainer.GetSessionByID(sessionID)); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/SessionTest.cs b/test/SuperSocket.Tests/SessionTest.cs index 0c79b6965..e3f537425 100644 --- a/test/SuperSocket.Tests/SessionTest.cs +++ b/test/SuperSocket.Tests/SessionTest.cs @@ -70,14 +70,14 @@ public async Task TestSessionEvents() { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Started."); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); - await Task.Delay(1000); + await Task.Delay(1000, TestContext.Current.CancellationToken); session.Closed += (s, e) => { @@ -103,7 +103,7 @@ public async Task TestSessionEvents() client.Shutdown(SocketShutdown.Both); client.Close(); - await Task.Delay(1000); + await Task.Delay(1000, TestContext.Current.CancellationToken); Assert.Equal(SessionState.Closed, session.State); Assert.False(connected); @@ -121,7 +121,7 @@ public async Task TestSessionEvents() Assert.Equal(0, GetEventInvocationCount(session, nameof(session.Closed))); Assert.Equal(0, GetEventInvocationCount(session, nameof(session.Connected))); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -179,7 +179,7 @@ public async Task TestCloseReason(Type hostConfiguratorType) { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Started."); // RemoteClosing @@ -233,7 +233,7 @@ public async Task TestCloseReason(Type hostConfiguratorType) Assert.Equal(CloseReason.ProtocolError, closeReason); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -275,7 +275,7 @@ public async Task TestCustomCloseReason(Type hostConfiguratorType, int closeReas { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Started."); using (var socket = CreateClient(hostConfigurator)) @@ -296,7 +296,7 @@ public async Task TestCustomCloseReason(Type hostConfiguratorType, int closeReas Assert.Equal(closeReasonRequested, closeReason); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -310,23 +310,23 @@ public async Task TestConsoleProtocol() await s.SendAsync(Utf8Encoding.GetBytes("Hello World\r\n")); }).BuildAsServer() as IServer) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); Assert.Equal(0, server.SessionCount); var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); + await client.ConnectAsync(hostConfigurator.GetServerEndPoint(), TestContext.Current.CancellationToken); using (var stream = await hostConfigurator.GetClientStream(client)) using (var streamReader = new StreamReader(stream, Utf8Encoding, true)) using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4)) { await streamWriter.WriteAsync("Hello World\r\n"); - await streamWriter.FlushAsync(); - var line = await streamReader.ReadLineAsync(); + await streamWriter.FlushAsync(TestContext.Current.CancellationToken); + var line = await streamReader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal("Hello World", line); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -339,11 +339,11 @@ public async Task TestServiceProvider() services.AddSingleton(); }).BuildAsServer() as IServer) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); Assert.IsType(server.ServiceProvider.GetService()); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -378,7 +378,7 @@ public async Task TestSendAsyncReadOnlySequence_SingleSegment() { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Started."); @@ -390,7 +390,7 @@ public async Task TestSendAsyncReadOnlySequence_SingleSegment() var client = hostConfigurator.ConfigureEasyClient(new MyFixedHeaderPipelineFilter(), options); - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); @@ -412,7 +412,7 @@ public async Task TestSendAsyncReadOnlySequence_SingleSegment() Assert.NotNull(clientReceiverPackage2); Assert.Equal("ClientRequest1ClientRequest2ClientRequest3", serverReceiverPackage.Text); Assert.Equal("ServerResponse1ServerResponse2ServerResponse3", clientReceiverPackage.Text); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -449,7 +449,7 @@ public async Task TestSendAsyncReadOnlySequence_MultiSegment() { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Started."); var options = new ConnectionOptions @@ -460,7 +460,7 @@ public async Task TestSendAsyncReadOnlySequence_MultiSegment() var client = hostConfigurator.ConfigureEasyClient(new MyFixedHeaderPipelineFilter(), options); - await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port)); + await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, hostConfigurator.Listener.Port), TestContext.Current.CancellationToken); OutputHelper.WriteLine("Connected."); byte[] buffer = new byte[4]; @@ -485,7 +485,7 @@ public async Task TestSendAsyncReadOnlySequence_MultiSegment() Assert.Equal("ClientRequest1ClientRequest2ClientRequest3", serverReceiverPackage.Text); Assert.Equal("ServerResponse1ServerResponse2ServerResponse3", clientReceiverPackage.Text); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -573,7 +573,7 @@ public async Task TestSessionRejectedByMiddleware() .BuildAsServer()) { Assert.Equal("TestServer", server.Name); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); // First connection (should be accepted) @@ -629,7 +629,7 @@ public async Task TestSessionRejectedByMiddleware() Assert.True(closedEvent.WaitOne(1000)); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/SuperSocket.Tests.csproj b/test/SuperSocket.Tests/SuperSocket.Tests.csproj index 7e4894a51..846780150 100755 --- a/test/SuperSocket.Tests/SuperSocket.Tests.csproj +++ b/test/SuperSocket.Tests/SuperSocket.Tests.csproj @@ -23,9 +23,7 @@ - - diff --git a/test/SuperSocket.Tests/SwitchProtocolTest.cs b/test/SuperSocket.Tests/SwitchProtocolTest.cs index ae76c7033..7c9fabbfd 100644 --- a/test/SuperSocket.Tests/SwitchProtocolTest.cs +++ b/test/SuperSocket.Tests/SwitchProtocolTest.cs @@ -127,7 +127,7 @@ public override async Task TestNormalRequest(Type hostConfiguratorType) using (var server = CreateServer(hostConfigurator)) { - await server.StartAsync(); + await server.StartAsync(TestContext.Current.CancellationToken); using (var socket = CreateClient(hostConfigurator)) { @@ -139,12 +139,12 @@ public override async Task TestNormalRequest(Type hostConfiguratorType) writer.Write(CreateRequest(line)); writer.Flush(); - var receivedLine = await reader.ReadLineAsync(); + var receivedLine = await reader.ReadLineAsync(TestContext.Current.CancellationToken); Assert.Equal(line, receivedLine); } } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } } diff --git a/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs b/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs index d4d025394..57d2200be 100644 --- a/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs +++ b/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs @@ -143,7 +143,7 @@ public async Task TestCustomWebSocketSession(Type hostConfiguratorType) }, hostConfigurator: hostConfigurator) .BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var websocket = new ClientWebSocket(); @@ -152,17 +152,17 @@ public async Task TestCustomWebSocketSession(Type hostConfiguratorType) Assert.Equal(WebSocketState.Open, websocket.State); - await Task.Delay(1 * 1000); + await Task.Delay(1 * 1000, TestContext.Current.CancellationToken); Assert.IsType(session); Assert.IsType((session as MyWebSocketSession).TestService); await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); - await Task.Delay(1 * 1000); + await Task.Delay(1 * 1000, TestContext.Current.CancellationToken); Assert.Equal(WebSocketState.Closed, websocket.State); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -195,7 +195,7 @@ public async Task TestHandshake(Type hostConfiguratorType) }, hostConfigurator: hostConfigurator) .BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var path = "/app/talk"; @@ -208,18 +208,18 @@ public async Task TestHandshake(Type hostConfiguratorType) Assert.Equal(WebSocketState.Open, websocket.State); - await Task.Delay(1 * 1000); + await Task.Delay(1 * 1000, TestContext.Current.CancellationToken); // test path Assert.Equal(path, serverSessionPath); Assert.True(connected); await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); - await Task.Delay(1 * 1000); + await Task.Delay(1 * 1000, TestContext.Current.CancellationToken); Assert.Equal(WebSocketState.Closed, websocket.State); Assert.False(connected); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -304,7 +304,7 @@ public async Task TestEmptyMessage(Type hostConfiguratorType) }, hostConfigurator: hostConfigurator) .BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var websocket = new ClientWebSocket(); @@ -330,7 +330,7 @@ public async Task TestEmptyMessage(Type hostConfiguratorType) Assert.Equal(WebSocketState.Closed, websocket.State); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -360,7 +360,7 @@ public async Task TestLongMessageFromServer(Type hostConfiguratorType) }, hostConfigurator: hostConfigurator) .BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var path = "/app/talk"; @@ -394,11 +394,11 @@ public async Task TestLongMessageFromServer(Type hostConfiguratorType) } await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); - await Task.Delay(1 * 1000); + await Task.Delay(1 * 1000, TestContext.Current.CancellationToken); Assert.Equal(WebSocketState.Closed, websocket.State); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -423,13 +423,13 @@ public async Task TestHandshakeTimeOut(Type hostConfiguratorType) return builder; }, hostConfigurator).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.NoDelay = true; var endPoint = hostConfigurator.GetServerEndPoint(); - await socket.ConnectAsync(endPoint); + await socket.ConnectAsync(endPoint, TestContext.Current.CancellationToken); Assert.True(socket.Connected); Assert.False(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0); @@ -439,11 +439,11 @@ public async Task TestHandshakeTimeOut(Type hostConfiguratorType) socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 1); */ - await Task.Delay(1000 * 5); + await Task.Delay(1000 * 5, TestContext.Current.CancellationToken); //Assert.False(socket.Connected); Assert.True(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -463,7 +463,7 @@ public async Task TestTextMessageSendReceive(Type hostConfiguratorType, int conn }); }, hostConfigurator).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var tasks = Enumerable.Range(0, connCount).Select((x, y) => @@ -504,7 +504,7 @@ public async Task TestTextMessageSendReceive(Type hostConfiguratorType, int conn }); await Task.WhenAll(tasks.ToArray()); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -523,7 +523,7 @@ public async Task TestSendReadOnlySequence(Type hostConfiguratorType) }); }, hostConfigurator).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var websocket = new ClientWebSocket(); @@ -554,7 +554,7 @@ public async Task TestSendReadOnlySequence(Type hostConfiguratorType) Assert.Equal(WebSocketState.Closed, websocket.State); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -574,7 +574,7 @@ public async Task TestBinaryMessageSendReceive(Type hostConfiguratorType, int co }); }, hostConfigurator).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var tasks = Enumerable.Range(0, connCount).Select((x, y) => @@ -641,7 +641,7 @@ public async Task TestBinaryMessageSendReceive(Type hostConfiguratorType, int co Assert.True(r.Result, r.Message); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -660,7 +660,7 @@ public async Task TestBinaryMessageToArray(Type hostConfiguratorType, int connCo }); }, hostConfigurator).BuildAsServer()) { - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); OutputHelper.WriteLine("Server started."); var tasks = Enumerable.Range(0, connCount).Select((x, y) => @@ -727,7 +727,7 @@ public async Task TestBinaryMessageToArray(Type hostConfiguratorType, int connCo Assert.True(r.Result, r.Message); } - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -759,7 +759,7 @@ public async Task TestVariousSizeMessagesConcurrent(Type hostConfiguratorType, i var loggerFactory = server.ServiceProvider.GetService(); var logger = loggerFactory.CreateLogger("Test"); - Assert.True(await server.StartAsync()); + Assert.True(await server.StartAsync(TestContext.Current.CancellationToken)); logger.LogInformation("Server started."); var tasks = Enumerable.Range(0, connCount).Select((x, y) => @@ -808,7 +808,7 @@ public async Task TestVariousSizeMessagesConcurrent(Type hostConfiguratorType, i }); await Task.WhenAll(tasks.ToArray()); - await server.StopAsync(); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -848,7 +848,7 @@ public async Task TestVariousSizeMessages(Type hostConfiguratorType, int message }, hostConfigurator: hostConfigurator) .BuildAsServer()) { - await server.StartAsync(); + await server.StartAsync(TestContext.Current.CancellationToken); OutputHelper.WriteLine("Server started."); var websocket = new ClientWebSocket(); @@ -875,7 +875,7 @@ public async Task TestVariousSizeMessages(Type hostConfiguratorType, int message var endOfMessage = rest == 0; await websocket.SendAsync(segment, WebSocketMessageType.Text, endOfMessage, CancellationToken.None); - await Task.Delay(500); + await Task.Delay(500, TestContext.Current.CancellationToken); } var receivedMessage = await GetWebSocketReply(websocket, receiveBuffer); @@ -883,8 +883,8 @@ public async Task TestVariousSizeMessages(Type hostConfiguratorType, int message } await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); - await Task.Delay(TimeSpan.FromSeconds(1)); - await server.StopAsync(); + await Task.Delay(TimeSpan.FromSeconds(1), TestContext.Current.CancellationToken); + await server.StopAsync(TestContext.Current.CancellationToken); } } @@ -915,7 +915,7 @@ public async Task TestCommands(Type hostConfiguratorType, Func