Skip to content

Commit

Permalink
test: Added Reconnect test.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jan 6, 2024
1 parent f64fb84 commit 2a75354
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/tests/H.Pipes.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ await server.WriteAsync(new byte[]
// Console.WriteLine("Server is started!");
//
// var isClientReceivedMessage = new TaskCompletionSource<bool>();
// cancellationToken.Register(() => isClientReceivedMessage.TrySetCanceled());
// await using var client = new PipeClient<byte[]>(pipeName);
// client.CreatePipeStreamForConnectionFunc = static (pipeName, serverName) => new NamedPipeClientStream(
// serverName: serverName,
Expand Down Expand Up @@ -247,5 +248,113 @@ await server.WriteAsync(new byte[]
//
// isConnected.Should().BeTrue();
// }

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

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

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

await using var server = new PipeServer<byte[]>(pipeName);
server.ClientConnected += (_, args) =>
{
Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Client {args.Connection.PipeName} is now connected!");
};
server.ClientDisconnected += (_, args) =>
{
Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Client {args.Connection.PipeName} disconnected");
};
server.ExceptionOccurred += (_, args) => exceptions.Add(args.Exception);

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

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

_ = Task.Run(async () =>
{
try
{
Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Waiting 1 second");

await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);

Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Stopping server");

await server.StopAsync(cancellationToken);

Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Starting server");

await server.StartAsync(cancellationToken);

Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Waiting 3 seconds");

await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken);

Console.WriteLine($"{DateTime.Now:hh:mm:ss.fff}: Send data to clients");

try
{
await server.WriteAsync(
[1, 2, 3, 4, 5], cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}, cancellationToken);

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

var isClientReceivedMessage = new TaskCompletionSource<bool>();
cancellationToken.Register(() => isClientReceivedMessage.TrySetCanceled());

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

client.MessageReceived += (_, _) =>
{
_ = isClientReceivedMessage.TrySetResult(true);
};
await client.ConnectAsync(cancellationToken);

var result = await isClientReceivedMessage.Task;

result.Should().BeTrue();

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

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

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

0 comments on commit 2a75354

Please sign in to comment.