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

Code before:
                        safeHandle.DangerousRelease();
                }
            }
        }

        [Fact]
        public static void NamedPipeClientStream_InvalidHandleInerhitability()
        {
            AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new NamedPipeClientStream("a", "b", PipeDirection.Out, 0, TokenImpersonationLevel.Delegation, HandleInheritability.None - 1));
            AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new NamedPipeClientStream("a", "b", PipeDirection.Out, 0, TokenImpersonationLevel.Delegation, HandleInheritability.Inheritable + 1));
        }

        [Fact]
        public static async Task ConnectOnPipeFromExistingHandle_Throws_InvalidOperationException()
        {
            string pipeName = PipeStreamConformanceTests.GetUniquePipeName();

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

                var clientFromHandle = new NamedPipeClientStream(PipeDirection.InOut, false, client1.SafePipeHandle);
                Assert.Throws<InvalidOperationException>(() => clientFromHandle.Connect());
                await Assert.ThrowsAsync<InvalidOperationException>(async () => await 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>(async () => await 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>(async () => await clientFromHandleFalse.ConnectAsync());
            }
#pragma warning restore SYSLIB0063

Review comment (suggestion):
```suggestion
                Assert.Throws<InvalidOperationException>(clientFromHandle.Connect);
                await Assert.ThrowsAsync<InvalidOperationException>(clientFromHandle.ConnectAsync);
```

Code after:
                        safeHandle.DangerousRelease();
                }
            }
        }

        [Fact]
        public static void NamedPipeClientStream_InvalidHandleInerhitability()
        {
            AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new NamedPipeClientStream("a", "b", PipeDirection.Out, 0, TokenImpersonationLevel.Delegation, HandleInheritability.None - 1));
            AssertExtensions.Throws<ArgumentOutOfRangeException>("inheritability", () => new NamedPipeClientStream("a", "b", PipeDirection.Out, 0, TokenImpersonationLevel.Delegation, HandleInheritability.Inheritable + 1));
        }

        [Fact]
        public static async Task ConnectOnPipeFromExistingHandle_Throws_InvalidOperationException()
        {
            string pipeName = PipeStreamConformanceTests.GetUniquePipeName();

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

                var clientFromHandle = new NamedPipeClientStream(PipeDirection.InOut, false, client1.SafePipeHandle);
                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