Skip to content

Commit

Permalink
added new unit test TestEchoMessageByPackageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Nov 9, 2024
1 parent 214af9a commit 70c38a0
Showing 1 changed file with 91 additions and 3 deletions.
94 changes: 91 additions & 3 deletions test/WebSocket4Net.Tests/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,20 @@ public async Task TestEchoMessage(Type hostConfiguratorType, int repeat)
var serverSessionPath = string.Empty;
var connected = false;

var resetEvent = new AutoResetEvent(false);

using (var server = CreateWebSocketSocketServerBuilder(builder =>
{
builder.UseSessionHandler(async (s) =>
{
connected = true;
resetEvent.Set();
await Task.CompletedTask;
},
async (s, e) =>
{
connected = false;
resetEvent.Set();
await Task.CompletedTask;
});

Expand All @@ -212,7 +216,7 @@ public async Task TestEchoMessage(Type hostConfiguratorType, int repeat)

Assert.Equal(WebSocketState.Open, websocket.State);

await Task.Delay(1 * 1000);
Assert.True(resetEvent.WaitOne(1000));
Assert.True(connected);

for (var i = 0; i < repeat; i++)
Expand All @@ -228,7 +232,7 @@ public async Task TestEchoMessage(Type hostConfiguratorType, int repeat)
Assert.NotNull(websocket.CloseStatus);
Assert.Equal(CloseReason.NormalClosure, websocket.CloseStatus.Reason);

await Task.Delay(1 * 1000);
Assert.True(resetEvent.WaitOne(1000));

Assert.Equal(WebSocketState.Closed, websocket.State);
Assert.False(connected);
Expand All @@ -237,7 +241,91 @@ public async Task TestEchoMessage(Type hostConfiguratorType, int repeat)
}
}


[Theory]
[InlineData(typeof(RegularHostConfigurator), 10)]
[InlineData(typeof(SecureHostConfigurator), 10)]
public async Task TestEchoMessageByPackageHandler(Type hostConfiguratorType, int repeat)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);

var serverSessionPath = string.Empty;
var connected = false;

var resetEvent = new AutoResetEvent(false);

using (var server = CreateWebSocketSocketServerBuilder(builder =>
{
builder.UseSessionHandler(async (s) =>
{
connected = true;
resetEvent.Set();
await Task.CompletedTask;
},
async (s, e) =>
{
connected = false;
resetEvent.Set();
await Task.CompletedTask;
});

builder.UseWebSocketMessageHandler(async (s, p) =>
{
var session = s as WebSocketSession;
await session.SendAsync(p.Message);
});

return builder;
}, hostConfigurator: hostConfigurator)
.BuildAsServer())
{
Assert.True(await server.StartAsync());
OutputHelper.WriteLine("Server started.");

var websocket = new WebSocket($"{hostConfigurator.WebSocketSchema}://{_loopbackIP}:{hostConfigurator.Listener.Port}");

hostConfigurator.ConfigureClient(websocket);

Assert.True(await websocket.OpenAsync(), "Failed to connect");

Assert.Equal(WebSocketState.Open, websocket.State);

Assert.True(resetEvent.WaitOne(1000));
Assert.True(connected);

var lastReceivedMessage = string.Empty;

websocket.PackageHandler += (s, p) =>
{
lastReceivedMessage = p.Message;
resetEvent.Set();
return ValueTask.CompletedTask;
};

websocket.StartReceive();

for (var i = 0; i < repeat; i++)
{
var text = Guid.NewGuid().ToString();
await websocket.SendAsync(text);
Assert.True(resetEvent.WaitOne(1000));
Assert.Equal(text, lastReceivedMessage);
}

await websocket.CloseAsync();

Assert.NotNull(websocket.CloseStatus);
Assert.Equal(CloseReason.NormalClosure, websocket.CloseStatus.Reason);

Assert.True(resetEvent.WaitOne(1000));

Assert.Equal(WebSocketState.Closed, websocket.State);
Assert.False(connected);

await server.StopAsync();
}
}


[Theory]
[InlineData(typeof(RegularHostConfigurator))]
[InlineData(typeof(SecureHostConfigurator))]
Expand Down

0 comments on commit 70c38a0

Please sign in to comment.