-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
126 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,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(); | ||
} | ||
} |