Skip to content

Commit

Permalink
test: Added test for #27.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 16, 2023
1 parent 4e33fac commit da1a72d
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/tests/H.Pipes.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.Collections.Concurrent;
using System.IO.Pipes;
using System.Net;

namespace H.Pipes.Tests;

// ReSharper disable AccessToDisposedClosure

[TestClass]
public class Tests
{
[TestMethod]
public async Task Interrupted()
{
using var source = new CancellationTokenSource(TimeSpan.FromSeconds(11));
var cancellationToken = source.Token;
var isConnected = false;

var exceptions = new ConcurrentBag<Exception>();
const string pipeName = "int";
try
{

Console.WriteLine($"PipeName: {pipeName}");

await using var server = new PipeServer<byte[]>(pipeName);
server.ClientConnected += async (_, args) =>
{
Console.WriteLine($"Client {args.Connection.PipeName} is now connected!");
try
{
await args.Connection.WriteAsync(new byte[]
{
1, 2, 3, 4, 5
}, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
exceptions.Add(exception);
}
};
server.ClientDisconnected += (_, args) =>
{
Console.WriteLine($"Client {args.Connection.PipeName} disconnected");
};
server.MessageReceived += (_, args) =>
{
Console.WriteLine($"Client {args.Connection.PipeName} says: {args.Message}");
};
server.ExceptionOccurred += (_, args) => exceptions.Add(args.Exception);

_ = Task.Run(async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
Console.WriteLine($"Sent to {server.ConnectedClients.Count} clients");
await server.WriteAsync(new byte[]
{
1, 2, 3, 4, 5
}, cancellationToken).ConfigureAwait(false);
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}
}, cancellationToken);

Console.WriteLine("Server starting...");

await server.StartAsync(cancellationToken: cancellationToken).ConfigureAwait(false);

Console.WriteLine("Server is started!");

// https://github.com/HavenDV/H.Pipes/issues/27
{
await using var pipeClient = new NamedPipeClientStream(pipeName);
while (!pipeClient.IsConnected)
{
await pipeClient.ConnectAsync(cancellationToken);
await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
}

// read string length
var buffer = new byte[sizeof(int)];
_ = await pipeClient.ReadAsync(buffer.AsMemory(0, sizeof(int)), cancellationToken: cancellationToken);
var len = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));

// read string content
buffer = new byte[len];
_ = await pipeClient.ReadAsync(buffer.AsMemory(0, len), cancellationToken);
}

await using var client = new PipeClient<byte[]>(pipeName);
await client.ConnectAsync(cancellationToken);

isConnected = true;
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
exceptions.Add(exception);
}

if (!exceptions.IsEmpty)
{
throw new AggregateException(exceptions);
}

isConnected.Should().BeTrue();
}
}

0 comments on commit da1a72d

Please sign in to comment.