Feature/poll consul (#392)

* WIP - implement a consul service discovery poller, lots of shared code with existing, refactor next and a todo in the docs to finish

* #374 implement polling for consul as option

* #374 updated docs to remove todo

* #374 fixed failing unit test

* #374 fixed failing unit test

* #374 fixed failing acceptance test
This commit is contained in:
Tom Pallister
2018-06-12 00:58:08 +03:00
committed by GitHub
parent 14308ff5fb
commit 0f2a9c1d0d
12 changed files with 649 additions and 369 deletions

View File

@ -458,6 +458,64 @@ namespace Ocelot.AcceptanceTests
.BDDfy();
}
[Fact]
public void should_handle_request_to_poll_consul_for_downstream_service_and_make_request()
{
const int consulPort = 8518;
const string serviceName = "web";
const int downstreamServicePort = 8082;
var downstreamServiceOneUrl = $"http://localhost:{downstreamServicePort}";
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
var serviceEntryOne = new ServiceEntry()
{
Service = new AgentService()
{
Service = serviceName,
Address = "localhost",
Port = downstreamServicePort,
ID = $"web_90_0_2_224_{downstreamServicePort}",
Tags = new[] {"version-v1"}
},
};
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/api/home",
DownstreamScheme = "http",
UpstreamPathTemplate = "/home",
UpstreamHttpMethod = new List<string> { "Get", "Options" },
ServiceName = serviceName,
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" },
UseServiceDiscovery = true,
}
},
GlobalConfiguration = new FileGlobalConfiguration()
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
{
Host = "localhost",
Port = consulPort,
Type = "PollConsul",
PollingInterval = 0
}
}
};
this.Given(x => x.GivenThereIsAServiceRunningOn(downstreamServiceOneUrl, "/api/home", 200, "Hello from Laura"))
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
.And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
.And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGatewayWaitingForTheResponseToBeOk("/home"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
.BDDfy();
}
private void WhenIAddAServiceBackIn(ServiceEntry serviceEntryTwo)
{
_consulServices.Add(serviceEntryTwo);

View File

@ -27,6 +27,7 @@ using System.Text;
using static Ocelot.AcceptanceTests.HttpDelegatingHandlersTests;
using Ocelot.Requester;
using Ocelot.Middleware.Multiplexer;
using static Ocelot.Infrastructure.Wait;
namespace Ocelot.AcceptanceTests
{
@ -675,6 +676,24 @@ namespace Ocelot.AcceptanceTests
_response = _ocelotClient.GetAsync(url).Result;
}
public void WhenIGetUrlOnTheApiGatewayWaitingForTheResponseToBeOk(string url)
{
var result = WaitFor(2000).Until(() => {
try
{
_response = _ocelotClient.GetAsync(url).Result;
_response.EnsureSuccessStatusCode();
return true;
}
catch(Exception)
{
return false;
}
});
result.ShouldBeTrue();
}
public void WhenIGetUrlOnTheApiGateway(string url, string cookie, string value)
{
var request = _ocelotServer.CreateRequest(url);