Debian Codegraph @

external/corefx/src/System.IO.Pipes/tests/AnonymousPipeTests/AnonymousPipeTest.CrossProcess.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Diagnostics;
6 using Xunit;
7
8 namespace System.IO.Pipes.Tests
9 {
10 public class AnonymousPipeTest_CrossProcess : RemoteExecutorTestBase
11 {
12 [Fact]
13 [ActiveIssue("https://github.com/dotnet/corefx/issues/21275", TargetFrameworkMonikers.Uap)]
14 public void PingPong()
15 {
16 // Create two anonymous pipes, one for each direction of communication.
17 // Then spawn another process to communicate with.
18 using (var outbound = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
19 using (var inbound = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
20 using (var remote = RemoteInvoke(new Func<string, string, int>(PingPong_OtherProcess), outbound.GetClientHandleAsString(), inbound.GetClientHandleAsString()))
21 {
22 // Close our local copies of the handles now that we've passed them of to the other process
23 outbound.DisposeLocalCopyOfClientHandle();
24 inbound.DisposeLocalCopyOfClientHandle();
25
26 // Ping-pong back and forth by writing then reading bytes one at a time.
27 for (byte i = 0; i < 10; i++)
28 {
29 outbound.WriteByte(i);
30 int received = inbound.ReadByte();
31 Assert.Equal(i, received);
32 }
33 }
34 }
35
36 private static int PingPong_OtherProcess(string inHandle, string outHandle)
37 {
38 // Create the clients associated with the supplied handles
39 using (var inbound = new AnonymousPipeClientStream(PipeDirection.In, inHandle))
40 using (var outbound = new AnonymousPipeClientStream(PipeDirection.Out, outHandle))
41 {
42 // Repeatedly read then write a byte from and to the server
43 for (int i = 0; i < 10; i++)
44 {
45 int b = inbound.ReadByte();
46 outbound.WriteByte((byte)b);
47 }
48 }
49 return SuccessExitCode;
50 }
51
52 }
53 }