|
| 1 | +using AutoFixture.Xunit2; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Globalization; |
| 6 | +using System.Net; |
| 7 | +using System.Numerics; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace JKang.IpcServiceFramework.IntegrationTests |
| 13 | +{ |
| 14 | + public class ContractTest : IDisposable |
| 15 | + { |
| 16 | + private readonly CancellationTokenSource _cancellationToken; |
| 17 | + private readonly int _port; |
| 18 | + private readonly IpcServiceClient<ITestService> _client; |
| 19 | + |
| 20 | + public ContractTest() |
| 21 | + { |
| 22 | + // configure DI |
| 23 | + IServiceCollection services = new ServiceCollection() |
| 24 | + .AddIpc(builder => builder.AddNamedPipe().AddService<ITestService, TestService>()); |
| 25 | + _port = new Random().Next(10000, 50000); |
| 26 | + IIpcServiceHost host = new IpcServiceHostBuilder(services.BuildServiceProvider()) |
| 27 | + .AddTcpEndpoint<ITestService>( |
| 28 | + name: Guid.NewGuid().ToString(), |
| 29 | + ipEndpoint: IPAddress.Loopback, |
| 30 | + port: _port) |
| 31 | + .Build(); |
| 32 | + _cancellationToken = new CancellationTokenSource(); |
| 33 | + host.RunAsync(_cancellationToken.Token); |
| 34 | + |
| 35 | + _client = new IpcServiceClientBuilder<ITestService>() |
| 36 | + .UseTcp(IPAddress.Loopback, _port) |
| 37 | + .Build(); |
| 38 | + } |
| 39 | + |
| 40 | + [Theory, AutoData] |
| 41 | + public async Task SimpleType(float a, float b) |
| 42 | + { |
| 43 | + float actual = await _client.InvokeAsync(x => x.AddFloat(a, b)); |
| 44 | + float expected = new TestService().AddFloat(a, b); |
| 45 | + Assert.Equal(expected, actual); |
| 46 | + } |
| 47 | + |
| 48 | + [Theory, AutoData] |
| 49 | + public async Task ComplexType(Complex a, Complex b) |
| 50 | + { |
| 51 | + Complex actual = await _client.InvokeAsync(x => x.AddComplex(a, b)); |
| 52 | + Complex expected = new TestService().AddComplex(a, b); |
| 53 | + Assert.Equal(expected, actual); |
| 54 | + } |
| 55 | + |
| 56 | + [Theory, AutoData] |
| 57 | + public async Task ComplexTypeArray(IEnumerable<Complex> array) |
| 58 | + { |
| 59 | + Complex actual = await _client.InvokeAsync(x => x.SumComplexArray(array)); |
| 60 | + Complex expected = new TestService().SumComplexArray(array); |
| 61 | + Assert.Equal(expected, actual); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task ReturnVoid() |
| 66 | + { |
| 67 | + await _client.InvokeAsync(x => x.DoNothing()); |
| 68 | + } |
| 69 | + |
| 70 | + [Theory, InlineData(" 2008-06-11T16:11:20.0904778Z", DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces)] |
| 71 | + public async Task EnumParameter(string value, DateTimeStyles styles) |
| 72 | + { |
| 73 | + DateTime actual = await _client.InvokeAsync(x => x.ParseDate(value, styles)); |
| 74 | + DateTime expected = new TestService().ParseDate(value, styles); |
| 75 | + Assert.Equal(expected, actual); |
| 76 | + } |
| 77 | + |
| 78 | + [Theory, AutoData] |
| 79 | + public async Task ByteArray(byte[] input) |
| 80 | + { |
| 81 | + byte[] actual = await _client.InvokeAsync(x => x.ReverseBytes(input)); |
| 82 | + byte[] expected = new TestService().ReverseBytes(input); |
| 83 | + Assert.Equal(expected, actual); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public async Task GenericParameter() |
| 88 | + { |
| 89 | + decimal actual = await _client.InvokeAsync(x => x.GetDefaultValue<decimal>()); |
| 90 | + decimal expected = new TestService().GetDefaultValue<decimal>(); |
| 91 | + Assert.Equal(expected, actual); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task AsyncOperation() |
| 96 | + { |
| 97 | + long actual = await _client.InvokeAsync(x => x.WaitAsync(500)); |
| 98 | + Assert.True(actual >= 450); |
| 99 | + } |
| 100 | + |
| 101 | + public void Dispose() |
| 102 | + { |
| 103 | + _cancellationToken.Cancel(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments