Skip to content

Commit

Permalink
#1700 Create custom Ocelot config file when instantiating steps durin…
Browse files Browse the repository at this point in the history
…g parallel execution (#1703)

* When instantiating steps, creating a custom ocelot config file name. Finally deleting the newly created config file when disposing the Step instance.

* Removing DeleteOcelotConfig from GivenThereIsAConfiguration, since File.WriteAllText will overwrite the file.

* using xunit.runner.json it's getting on my nerves

* Trying yet another strategy

* Revert "using xunit.runner.json it's getting on my nerves"

This reverts commit 9fac778.

* Not sure about GC.SuppressFinalize here

* correct usage of Wait.WaitFor...

* Do we really need to get a random port? Wouldn't be enough to get the next available port? Adding lock for thread safety

* Updating duration of break in Open_circuit_should_not_effect_different_routes from 1000 ms to 1500 ms

* ConfigurationReloadTests as sealed class

* moving variables initializations to Steps constructor

* putting back during of break to 1000, out of scope of current PR

* Code review

---------

Co-authored-by: raman-m <dotnet044@gmail.com>
  • Loading branch information
ggnaegi and raman-m committed Sep 25, 2023
1 parent 75cd0b3 commit fdad15d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 150 deletions.
6 changes: 2 additions & 4 deletions test/Ocelot.AcceptanceTests/ConfigurationReloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Ocelot.AcceptanceTests
{
public class ConfigurationReloadTests : IDisposable
public sealed class ConfigurationReloadTests : IDisposable
{
private readonly FileConfiguration _initialConfig;
private readonly FileConfiguration _anotherConfig;
Expand Down Expand Up @@ -42,8 +42,7 @@ public void should_reload_config_on_change()
this.Given(x => _steps.GivenThereIsAConfiguration(_initialConfig))
.And(x => _steps.GivenOcelotIsRunningReloadingConfig(true))
.And(x => _steps.GivenThereIsAConfiguration(_anotherConfig))
.And(x => _steps.GivenIWait(5000))
.And(x => _steps.ThenConfigShouldBe(_anotherConfig))
.And(x => _steps.ThenConfigShouldBeWithTimeout(_anotherConfig, 10000))
.BDDfy();
}

Expand Down Expand Up @@ -88,7 +87,6 @@ public void should_not_trigger_change_token_with_no_change()
public void Dispose()
{
_steps.Dispose();
GC.SuppressFinalize(this);
}
}
}
16 changes: 7 additions & 9 deletions test/Ocelot.AcceptanceTests/CustomMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Ocelot.AcceptanceTests
{
public class CustomMiddlewareTests : IDisposable
{
private readonly string _configurationPath;
private readonly Steps _steps;
private int _counter;
private readonly ServiceHandler _serviceHandler;
Expand All @@ -30,7 +29,6 @@ public CustomMiddlewareTests()
_serviceHandler = new ServiceHandler();
_counter = 0;
_steps = new Steps();
_configurationPath = "ocelot.json";
}

[Fact]
Expand Down Expand Up @@ -70,7 +68,7 @@ public void should_call_pre_query_string_builder_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -115,7 +113,7 @@ public void should_call_authorization_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -160,7 +158,7 @@ public void should_call_authentication_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -205,7 +203,7 @@ public void should_call_pre_error_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -250,7 +248,7 @@ public void should_call_pre_authorization_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -295,7 +293,7 @@ public void should_call_pre_http_authentication_middleware()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunning(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
Expand Down Expand Up @@ -344,7 +342,7 @@ public void should_fix_issue_237()
};

this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "/test"))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration))
.And(x => _steps.GivenOcelotIsRunningWithMiddleareBeforePipeline<FakeMiddleware>(callback))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
Expand Down
42 changes: 13 additions & 29 deletions test/Ocelot.AcceptanceTests/RandomPortFinder.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace Ocelot.AcceptanceTests
{
public static class RandomPortFinder
{
private const int TrialNumber = 100;
private const int BeginPortRange = 20000;
private const int EndPortRange = 45000;
private static readonly Random Random = new();
private static int _currentPort = 20000;
private static readonly object LockObj = new();
private static readonly ConcurrentBag<int> UsedPorts = new();

public static int GetRandomPort()
{
for (var i = 0; i < TrialNumber; i++)
lock (LockObj)
{
var randomPort = Random.Next(BeginPortRange, EndPortRange);

if (!PortInUse(randomPort))
if (_currentPort > EndPortRange)
{
try
{
return UsePort(randomPort);
}
catch (Exception)
{
// ignored
}
throw new Exception("Cannot find available port to bind to.");
}
}

throw new Exception("Cannot find available port to bind to.");
var port = UsePort(_currentPort);
_currentPort += 1;
return port;
}
}

private static int UsePort(int randomPort)
Expand All @@ -42,17 +33,10 @@ private static int UsePort(int randomPort)

var ipe = new IPEndPoint(IPAddress.Loopback, randomPort);

using (var socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(ipe);
socket.Close();
return randomPort;
}
}

private static bool PortInUse(int randomPort)
{
return UsedPorts.Any(p => p == randomPort);
using var socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipe);
socket.Close();
return randomPort;
}
}
}

0 comments on commit fdad15d

Please sign in to comment.