Skip to content

Commit

Permalink
connection service test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinearJester committed Jun 5, 2022
1 parent 089478c commit d98e1af
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
69 changes: 54 additions & 15 deletions Shop1/IntegrationTest/ClientServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopData;
using ShopLogic;
using ShopServerPresentation;
using WebSocketConnection = ShopServerPresentation.WebSocketConnection;
using WebSocketConnection = ShopData.WebSocketConnection;

//using WebSocketConnection = ShopServerPresentation.WebSocketConnection;

namespace IntegrationTest
{
[TestClass]
public class ClientServerTest
{
[TestMethod]
public async Task ConnectionService()
{
List<string> logs = new List<string>();
IConnectionService connService = ServiceFactory.CreateConnectionService;

connService.ConnectionLogger += s => { logs.Add(s); };

var task = Task.Run( async () => ShopServerPresentation.Program.CreateServer());

await connService.Connect(new Uri("ws://localhost:8081/"));


Assert.IsTrue(connService.Connected);
await connService.Disconnect();
await Task.Delay(10);

task.Dispose();
Assert.IsFalse(connService.Connected);
}

[TestMethod]
public async Task WebSocketUsageTestMethod()
{
WebSocketConnection _wserver = null;
ShopServerPresentation.WebSocketConnection _wserver = null;
ShopData.WebSocketConnection _wclient = null;
const int _delay = 10;

Expand All @@ -24,35 +49,49 @@ public async Task WebSocketUsageTestMethod()
Task server = Task.Run(async () => await WebSocketServer.Server(uri.Port,
_ws =>
{
_wserver = _ws; _wserver.onMessage = (data) =>
_wserver = _ws;
Console.WriteLine("Connected with");
Console.WriteLine(_ws);
Console.WriteLine(_wserver);
Console.WriteLine(_wserver is null);
_wserver.onMessage = (data) =>
{
logOutput.Add($"Received message from client: { data}");
};
}));
await Task.Delay(_delay); //To make sure that the server is listening for messages
//connect client

await Task.Delay(_delay);

_wclient = await WebSocketClient.Connect(uri, message => logOutput.Add(message));
Console.WriteLine(_wserver);
Console.WriteLine(_wserver is null);

Assert.IsNotNull(_wserver);
Assert.IsNotNull(_wclient);
Assert.IsTrue(_wclient.IsConnected);

Console.WriteLine(_wserver);
Console.WriteLine(_wserver is null);
Console.WriteLine(_wclient is null);

Assert.IsNotNull(_wserver);


//send testing data from client to the server (use serialization if possible)
Task clientSendTask = _wclient.SendAsync("test");
Assert.IsTrue(clientSendTask.Wait(new TimeSpan(0, 0, 1))); //To make sure that the send operation has been finished successfully
await Task.Delay(_delay); //To make sure that the message has been received by the server
Assert.IsTrue(clientSendTask.Wait(new TimeSpan(0, 0, 1)));
await Task.Delay(_delay);


Assert.AreEqual($"Received message from client: test", logOutput[0]);

//test correctness of the data
Assert.AreEqual($"Received message from client: test", logOutput[1]);

//respond from the server
_wclient.onMessage = (data) =>
{
logOutput.Add($"Received message from server: { data}");
};
Task serverSendTask = _wserver.SendAsync("test 2");
Assert.IsTrue(serverSendTask.Wait(new TimeSpan(0, 0, 1))); //To make sure that the send operation has been finished successfully
await Task.Delay(_delay); //To make sure that the message has been received by the server
Assert.AreEqual($"Received message from server: test 2", logOutput[2]); //test correctness of the response
Assert.IsTrue(serverSendTask.Wait(new TimeSpan(0, 0, 1)));
await Task.Delay(_delay);
Assert.AreEqual($"Received message from server: test 2", logOutput[1]);
await _wclient?.DisconnectAsync();
await _wserver?.DisconnectAsync();
}
Expand Down
1 change: 1 addition & 0 deletions Shop1/IntegrationTest/IntegrationTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<ProjectReference Include="..\ShopData\ShopData.csproj" />
<ProjectReference Include="..\ShopLogic\ShopLogic.csproj" />
<ProjectReference Include="..\ShopServerPresentation\ShopServerPresentation.csproj" />
</ItemGroup>

Expand Down
34 changes: 25 additions & 9 deletions Shop1/ShopServerPresentation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Globalization;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ShopServerLogic;

[assembly: InternalsVisibleTo("IntegrationTest")]
namespace ShopServerPresentation
{
internal class Program
Expand All @@ -13,15 +15,16 @@ internal class Program

static async Task Main(string[] args)
{
Console.WriteLine("Server started");
logicLayer = ILogicLayer.Create();
shop = logicLayer.Shop;
shop.PriceChanged += async (sender, eventArgs) =>
{
if (WebSocketServer.CurrentConnection != null)
await SendMessageAsync("PriceChanged" + eventArgs.Price.ToString() + "/" + eventArgs.Id.ToString());
};
await WebSocketServer.Server(8081, ConnectionHandler);
//Console.WriteLine("Server started");
//logicLayer = ILogicLayer.Create();
//shop = logicLayer.Shop;
//shop.PriceChanged += async (sender, eventArgs) =>
//{
// if (WebSocketServer.CurrentConnection != null)
// await SendMessageAsync("PriceChanged" + eventArgs.Price.ToString() + "/" + eventArgs.Id.ToString());
//};
//await WebSocketServer.Server(8081, ConnectionHandler);
await CreateServer();
}

static void ConnectionHandler(WebSocketConnection webSocketConnection)
Expand Down Expand Up @@ -82,5 +85,18 @@ static async Task SendMessageAsync(string message)
Console.WriteLine($"[Server]: {message}");
await WebSocketServer.CurrentConnection.SendAsync(message);
}

public static async Task CreateServer()
{
Console.WriteLine("Server started");
logicLayer = ILogicLayer.Create();
shop = logicLayer.Shop;
shop.PriceChanged += async (sender, eventArgs) =>
{
if (WebSocketServer.CurrentConnection != null)
await SendMessageAsync("PriceChanged" + eventArgs.Price.ToString() + "/" + eventArgs.Id.ToString());
};
await WebSocketServer.Server(8081, ConnectionHandler);
}
}
}

0 comments on commit d98e1af

Please sign in to comment.