Skip to content

Commit

Permalink
Add logging for invalid service configuration error in RoundRobin loa…
Browse files Browse the repository at this point in the history
…d balancer
  • Loading branch information
antikorol committed Jul 10, 2024
1 parent 0e9a616 commit bbed7d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Ocelot/LoadBalancer/LoadBalancers/RoundRobin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public async Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
}

var next = services[_last++];

if (next == null)
{
return new ErrorResponse<ServiceHostAndPort>(new ServicesAreNullError($"The service with index {_last} was null in {nameof(RoundRobin)} during the {nameof(Lease)} operation. Total services count: {services.Count}."));
}

if (next.HostAndPort == null)
{
return new ErrorResponse<ServiceHostAndPort>(new ServicesAreNullError($"The {nameof(next.HostAndPort)} was null in the service with index {_last} in {nameof(RoundRobin)} during the {nameof(Lease)} operation. Total services count: {services.Count}."));
}

return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
}
}
Expand Down
35 changes: 34 additions & 1 deletion test/Ocelot.UnitTests/LoadBalancer/RoundRobinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ocelot.UnitTests.LoadBalancer
public class RoundRobinTests : UnitTest
{
private readonly RoundRobin _roundRobin;
private readonly List<Service> _services;
private List<Service> _services;
private Response<ServiceHostAndPort> _hostAndPort;
private readonly HttpContext _httpContext;

Expand Down Expand Up @@ -54,14 +54,47 @@ public async Task should_go_back_to_first_address_after_finished_last()
}
}

[Fact]
public void should_return_error_if_selected_service_is_null()
{
Service invalidService = null;

this.Given(x => x.GivenServices(new List<Service> { invalidService }))
.And(x => x.GivenIGetTheNextAddress())
.Then(x => x.ThenServiceAreNullErrorIsReturned())
.BDDfy();
}

[Fact]
public void should_return_error_if_host_and_port_is_null_in_the_selected_service()
{
var invalidService = new Service(string.Empty, null, string.Empty, string.Empty, new List<string>());

this.Given(x => x.GivenServices(new List<Service> { invalidService }))
.And(x => x.GivenIGetTheNextAddress())
.Then(x => x.ThenServiceAreNullErrorIsReturned())
.BDDfy();
}

private void GivenIGetTheNextAddress()
{
_hostAndPort = _roundRobin.Lease(_httpContext).Result;
}

private void GivenServices(List<Service> services)
{
_services = services;
}

private void ThenTheNextAddressIndexIs(int index)
{
_hostAndPort.Data.ShouldBe(_services[index].HostAndPort);
}

private void ThenServiceAreNullErrorIsReturned()
{
_hostAndPort.IsError.ShouldBeTrue();
_hostAndPort.Errors[0].ShouldBeOfType<ServicesAreNullError>();
}
}
}

0 comments on commit bbed7d1

Please sign in to comment.