Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 167 additions & 80 deletions Hazel.UnitTests/Dtls/DtlsConnectionTests.cs

Large diffs are not rendered by default.

1,345 changes: 1,345 additions & 0 deletions Hazel.UnitTests/Dtls/LocklessDtlsConnectionTests.cs

Large diffs are not rendered by default.

29 changes: 18 additions & 11 deletions Hazel.UnitTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Diagnostics;
using Hazel.Udp.FewerThreads;
using System.Collections.Generic;

namespace Hazel.UnitTests
{
Expand Down Expand Up @@ -82,25 +83,19 @@ internal static void RunServerToClientTest(NetworkConnectionListener listener, C
listener.Start();

DataReceivedEventArgs? result = null;
//Setup conneciton
connection.DataReceived += delegate (DataReceivedEventArgs a)
{
Trace.WriteLine("Data was received correctly.");

try
{
result = a;
}
finally
{
mutex.Set();
}
result = a;
mutex.Set();
};

connection.Connect();

//Wait until data is received
mutex.WaitOne();
mutex.WaitOne(1000);

Assert.IsNotNull(result, "Data never received");

var dataReader = ConvertToMessageReader(data);
Assert.AreEqual(dataReader.Length, result.Value.Message.Length);
Expand Down Expand Up @@ -333,5 +328,17 @@ static MessageWriter BuildData(SendOption sendOption, int dataSize)

return output;
}

public static void WaitAll(IEnumerable<ManualResetEventSlim> events, TimeSpan MaxWait)
{
DateTime timeStart = DateTime.UtcNow;
foreach (var evt in events)
{
var timeSpent = DateTime.UtcNow - timeStart;
if (timeSpent > MaxWait) break;

evt.Wait(MaxWait - timeSpent);
}
}
}
}
4 changes: 2 additions & 2 deletions Hazel.UnitTests/ThreadLimitedUdpConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void ClientDisconnectTest()
connection.Disconnect("Testing");

mutex2.WaitOne(1000);
Assert.AreEqual(ConnectionState.NotConnected, connection.State);
Assert.AreEqual(ConnectionState.Disconnected, connection.State);
}
}

Expand Down Expand Up @@ -465,7 +465,7 @@ public void ServerDisconnectTest()
serverMutex.Set();

mutex.Wait(500);
Assert.AreEqual(ConnectionState.NotConnected, connection.State);
Assert.AreEqual(ConnectionState.Disconnected, connection.State);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Hazel.UnitTests/UdpConnectionTestHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override bool SendDisconnect(MessageWriter writer)
return false;
}

this.State = ConnectionState.NotConnected;
this.State = ConnectionState.Disconnected;
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion Hazel.UnitTests/UdpConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void PingDisconnectClientTest()

Thread.Sleep(1050); //Enough time for ~10 keep alive packets

Assert.AreEqual(ConnectionState.NotConnected, connection.State);
Assert.AreEqual(ConnectionState.Disconnected, connection.State);
Assert.AreEqual(3 * connection.MissingPingsUntilDisconnect + 4, connection.Statistics.TotalBytesSent); // + 4 for connecting overhead
}
#else
Expand Down
7 changes: 6 additions & 1 deletion Hazel/ConnectionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Hazel
public enum ConnectionState
{
/// <summary>
/// The Connection has either not been established yet or has been disconnected.
/// The Connection has not been established yet.
/// </summary>
NotConnected,

Expand All @@ -24,5 +24,10 @@ public enum ConnectionState
/// The Connection is connected and data can be transfered.
/// </summary>
Connected,

/// <summary>
/// The Connection was established, but is no longer.
/// </summary>
Disconnected
}
}
29 changes: 29 additions & 0 deletions Hazel/ConnectionStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,27 @@ public int PingMessagesReceived
/// </summary>
int pingMessagesReceived;


/// <summary>
/// The number of ping messages sent.
/// </summary>
/// <remarks>
/// This is the number of hello messages that were sent by the <see cref="Connection"/>, incremented
/// each time that SendPing is called by the Connection. Messages are counted before the bytes go on the wire.
/// </remarks>
public int PingMessagesSent
{
get
{
return pingMessagesSent;
}
}

/// <summary>
/// The number of hello messages received.
/// </summary>
int pingMessagesSent;

/// <summary>
/// The number of hello messages received.
/// </summary>
Expand Down Expand Up @@ -542,6 +563,14 @@ internal void LogPingReceive(int totalLength)
Interlocked.Add(ref totalBytesReceived, totalLength);
}

/// <summary>
/// Logs the sending of a hello data packet in the statistics.
/// </summary>
internal void LogPingSent()
{
Interlocked.Increment(ref pingMessagesSent);
}

/// <summary>
/// Logs the receiving of a hello data packet in the statistics.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Hazel/Dtls/DtlsConnectionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,10 @@ protected void MarkConnectionAsStale(ConnectionId connectionId)
/// <inheritdoc />
internal override void RemovePeerRecord(ConnectionId connectionId)
{
this.existingPeers.TryRemove(connectionId.EndPoint, out _);
if (this.existingPeers.TryRemove(connectionId.EndPoint, out var peer))
{
peer.Dispose();
}
}

/// <summary>
Expand Down
Loading