Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Feb 21, 2023
1 parent e0d8956 commit 4003a53
Show file tree
Hide file tree
Showing 10 changed files with 954 additions and 1,015 deletions.
206 changes: 99 additions & 107 deletions test/Websocket.Client.Tests/AdvancedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,150 +23,142 @@ public AdvancedTests(ITestOutputHelper output)
[Fact]
public async Task StreamFakeMessage_CustomMessage_ShouldBeFakelyStreamed()
{
using (var client = _context.CreateClient())
{
var myMessage = "my fake message";
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;
using var client = _context.CreateClient();
var myMessage = "my fake message";
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;

if (receivedCount >= 3)
receivedEvent.Set();
});
if (receivedCount >= 3)
receivedEvent.Set();
});

await client.Start();
await client.Start();

client.StreamFakeMessage(ResponseMessage.TextMessage(null));
client.StreamFakeMessage(ResponseMessage.TextMessage(myMessage));
client.StreamFakeMessage(ResponseMessage.TextMessage(null));
client.StreamFakeMessage(ResponseMessage.TextMessage(myMessage));

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.Equal(myMessage, received);
Assert.Throws<WebsocketBadInputException>(() => client.StreamFakeMessage(null));
}
Assert.Equal(myMessage, received);
Assert.Throws<WebsocketBadInputException>(() => client.StreamFakeMessage(null));
}

[Fact]
public async Task Echo_OneChunkMessage_ShouldReceiveCorrectly()
{
using (var client = _context.CreateClient())
{
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;
using var client = _context.CreateClient();
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;

if (receivedCount >= 3)
receivedEvent.Set();
});
if (receivedCount >= 3)
receivedEvent.Set();
});

await client.Start();
await client.Start();

for (int i = 1; i < 3; i++)
{
var sign = i == 1 ? 'A' : 'B';
var msg = new string(sign, 1024 * 4 - 14);
client.Send($"echo: special {msg}");
}
for (int i = 1; i < 3; i++)
{
var sign = i == 1 ? 'A' : 'B';
var msg = new string(sign, 1024 * 4 - 14);
client.Send($"echo: special {msg}");
}

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(3, receivedCount);
Assert.Equal(1024 * 4, received.Length);
Assert.StartsWith("echo: special BBBB", received);
}
Assert.NotNull(received);
Assert.Equal(3, receivedCount);
Assert.Equal(1024 * 4, received.Length);
Assert.StartsWith("echo: special BBBB", received);
}

[Fact]
public async Task Echo_LargeMessage_ShouldReceiveCorrectly()
{
using (var client = _context.CreateClient())
{
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;
using var client = _context.CreateClient();
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;

if (receivedCount >= 3)
receivedEvent.Set();
});
if (receivedCount >= 3)
receivedEvent.Set();
});

await client.Start();
await client.Start();

for (int i = 1; i < 3; i++)
{
var sign = i == 1 ? 'A' : 'B';
var msg = new string(sign, 1024 * 9);
client.Send($"echo:{msg}");
}
for (int i = 1; i < 3; i++)
{
var sign = i == 1 ? 'A' : 'B';
var msg = new string(sign, 1024 * 9);
client.Send($"echo:{msg}");
}

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(3, receivedCount);
Assert.Equal(1024 * 9 + 5, received.Length);
Assert.StartsWith("echo:BBBB", received);
}
Assert.NotNull(received);
Assert.Equal(3, receivedCount);
Assert.Equal(1024 * 9 + 5, received.Length);
Assert.StartsWith("echo:BBBB", received);
}

[Fact]
public async Task IsTextMessageConversionEnabled_False_ShouldReceiveStringMessageAsBinary()
{
using (var client = _context.CreateClient())
{
client.IsTextMessageConversionEnabled = false;
using var client = _context.CreateClient();
client.IsTextMessageConversionEnabled = false;

ResponseMessage received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
ResponseMessage received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg;
client
.MessageReceived
.Subscribe(msg =>
{
receivedCount++;
received = msg;

if (receivedCount > 1)
receivedEvent.Set();
});
if (receivedCount > 1)
receivedEvent.Set();
});

await client.Start();
await client.Start();

var sign = 'C';
var msg = new string(sign, 1024 * 9);
client.Send($"echo:{msg}");
var sign = 'C';
var msg = new string(sign, 1024 * 9);
client.Send($"echo:{msg}");

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(WebSocketMessageType.Binary, received.MessageType);
Assert.NotNull(received.Binary);
Assert.Null(received.Text);
Assert.Equal(2, receivedCount);
Assert.Equal(1024 * 9 + 5, received.Binary.Length);
}
Assert.NotNull(received);
Assert.Equal(WebSocketMessageType.Binary, received.MessageType);
Assert.NotNull(received.Binary);
Assert.Null(received.Text);
Assert.Equal(2, receivedCount);
Assert.Equal(1024 * 9 + 5, received.Binary.Length);
}
}
}
113 changes: 53 additions & 60 deletions test/Websocket.Client.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Websocket.Client.Tests.TestServer;
Expand All @@ -24,73 +21,69 @@ public BasicTests(ITestOutputHelper output)
[Fact]
public async Task PingPong()
{
using (var client = _context.CreateClient())
{
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;

if(receivedCount >= 6)
receivedEvent.Set();
});

await client.Start();

client.Send("ping");
client.Send("ping");
client.Send("ping");
client.Send("ping");
client.Send("ping");

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(5+1, receivedCount);
}
using var client = _context.CreateClient();
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;

if (receivedCount >= 6)
receivedEvent.Set();
});

await client.Start();

client.Send("ping");
client.Send("ping");
client.Send("ping");
client.Send("ping");
client.Send("ping");

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(5 + 1, receivedCount);
}

[Fact]
public async Task Echo_ShouldReceiveInCorrectOrder()
{
using (var client = _context.CreateClient())
{
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;
using var client = _context.CreateClient();
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;

if (receivedCount >= 7)
receivedEvent.Set();
});
if (receivedCount >= 7)
receivedEvent.Set();
});

await client.Start();
await client.Start();

for (int i = 0; i < 6; i++)
{
client.Send($"echo:{i}");
}
for (int i = 0; i < 6; i++)
{
client.Send($"echo:{i}");
}

receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));

Assert.NotNull(received);
Assert.Equal(7, receivedCount);
Assert.Equal("echo:5", received);
}
Assert.NotNull(received);
Assert.Equal(7, receivedCount);
Assert.Equal("echo:5", received);
}
}

Expand Down
Loading

0 comments on commit 4003a53

Please sign in to comment.