Repository: dotnet/runtime
File: src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CreateClient.cs (C#)

Code before:
                var clientFromHandle = new NamedPipeClientStream(PipeDirection.InOut, true, new SafePipeHandle(client1.SafePipeHandle.DangerousGetHandle(), ownsHandle: false));
                Assert.Throws<InvalidOperationException>(() => clientFromHandle.Connect());
            }

            // Test obsolete constructor with isConnected: true - should throw InvalidOperationException when Connect is called
#pragma warning disable SYSLIB0063
            using (var server2 = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
            using (var client2 = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous))
            {
                client2.Connect();
                server2.WaitForConnection();

                var clientFromHandleTrue = new NamedPipeClientStream(PipeDirection.InOut, true, true, new SafePipeHandle(client2.SafePipeHandle.DangerousGetHandle(), ownsHandle: false));
                Assert.Throws<InvalidOperationException>(() => clientFromHandleTrue.Connect());
            }

            // Test obsolete constructor with isConnected: false - should also throw InvalidOperationException 
            // because the constructor now ignores isConnected and always sets state to Connected
            using (var server3 = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
            using (var client3 = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous))
            {
                client3.Connect();
                server3.WaitForConnection();

                var clientFromHandleFalse = new NamedPipeClientStream(PipeDirection.InOut, true, false, new SafePipeHandle(client3.SafePipeHandle.DangerousGetHandle(), ownsHandle: false));
                Assert.Throws<InvalidOperationException>(() => clientFromHandleFalse.Connect());
            }
#pragma warning restore SYSLIB0063
        }
    }
}


Review comment (suggestion):
@copilot The test error "'handle' has already been bound to the thread pool, or was not opened for asynchronous I/O. (Parameter 'handle')" is because you cannot reuse an asynchronous handle.  Don't use isAsync: true for the client for this test.

Also assert `ConnectAsync` too.

Code after:
                Assert.Throws<InvalidOperationException>(clientFromHandle.Connect);
                await Assert.ThrowsAsync<InvalidOperationException>(clientFromHandle.ConnectAsync);
            }

#pragma warning disable SYSLIB0063
            using (var server2 = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.None))
            using (var client2 = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None))
            {
                client2.Connect();
                server2.WaitForConnection();

                var clientFromHandleTrue = new NamedPipeClientStream(PipeDirection.InOut, isAsync: false, isConnected: true, client2.SafePipeHandle);
                Assert.Throws<InvalidOperationException>(clientFromHandleTrue.Connect);
                await Assert.ThrowsAsync<InvalidOperationException>(clientFromHandleTrue.ConnectAsync);
            }

            using (var server3 = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.None))
            using (var client3 = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None))
            {
                client3.Connect();
                server3.WaitForConnection();

                var clientFromHandleFalse = new NamedPipeClientStream(PipeDirection.InOut, isAsync: false, isConnected: false, client3.SafePipeHandle);
                Assert.Throws<InvalidOperationException>(clientFromHandleFalse.Connect);
                await Assert.ThrowsAsync<InvalidOperationException>(clientFromHandleFalse.ConnectAsync);
            }
#pragma warning restore SYSLIB0063
        }
    }
}