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

Code before:
                Assert.Throws<InvalidOperationException>(() => clientFromHandle.Connect());
                Assert.ThrowsAsync<InvalidOperationException>(async () => await clientFromHandle.ConnectAsync()).GetAwaiter().GetResult();
            }

            // Test obsolete constructor with isConnected: true - should throw InvalidOperationException when Connect and ConnectAsync are called
#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, false, true, new SafePipeHandle(client2.SafePipeHandle.DangerousGetHandle(), ownsHandle: false));
                Assert.Throws<InvalidOperationException>(() => clientFromHandleTrue.Connect());
                Assert.ThrowsAsync<InvalidOperationException>(async () => await clientFromHandleTrue.ConnectAsync()).GetAwaiter().GetResult();
            }

            // 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.None))
            using (var client3 = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None))
            {
                client3.Connect();
                server3.WaitForConnection();

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


Review comment (suggestion):
```suggestion
                var clientFromHandleFalse = new NamedPipeClientStream(PipeDirection.InOut, isAsync: false, isConnected: false, client3.SafePipeHandle);
```

Code after:
                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
        }
    }
}