-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#109) TcpClientEx: add memory leak tests
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.dotMemoryUnit; | ||
using NUnit.Framework; | ||
|
||
namespace SharpXMPP.Compat | ||
{ | ||
public class TcpClientExTests | ||
{ | ||
private static int GetFreePort() | ||
{ | ||
var host = new TcpListener(new IPEndPoint(IPAddress.Loopback, 0)); | ||
host.Start(); | ||
try | ||
{ | ||
return ((IPEndPoint)host.LocalEndpoint).Port; | ||
} | ||
finally | ||
{ | ||
host.Stop(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This test checks for TcpClient leaks which are guaranteed to happen if the requests aren't properly | ||
/// cancelled. | ||
/// </summary> | ||
/// <remarks> | ||
/// Run with dotMemoryUnit to check for leaks, run without it for basic sanity check. For details see | ||
/// https://www.jetbrains.com/help/dotmemory-unit/Get_Started.html#3-run-the-test | ||
/// </remarks> | ||
[Test, DotMemoryUnit(FailIfRunWithoutSupport = false)] | ||
public void TestCancellation() | ||
{ | ||
const int iterations = 100; | ||
int cancelled = 0; | ||
|
||
// Connecting to a free port takes enough time for the tasks to be cancelled in time. | ||
var port = GetFreePort(); | ||
for (int i = 0; i < iterations; ++i) | ||
{ | ||
Task.Run(async () => | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
var token = cts.Token; | ||
|
||
using var client = new TcpClient(); | ||
var task = client.ConnectWithCancellationAsync(IPAddress.Loopback, port, token); | ||
cts.Cancel(); | ||
|
||
try | ||
{ | ||
await task; | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
++cancelled; | ||
} | ||
}).Wait(); | ||
} | ||
|
||
Assert.Greater(cancelled, 0); | ||
|
||
dotMemory.Check(memory => | ||
// note there's 1 task object on pre-.NET 5 runtimes | ||
Assert.LessOrEqual(memory.GetObjects(o => o.Type.Is<Task>()).ObjectsCount, 1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters