Skip to content

Commit

Permalink
added close reason unit test for KestralConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Mar 30, 2024
1 parent 6a6b364 commit 7e45414
Showing 1 changed file with 58 additions and 52 deletions.
110 changes: 58 additions & 52 deletions test/SuperSocket.Tests/SessionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using SuperSocket.Server.Host;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Threading;

namespace SuperSocket.Tests
{
Expand Down Expand Up @@ -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<IHostConfigurator>(hostConfiguratorType);

IAppSession session = null;

CloseReason closeReason = CloseReason.Unknown;

var connectEvent = new AutoResetEvent(false);
var closeEvent = new AutoResetEvent(false);

using (var server = CreateSocketServerBuilder<TextPackageInfo, LinePipelineFilter>(hostConfigurator)
.ConfigureAppConfiguration((HostBuilder, configBuilder) =>
{
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down

0 comments on commit 7e45414

Please sign in to comment.