public void Close_DisconnectsStreamAsync() { var pipeName = Guid.NewGuid().ToString(); var readStream = new NamedPipeServerStream(pipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous); var writeStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.WriteThrough | PipeOptions.Asynchronous); new StreamingRequestHandler(new Microsoft.Bot.Streaming.UnitTests.Mocks.MockBot(), new BotFrameworkHttpAdapter(), pipeName); var reader = new NamedPipeClient(pipeName); var writer = new NamedPipeServer(pipeName, new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName)); try { reader.ConnectAsync(); writer.StartAsync(); readStream.WaitForConnectionAsync().ConfigureAwait(false); writeStream.ConnectAsync(500).ConfigureAwait(false); writer.Disconnect(); reader.Disconnect(); } finally { readStream.Dispose(); writeStream.Dispose(); } Assert.False(reader.IsConnected); }
static async Task StartStringLengthServer() { var server = new NamedPipeServer <string, int>("string_length", (txt) => txt.Length); server.OnRequestReceived += (o, e) => Console.WriteLine($"request: {e}"); server.OnResponseSended += (o, e) => Console.WriteLine($"response: {e}"); var source = new CancellationTokenSource(); await server.StartAsync(source.Token); }
public async Task DisconnectWorksAsIntendedAsync() { // Truncating GUID to make sure the full path does not exceed 104 characters. var pipeName = Guid.NewGuid().ToString().Substring(0, 18); var readStream = new NamedPipeServerStream(pipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous); var writeStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.WriteThrough | PipeOptions.Asynchronous); new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName); var reader = new NamedPipeClient(pipeName); var writer = new NamedPipeServer(pipeName, new StreamingRequestHandler(new MockBot(), new BotFrameworkHttpAdapter(), pipeName)); try { // The ConnectAsync task returns only after the connection has been disposed. // In this context it makes for ugly code, but in the context of blocking an HTTP // response until the the streaming connection has ended it creates an easier to // follow user experience. var connectTask = reader.ConnectAsync(); await writer.StartAsync(); // The writeStream can only connect to the readStream if the readStream is listening for new connections. // This creates a dependency between the two tasks below, requiring the two to be running in parallel until // the readStream receives an incoming connection and the writeStream establishes an outgoing connection. await Task.WhenAll(readStream.WaitForConnectionAsync(), writeStream.ConnectAsync()); // Assert that the reader is now connected. Assert.True(reader.IsConnected, "Reader failed to connect."); // The line we're actually testing. reader.Disconnect(); // Assert that the reader and writer are no longer connected Assert.False(reader.IsConnected, "Reader did not disconnect."); } finally { readStream.Dispose(); writeStream.Dispose(); } }