From 7e4541439f6ae8e7ea568c5d51cc3229c85b48fc Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Fri, 29 Mar 2024 22:01:27 -0700 Subject: [PATCH] added close reason unit test for KestralConnection --- test/SuperSocket.Tests/SessionTest.cs | 110 ++++++++++++++------------ 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/test/SuperSocket.Tests/SessionTest.cs b/test/SuperSocket.Tests/SessionTest.cs index 7a72659a8..d6df161db 100644 --- a/test/SuperSocket.Tests/SessionTest.cs +++ b/test/SuperSocket.Tests/SessionTest.cs @@ -18,6 +18,7 @@ using SuperSocket.Server.Host; using Microsoft.Extensions.Configuration; using System.Collections.Generic; +using System.Threading; namespace SuperSocket.Tests { @@ -118,14 +119,20 @@ private int GetEventInvocationCount(object objectWithEvent, string eventName) return handler.GetInvocationList().Length; } - [Fact] - public async Task TestCloseReason() + [Theory] + [InlineData(typeof(RegularHostConfigurator))] + [InlineData(typeof(KestralConnectionHostConfigurator))] + public async Task TestCloseReason(Type hostConfiguratorType) { - var hostConfigurator = new RegularHostConfigurator(); + var hostConfigurator = CreateObject(hostConfiguratorType); + IAppSession session = null; CloseReason closeReason = CloseReason.Unknown; + var connectEvent = new AutoResetEvent(false); + var closeEvent = new AutoResetEvent(false); + using (var server = CreateSocketServerBuilder(hostConfigurator) .ConfigureAppConfiguration((HostBuilder, configBuilder) => { @@ -134,18 +141,19 @@ public async Task TestCloseReason() { "serverOptions:maxPackageLength", "8" } }); }) - .UseSessionHandler((s) => - { - session = s; - - s.Closed += (xs, e) => - { - closeReason = e.Reason; - return ValueTask.CompletedTask; - }; - - return ValueTask.CompletedTask; - }) + .UseSessionHandler( + onConnected: (s) => + { + session = s; + connectEvent.Set(); + return ValueTask.CompletedTask; + }, + onClosed: (s, e) => + { + closeReason = e.Reason; + closeEvent.Set(); + return ValueTask.CompletedTask; + }) .BuildAsServer()) { Assert.Equal("TestServer", server.Name); @@ -154,57 +162,55 @@ public async Task TestCloseReason() OutputHelper.WriteLine("Started."); // RemoteClosing + using (var socket = CreateClient(hostConfigurator)) + using (var socketStream = await hostConfigurator.GetClientStream(socket)) + { + OutputHelper.WriteLine("Connected."); - var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); - OutputHelper.WriteLine("Connected."); - - await Task.Delay(1000); - - client.Shutdown(SocketShutdown.Both); - client.Close(); - - await Task.Delay(1000); + connectEvent.WaitOne(1000); - Assert.Equal(SessionState.Closed, session.State); - Assert.Equal(CloseReason.RemoteClosing, closeReason); + socket.Shutdown(SocketShutdown.Both); + socket.Close(); + closeEvent.WaitOne(1000); + Assert.Equal(SessionState.Closed, session.State); + Assert.Equal(CloseReason.RemoteClosing, closeReason); + } - // LocalClosing - closeReason = CloseReason.Unknown; + using (var socket = CreateClient(hostConfigurator)) + using (var socketStream = await hostConfigurator.GetClientStream(socket)) + { + connectEvent.WaitOne(1000); - client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); - OutputHelper.WriteLine("Connected."); + // LocalClosing + closeReason = CloseReason.Unknown; - await Task.Delay(1000); + await session.CloseAsync(CloseReason.LocalClosing); - await session.CloseAsync(CloseReason.LocalClosing); - await Task.Delay(1000); - Assert.Equal(SessionState.Closed, session.State); - Assert.Equal(CloseReason.LocalClosing, closeReason); + closeEvent.WaitOne(1000); + Assert.Equal(SessionState.Closed, session.State); + Assert.Equal(CloseReason.LocalClosing, closeReason); + } - // ProtocolError - closeReason = CloseReason.Unknown; - - client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - await client.ConnectAsync(hostConfigurator.GetServerEndPoint()); - OutputHelper.WriteLine("Connected."); - - await Task.Delay(1000); + using (var socket = CreateClient(hostConfigurator)) + using (var socketStream = await hostConfigurator.GetClientStream(socket)) + { + connectEvent.WaitOne(1000); - var outputStream = await hostConfigurator.GetClientStream(client); + // ProtocolError + closeReason = CloseReason.Unknown; - var buffer = Encoding.ASCII.GetBytes("123456789\r\n"); // package size exceeds maxPackageSize(8) - outputStream.Write(buffer, 0, buffer.Length); - outputStream.Flush(); + var buffer = Encoding.ASCII.GetBytes("123456789\r\n"); // package size exceeds maxPackageSize(8) + socketStream.Write(buffer, 0, buffer.Length); + socketStream.Flush(); - await Task.Delay(1000); + closeEvent.WaitOne(1000); - Assert.Equal(SessionState.Closed, session.State); - Assert.Equal(CloseReason.ProtocolError, closeReason); + Assert.Equal(SessionState.Closed, session.State); + Assert.Equal(CloseReason.ProtocolError, closeReason); + } await server.StopAsync(); }