Browse Source

Improve unit tests.

release/3.x.x
Christian Kratky 4 years ago
parent
commit
cf7e59a774
3 changed files with 11 additions and 4 deletions
  1. +8
    -1
      Source/MQTTnet/Implementations/CrossPlatformSocket.cs
  2. +2
    -2
      Tests/MQTTnet.Core.Tests/MqttTcpChannel_Tests.cs
  3. +1
    -1
      Tests/MQTTnet.Core.Tests/Server_Tests.cs

+ 8
- 1
Source/MQTTnet/Implementations/CrossPlatformSocket.cs View File

@@ -10,6 +10,8 @@ namespace MQTTnet.Implementations
{ {
readonly Socket _socket; readonly Socket _socket;


NetworkStream _networkStream;

public CrossPlatformSocket(AddressFamily addressFamily) public CrossPlatformSocket(AddressFamily addressFamily)
{ {
_socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp); _socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
@@ -137,6 +139,8 @@ namespace MQTTnet.Implementations


try try
{ {
_networkStream?.Dispose();

// Workaround for: workaround for https://github.com/dotnet/corefx/issues/24430 // Workaround for: workaround for https://github.com/dotnet/corefx/issues/24430
using (cancellationToken.Register(() => _socket.Dispose())) using (cancellationToken.Register(() => _socket.Dispose()))
{ {
@@ -148,6 +152,8 @@ namespace MQTTnet.Implementations
await _socket.ConnectAsync(host, port).ConfigureAwait(false); await _socket.ConnectAsync(host, port).ConfigureAwait(false);
#endif #endif
} }

_networkStream = new NetworkStream(_socket, true);
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
@@ -190,11 +196,12 @@ namespace MQTTnet.Implementations


public NetworkStream GetStream() public NetworkStream GetStream()
{ {
return new NetworkStream(_socket, true);
return _networkStream;
} }


public void Dispose() public void Dispose()
{ {
_networkStream?.Dispose();
_socket?.Dispose(); _socket?.Dispose();
} }




+ 2
- 2
Tests/MQTTnet.Core.Tests/MqttTcpChannel_Tests.cs View File

@@ -37,10 +37,10 @@ namespace MQTTnet.Tests
var clientSocket = new CrossPlatformSocket(AddressFamily.InterNetwork); var clientSocket = new CrossPlatformSocket(AddressFamily.InterNetwork);
await clientSocket.ConnectAsync("localhost", 50001, CancellationToken.None); await clientSocket.ConnectAsync("localhost", 50001, CancellationToken.None);


await Task.Delay(100, ct.Token);

var tcpChannel = new MqttTcpChannel(clientSocket.GetStream(), "test", null); var tcpChannel = new MqttTcpChannel(clientSocket.GetStream(), "test", null);


await Task.Delay(100, ct.Token);

var buffer = new byte[1]; var buffer = new byte[1];
await tcpChannel.ReadAsync(buffer, 0, 1, ct.Token); await tcpChannel.ReadAsync(buffer, 0, 1, ct.Token);




+ 1
- 1
Tests/MQTTnet.Core.Tests/Server_Tests.cs View File

@@ -375,7 +375,7 @@ namespace MQTTnet.Tests
await c1.PublishAsync(message); await c1.PublishAsync(message);
} }


await Task.Delay(500);
await Task.Delay(3000);


Assert.AreEqual(2000, receivedMessagesCount); Assert.AreEqual(2000, receivedMessagesCount);
} }


Loading…
Cancel
Save