public async Task UnserializableOutput_ThrowFaultException(string pipeName)
        {
            _serviceMock
            .Setup(x => x.UnserializableOutput())
            .Returns(UnserializableObject.Create());

            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(pipeName);
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, pipeName);
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            IpcFaultException exception = await Assert.ThrowsAnyAsync <IpcFaultException>(async() =>
            {
                await client.InvokeAsync(x => x.UnserializableOutput());
            });

            Assert.Equal(IpcStatus.InternalServerError, exception.Status);
#endif

            IpcFaultException exception2 = await Assert.ThrowsAnyAsync <IpcFaultException>(async() =>
            {
                var request = TestHelpers.CreateIpcRequest("UnserializableOutput");
                await client.InvokeAsync(request);
            });

            Assert.Equal(IpcStatus.InternalServerError, exception2.Status);
        }
        public async Task ServerIsOff_Timeout()
        {
            int timeout = 1000; // 1s
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName          = "inexisted-pipe";
                    options.ConnectionTimeout = timeout;
                });
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            var sw = Stopwatch.StartNew();
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                string output = await client.InvokeAsync(x => x.StringType("abc"));
            });

            Assert.True(sw.ElapsedMilliseconds < timeout * 2); // makesure timeout works with marge
#endif

            var sw2 = Stopwatch.StartNew();
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                var request   = TestHelpers.CreateIpcRequest(typeof(ITestService), "StringType", false, new object[] { "abc" });
                string output = await client.InvokeAsync <string>(request);
            });

            Assert.True(sw2.ElapsedMilliseconds < timeout * 2); // makesure timeout works with marge
        }
        public async Task UnserializableInput_ThrowSerializationException(string pipeName)
        {
            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(pipeName);
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, pipeName);
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            await Assert.ThrowsAnyAsync <IpcSerializationException>(async() =>
            {
                await client.InvokeAsync(x => x.UnserializableInput(UnserializableObject.Create()));
            });
#endif

            await Assert.ThrowsAnyAsync <IpcSerializationException>(async() =>
            {
                var request = TestHelpers.CreateIpcRequest(typeof(ITestService), "UnserializableInput", UnserializableObject.Create());
                await client.InvokeAsync(request);
            });
        }
        public async Task HappyPath(string input, string expected)
        {
#if !DISABLE_DYNAMIC_CODE_GENERATION
            _serviceMock
            .Setup(x => x.StringType(input))
            .Returns(expected);

            string actual = await _client
                            .InvokeAsync(x => x.StringType(input));

            Assert.Equal(expected, actual);
#endif
        }
#pragma warning disable CS1998,xUnit1026 // Async method lacks 'await' operators and will run synchronously
        public async Task HappyPath(string input, string expected)
#pragma warning restore CS1998,xUnit1026 // Async method lacks 'await' operators and will run synchronously
        {
#if !DISABLE_DYNAMIC_CODE_GENERATION
            _serviceMock
            .Setup(x => x.StringType(input))
            .Returns(expected);

            string actual = await _client
                            .InvokeAsync(x => x.StringType(input));

            Assert.Equal(expected, actual);
#endif
        }
Пример #6
0
        public async Task Exception_ThrowWithDetails(string pipeName, string details)
        {
            _serviceMock.Setup(x => x.ThrowException())
            .Throws(new Exception(details));

            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(options =>
                {
                    options.PipeName = pipeName;
                    options.IncludeFailureDetailsInResponse = true;
                });
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName = pipeName;
                });
            });

            IpcFaultException actual = await Assert.ThrowsAsync <IpcFaultException>(async() =>
            {
                await client.InvokeAsync(x => x.ThrowException());
            });

            Assert.NotNull(actual.InnerException);
            Assert.NotNull(actual.InnerException.InnerException);
            Assert.Equal(details, actual.InnerException.InnerException.Message);
        }
Пример #7
0
        public void ConnectionCancelled_Throw()
        {
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddTcpIpcClient <ITestService>(name, (_, options) =>
                {
                    // Connect to a non-routable IP address can trigger timeout
                    options.ServerIp = IPAddress.Parse("10.0.0.0");
                });
            });

            using (var cts = new CancellationTokenSource())
            {
                Task.WaitAll(
                    Task.Run(async() =>
                {
                    await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                    {
                        var request = TestHelpers.CreateIpcRequest(typeof(ITestService), "StringType", new object[] { string.Empty });
                        await client.InvokeAsync(request, cts.Token);
                    });
                }),
                    Task.Run(() => cts.CancelAfter(1000)));
            }
        }
Пример #8
0
        public async Task StreamTranslator_HappyPath(string pipeName, string input, string expected)
        {
            _serviceMock
            .Setup(x => x.StringType(input))
            .Returns(expected);

            IIpcClient <ITestService> client = _factory
                                               .WithServiceImplementation(_ => _serviceMock.Object)
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(options =>
                {
                    options.PipeName         = pipeName;
                    options.StreamTranslator = x => new XorStream(x);
                });
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName         = pipeName;
                    options.StreamTranslator = x => new XorStream(x);
                });
            });

            string actual = await client.InvokeAsync(x => x.StringType(input));

            Assert.Equal(expected, actual);
        }
Пример #9
0
        public async Task HappyPath(string input, string expected)
        {
            _serviceMock
            .Setup(x => x.StringType(input))
            .Returns(expected);

            string actual = await _client
                            .InvokeAsync(x => x.StringType(input));

            Assert.Equal(expected, actual);
        }
        public void ConnectionCancelled_Throw()
        {
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName          = "inexisted-pipe";
                    options.ConnectionTimeout = Timeout.Infinite;
                });
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            using (var cts = new CancellationTokenSource())
            {
                Task.WaitAll(
                    Task.Run(async() =>
                {
                    await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                    {
                        await client.InvokeAsync(x => x.ReturnVoid(), cts.Token);
                    });
                }),
                    Task.Run(() => cts.CancelAfter(1000)));
            }
#endif
            using (var cts = new CancellationTokenSource())
            {
                Task.WaitAll(
                    Task.Run(async() =>
                {
                    await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                    {
                        var request = TestHelpers.CreateIpcRequest("ReturnVoid");
                        await client.InvokeAsync(request, cts.Token);
                    });
                }),
                    Task.Run(() => cts.CancelAfter(1000)));
            }
        }
Пример #11
0
        public async Task Exception_ThrowWithoutDetails(string pipeName, string details)
        {
            _serviceMock.Setup(x => x.ThrowException())
            .Throws(new Exception(details));

            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(options =>
                {
                    options.PipeName = pipeName;
                    options.IncludeFailureDetailsInResponse = false;
                });
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName = pipeName;
                });
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            IpcFaultException actual = await Assert.ThrowsAsync <IpcFaultException>(async() =>
            {
                await client.InvokeAsync(x => x.ThrowException());
            });

            Assert.Null(actual.InnerException);
#endif

            IpcFaultException actual2 = await Assert.ThrowsAsync <IpcFaultException>(async() =>
            {
                var request = TestHelpers.CreateIpcRequest("ThrowException");
                await client.InvokeAsync(request);
            });

            Assert.Null(actual2.InnerException);
        }
        public async Task StreamTranslator_HappyPath(string pipeName, string input, string expected)
        {
            _serviceMock
            .Setup(x => x.StringType(input))
            .Returns(expected);

            IIpcClient <ITestService> client = _factory
                                               .WithServiceImplementation(_ => _serviceMock.Object)
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(options =>
                {
                    options.PipeName         = pipeName;
                    options.StreamTranslator = x => new XorStream(x);
                });
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName         = pipeName;
                    options.StreamTranslator = x => new XorStream(x);
                });
            });

#if !DISABLE_DYNAMIC_CODE_GENERATION
            string actual = await client.InvokeAsync(x => x.StringType(input));

            Assert.Equal(expected, actual);
#endif

            var request = TestHelpers.CreateIpcRequest(typeof(ITestService), "StringType", false, new object[] { input });
            var actual2 = await client.InvokeAsync <string>(request);

            Assert.Equal(expected, actual2);
        }
Пример #13
0
        public async Task UnserializableInput_ThrowSerializationException(string pipeName)
        {
            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(pipeName);
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, pipeName);
            });

            await Assert.ThrowsAnyAsync <IpcSerializationException>(async() =>
            {
                await client.InvokeAsync(x => x.UnserializableInput(UnserializableObject.Create()));
            });
        }
Пример #14
0
        public async Task MultipleEndpoints(
            Mock <ITestService> service1,
            Mock <ITestService2> service2)
        {
            IHost host = Host.CreateDefaultBuilder()
                         .ConfigureServices(services =>
            {
                services
                .AddScoped(x => service1.Object)
                .AddScoped(x => service2.Object);
            })
                         .ConfigureIpcHost(builder =>
            {
                builder
                .AddNamedPipeEndpoint <ITestService>("pipe1")
                .AddNamedPipeEndpoint <ITestService2>("pipe2");
            })
                         .Build();

            await host.StartAsync();

            ServiceProvider clientServiceProvider = new ServiceCollection()
                                                    .AddNamedPipeIpcClient <ITestService>("client1", "pipe1")
                                                    .AddNamedPipeIpcClient <ITestService2>("client2", "pipe2")
                                                    .BuildServiceProvider();

            IIpcClient <ITestService> client1 = clientServiceProvider
                                                .GetRequiredService <IIpcClientFactory <ITestService> >()
                                                .CreateClient("client1");

            await client1.InvokeAsync(x => x.ReturnVoid());

            service1.Verify(x => x.ReturnVoid(), Times.Once);

            IIpcClient <ITestService2> client2 = clientServiceProvider
                                                 .GetRequiredService <IIpcClientFactory <ITestService2> >()
                                                 .CreateClient("client2");
            await client2.InvokeAsync(x => x.SomeMethod());

            service2.Verify(x => x.SomeMethod(), Times.Once);
        }
Пример #15
0
        public async Task ServerIsOff_Timeout()
        {
            int timeout = 1000; // 1s
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName          = "inexisted-pipe";
                    options.ConnectionTimeout = timeout;
                });
            });

            var sw = Stopwatch.StartNew();
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                string output = await client.InvokeAsync(x => x.StringType("abc"));
            });

            Assert.True(sw.ElapsedMilliseconds < timeout * 2); // makesure timeout works with marge
        }
Пример #16
0
        public async Task ConnectionTimeout_Throw()
        {
            int timeout = 3000; // 3s
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddTcpIpcClient <ITestService>(name, (_, options) =>
                {
                    // Connect to a non-routable IP address can trigger timeout
                    options.ServerIp          = IPAddress.Parse("10.0.0.0");
                    options.ConnectionTimeout = timeout;
                });
            });

            var sw = Stopwatch.StartNew();
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                string output = await client.InvokeAsync(x => x.StringType("abc"));
            });

            Assert.True(sw.ElapsedMilliseconds < timeout * 2); // makesure timeout works with marge
        }
Пример #17
0
        public async Task UnserializableOutput_ThrowFaultException(string pipeName)
        {
            _serviceMock
            .Setup(x => x.UnserializableOutput())
            .Returns(UnserializableObject.Create());

            IIpcClient <ITestService> client = _factory
                                               .WithIpcHostConfiguration(hostBuilder =>
            {
                hostBuilder.AddNamedPipeEndpoint <ITestService>(pipeName);
            })
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, pipeName);
            });

            IpcFaultException exception = await Assert.ThrowsAnyAsync <IpcFaultException>(async() =>
            {
                await client.InvokeAsync(x => x.UnserializableOutput());
            });

            Assert.Equal(IpcStatus.InternalServerError, exception.Status);
        }
Пример #18
0
        private static async Task Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Type a phrase and press enter or press Ctrl+C to exit:");
                string input = Console.ReadLine();

                // register IPC clients
                ServiceProvider serviceProvider = new ServiceCollection()
                                                  .AddNamedPipeIpcClient <IInterProcessService>("client1", pipeName: "pipeinternal")
                                                  .BuildServiceProvider();

                // resolve IPC client factory
                IIpcClientFactory <IInterProcessService> clientFactory = serviceProvider
                                                                         .GetRequiredService <IIpcClientFactory <IInterProcessService> >();

                // create client
                IIpcClient <IInterProcessService> client = clientFactory.CreateClient("client1");

                string output = await client.InvokeAsync(x => x.ReverseString(input));

                Console.WriteLine($"Result from server: '{output}'.\n");
            }
        }
Пример #19
0
        public void ConnectionCancelled_Throw()
        {
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddNamedPipeIpcClient <ITestService>(name, (_, options) =>
                {
                    options.PipeName          = "inexisted-pipe";
                    options.ConnectionTimeout = Timeout.Infinite;
                });
            });

            using var cts = new CancellationTokenSource();

            Task.WaitAll(
                Task.Run(async() =>
            {
                await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                {
                    await client.InvokeAsync(x => x.ReturnVoid(), cts.Token);
                });
            }),
                Task.Run(() => cts.CancelAfter(1000)));
        }
Пример #20
0
        public void ConnectionCancelled_Throw()
        {
            IIpcClient <ITestService> client = _factory
                                               .CreateClient((name, services) =>
            {
                services.AddTcpIpcClient <ITestService>(name, (_, options) =>
                {
                    // Connect to a non-routable IP address can trigger timeout
                    options.ServerIp = IPAddress.Parse("10.0.0.0");
                });
            });

            using var cts = new CancellationTokenSource();

            Task.WaitAll(
                Task.Run(async() =>
            {
                await Assert.ThrowsAsync <OperationCanceledException>(async() =>
                {
                    await client.InvokeAsync(x => x.StringType(string.Empty), cts.Token);
                });
            }),
                Task.Run(() => cts.CancelAfter(1000)));
        }
        public async Task PrimitiveTypes(bool a, byte b, sbyte c, char d, decimal e, double f, float g, int h, uint i,
                                         long j, ulong k, short l, ushort m, int expected)
        {
            _serviceMock
            .Setup(x => x.PrimitiveTypes(a, b, c, d, e, f, g, h, i, j, k, l, m))
            .Returns(expected);

            int actual = await _client
                         .InvokeAsync(x => x.PrimitiveTypes(a, b, c, d, e, f, g, h, i, j, k, l, m));

            Assert.Equal(expected, actual);
        }
Пример #22
0
        public async Task MultipleEndpoints(
            Mock <ITestService> service1,
            Mock <ITestService2> service2)
        {
            IHost host = Host.CreateDefaultBuilder()
                         .ConfigureServices(services =>
            {
                services
                .AddScoped(x => service1.Object)
                .AddScoped(x => service2.Object);
            })
                         .ConfigureIpcHost(builder =>
            {
                builder
                .AddNamedPipeEndpoint <ITestService>("pipe1")
                .AddNamedPipeEndpoint <ITestService2>("pipe2");
            })
                         .Build();

            await host.StartAsync();

            ServiceProvider clientServiceProvider = new ServiceCollection()
                                                    .AddNamedPipeIpcClient <ITestService>("client1", "pipe1")
                                                    .AddNamedPipeIpcClient <ITestService2>("client2", "pipe2")
                                                    .BuildServiceProvider();

            IIpcClient <ITestService> client1 = clientServiceProvider
                                                .GetRequiredService <IIpcClientFactory <ITestService> >()
                                                .CreateClient("client1");

#if !DISABLE_DYNAMIC_CODE_GENERATION
            await client1.InvokeAsync(x => x.ReturnVoid());

            service1.Verify(x => x.ReturnVoid(), Times.Once);
#endif

            var request = TestHelpers.CreateIpcRequest("ReturnVoid");
            await client1.InvokeAsync(request);

#if !DISABLE_DYNAMIC_CODE_GENERATION
            service1.Verify(x => x.ReturnVoid(), Times.Exactly(2));
#else
            service1.Verify(x => x.ReturnVoid(), Times.Once);
#endif

            IIpcClient <ITestService2> client2 = clientServiceProvider
                                                 .GetRequiredService <IIpcClientFactory <ITestService2> >()
                                                 .CreateClient("client2");

#if !DISABLE_DYNAMIC_CODE_GENERATION
            await client2.InvokeAsync(x => x.SomeMethod());

            service2.Verify(x => x.SomeMethod(), Times.Once);
#endif

            request = TestHelpers.CreateIpcRequest("SomeMethod");
            await client2.InvokeAsync(request);

#if !DISABLE_DYNAMIC_CODE_GENERATION
            service2.Verify(x => x.SomeMethod(), Times.Exactly(2));
#else
            service2.Verify(x => x.SomeMethod(), Times.Once);
#endif
        }
        public async Task PrimitiveTypes(bool a, byte b, sbyte c, char d, decimal e, double f, float g, int h, uint i,
                                         long j, ulong k, short l, ushort m, int expected)
        {
            _serviceMock
            .Setup(x => x.PrimitiveTypes(a, b, c, d, e, f, g, h, i, j, k, l, m))
            .Returns(expected);

#if !DISABLE_DYNAMIC_CODE_GENERATION
            int actual = await _client
                         .InvokeAsync(x => x.PrimitiveTypes(a, b, c, d, e, f, g, h, i, j, k, l, m));

            Assert.Equal(expected, actual);
#endif

            var request = TestHelpers.CreateIpcRequest(typeof(ITestService), "PrimitiveTypes", a, b, c, d, e, f, g, h, i, j, k, l, m);
            int actual2 = await _client.InvokeAsync <int>(request);

            Assert.Equal(expected, actual2);
        }