Answer the question
In order to leave comments, you need to log in
NUnit. Test fails and hangs - C#!?
When the StartServer_localhost__8181__ws_expectNoThrows test is executed, nothing happens, the test execution progresses, as it were, but does not produce any result, i. it just hangs indefinitely.
What could be the problem?
The tests themselves:
// ProgramTest.cs
using NUnit.Framework;
using System;
namespace WebSocketServer.Tests
{
[TestFixture]
public class ProgramTest
{
/// <summary>
/// Запуск программы с тремя параметрами: localhost 8118 ws
/// </summary>
[Test]
public void StartServer_localhost__8181__ws_expectNoThrows()
{
string[] args = new string[] { "localhost", "8181", "ws" };
Assert.That(() => Program.StartServer(args), Throws.Nothing);
}
/// <summary>
/// Запуск программы без параметров
/// </summary>
[Test]
public void StartServer_WithoutParams_expectArgumentException()
{
string[] args = new string[] { };
var ex = Assert.Throws<ArgumentException>(() => { Program.StartServer(args); });
Assert.That(ex.Message, Is.EqualTo("Invalid arguments. Usage: <hostname> <port> <protocol>"));
}
/// <summary>
/// Запуск программы с тремя параметрами: localhost NaN ws
/// </summary>
[Test]
public void StartServer_localhost__NaN__ws_expectFormatException()
{
string[] args = new string[] { "localhost", "NaN", "8181" };
var ex = Assert.Throws<FormatException>(() => { Program.StartServer(args); });
Assert.That(ex.Message, Is.EqualTo("Входная строка имела неверный формат."));
}
}
}
// Program.cs
using System;
namespace WebSocketServer
{
public class Program
{
public static void Main(string[] args)
{
try
{
StartServer(args);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void StartServer(string[] args)
{
if (args.Length < 3 || args.Length > 3)
{
throw new ArgumentException("Invalid arguments. Usage: <hostname> <port> <protocol>");
}
string hostname = args[0];
int port = int.Parse(args[1]);
string protocol = args[2];
var server = new Server(hostname, port, protocol);
server.Start().ListenMessages();
}
}
}
// Server.cs
using System;
using System.Collections.Generic;
using Fleck2.Interfaces;
using Newtonsoft.Json;
namespace WebSocketServer
{
public class Server
{
private Dictionary<IWebSocketConnection, string> ConnectedSockets { get; set; }
private string Hostname { get; }
private int Port { get; }
private string Protocol { get; }
private Fleck2.WebSocketServer WSServer { get; set; }
public Server(string hostname, int port, string protocol = "ws")
{
Hostname = hostname;
Port = port;
Protocol = protocol;
}
public void ListenMessages()
{
while (Console.ReadLine() != "exit")
{
}
}
public Server Start()
{
Fleck2.FleckLog.Level = Fleck2.LogLevel.Debug;
ConnectedSockets = new Dictionary<IWebSocketConnection, string>();
WSServer = new Fleck2.WebSocketServer($"{Protocol}://{Hostname}:{Port}");
WSServer.Start(socket =>
{
socket.OnClose = () => OnClose(socket);
socket.OnMessage = message => OnMessage(socket, message);
});
return this;
}
}
}
Answer the question
In order to leave comments, you need to log in
Based on what you have written in your code:
server.Start().ListenMessages();
// ...
public void ListenMessages()
{
while (Console.ReadLine() != "exit")
{
}
}
Moved the call to the ListenMessages method from StartServer to Main, and everything worked, Test Explorer successfully executes all tests and does not freeze.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question