public async Task UnaryCall_MultipleCalls_RoundRobin() { // Ignore errors SetExpectedErrorsFilter(writeContext => { return(true); }); string?host = null; Task <HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context) { host = context.Host; return(Task.FromResult(new HelloReply { Message = request.Name })); } // Arrange using var endpoint1 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50051, UnaryMethod, nameof(UnaryMethod)); using var endpoint2 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50052, UnaryMethod, nameof(UnaryMethod)); var channel = await BalancerHelpers.CreateChannel(LoggerFactory, new RoundRobinConfig(), new[] { endpoint1.Address, endpoint2.Address }, connect : true); await BalancerHelpers.WaitForSubchannelsToBeReadyAsync(Logger, channel, 2).DefaultTimeout(); var client = TestClientFactory.Create(channel, endpoint1.Method); // Act var reply = await client.UnaryCall(new HelloRequest { Name = "Balancer" }).ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("Balancer", reply.Message); var nextHost = GetNextHost(host !); // Act reply = await client.UnaryCall(new HelloRequest { Name = "Balancer" }).ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("Balancer", reply.Message); Assert.AreEqual(nextHost, host !); nextHost = GetNextHost(host !); // Act reply = await client.UnaryCall(new HelloRequest { Name = "Balancer" }).ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("Balancer", reply.Message); Assert.AreEqual(nextHost, host); string GetNextHost(string host) { return(host == "127.0.0.1:50051" ? "127.0.0.1:50052" : "127.0.0.1:50051"); } }
public async Task Resolver_SubchannelTransientFailure_ResolverRefreshed() { // Ignore errors SetExpectedErrorsFilter(writeContext => { return(true); }); Task <HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context) { return(Task.FromResult(new HelloReply { Message = request.Name })); } // Arrange using var endpoint1 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50051, UnaryMethod, nameof(UnaryMethod)); using var endpoint2 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50052, UnaryMethod, nameof(UnaryMethod)); SyncPoint?syncPoint = new SyncPoint(runContinuationsAsynchronously: true); syncPoint.Continue(); var resolver = new TestResolver(LoggerFactory, async() => { await syncPoint.WaitToContinue().DefaultTimeout(); syncPoint = new SyncPoint(runContinuationsAsynchronously: true); }); resolver.UpdateAddresses(new List <BalancerAddress> { new BalancerAddress(endpoint1.Address.Host, endpoint1.Address.Port), new BalancerAddress(endpoint2.Address.Host, endpoint2.Address.Port) }); var channel = await BalancerHelpers.CreateChannel(LoggerFactory, new RoundRobinConfig(), resolver, connect : true); await BalancerHelpers.WaitForSubchannelsToBeReadyAsync(Logger, channel, 2).DefaultTimeout(); var waitForRefreshTask = syncPoint.WaitForSyncPoint(); endpoint1.Dispose(); await waitForRefreshTask.DefaultTimeout(); resolver.UpdateAddresses(new List <BalancerAddress> { new BalancerAddress(endpoint2.Address.Host, endpoint2.Address.Port) }); syncPoint.Continue(); await BalancerHelpers.WaitForSubchannelsToBeReadyAsync(Logger, channel, 1).DefaultTimeout(); }
public async Task UnaryCall_UnavailableAddress_FallbackToWorkingAddress() { // Ignore errors SetExpectedErrorsFilter(writeContext => { return(true); }); string?host = null; Task <HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context) { host = context.Host; return(Task.FromResult(new HelloReply { Message = request.Name })); } // Arrange using var endpoint1 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50051, UnaryMethod, nameof(UnaryMethod)); using var endpoint2 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50052, UnaryMethod, nameof(UnaryMethod)); var channel = await BalancerHelpers.CreateChannel(LoggerFactory, new RoundRobinConfig(), new[] { endpoint1.Address, endpoint2.Address }, connect : true); await BalancerHelpers.WaitForSubchannelsToBeReadyAsync(Logger, channel, 2).DefaultTimeout(); var client = TestClientFactory.Create(channel, endpoint1.Method); var reply1 = await client.UnaryCall(new HelloRequest { Name = "Balancer1" }); Assert.AreEqual("Balancer1", reply1.Message); var host1 = host; var reply2 = await client.UnaryCall(new HelloRequest { Name = "Balancer2" }); Assert.AreEqual("Balancer2", reply2.Message); var host2 = host; Assert.Contains("127.0.0.1:50051", new[] { host1, host2 }); Assert.Contains("127.0.0.1:50052", new[] { host1, host2 }); endpoint1.Dispose(); var subChannel = await BalancerHelpers.WaitForSubchannelToBeReadyAsync(Logger, channel).DefaultTimeout(); Assert.AreEqual(50052, subChannel.CurrentAddress?.EndPoint.Port); reply1 = await client.UnaryCall(new HelloRequest { Name = "Balancer" }); Assert.AreEqual("Balancer", reply1.Message); Assert.AreEqual("127.0.0.1:50052", host); }
public async Task UnaryCall_MultipleCalls_RoundRobin() { // Ignore errors SetExpectedErrorsFilter(writeContext => { return(true); }); SyncPoint?syncPoint = null; string? host = null; async Task <HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context) { host = context.Host; if (syncPoint != null) { await syncPoint.WaitToContinue(); } return(new HelloReply { Message = request.Name }); } // Arrange using var endpoint1 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50051, UnaryMethod, nameof(UnaryMethod)); using var endpoint2 = BalancerHelpers.CreateGrpcEndpoint <HelloRequest, HelloReply>(50052, UnaryMethod, nameof(UnaryMethod)); var channel = await BalancerHelpers.CreateChannel(LoggerFactory, new LoadBalancingConfig("least_used"), new[] { endpoint1.Address, endpoint2.Address }, connect : true); await BalancerHelpers.WaitForSubchannelsToBeReadyAsync( Logger, channel, expectedCount : 2, getPickerSubchannels : picker => ((LeastUsedPicker?)picker)?._subchannels.ToArray() ?? Array.Empty <Subchannel>()).DefaultTimeout(); var client = TestClientFactory.Create(channel, endpoint1.Method); // Act var reply = await client.UnaryCall(new HelloRequest { Name = "Balancer" }).ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("Balancer", reply.Message); Assert.AreEqual("127.0.0.1:50051", host); // Act reply = await client.UnaryCall(new HelloRequest { Name = "Balancer" }).ResponseAsync.DefaultTimeout(); // Assert Assert.AreEqual("Balancer", reply.Message); Assert.AreEqual("127.0.0.1:50051", host); // Act var sp1 = syncPoint = new SyncPoint(runContinuationsAsynchronously: true); var pendingCall1 = client.UnaryCall(new HelloRequest { Name = "Balancer" }); // Assert await syncPoint.WaitForSyncPoint().DefaultTimeout(); Assert.AreEqual("127.0.0.1:50051", host); // Act var sp2 = syncPoint = new SyncPoint(runContinuationsAsynchronously: true); var pendingCall2 = client.UnaryCall(new HelloRequest { Name = "Balancer" }); // Assert await syncPoint.WaitForSyncPoint().DefaultTimeout(); Assert.AreEqual("127.0.0.1:50052", host); // Act var sp3 = syncPoint = new SyncPoint(runContinuationsAsynchronously: true); var pendingCall3 = client.UnaryCall(new HelloRequest { Name = "Balancer" }); // Assert await syncPoint.WaitForSyncPoint().DefaultTimeout(); Assert.AreEqual("127.0.0.1:50051", host); sp1.Continue(); sp2.Continue(); sp3.Continue(); }