Skip to content

Commit b14525d

Browse files
committed
25.0.0-beta.6 TimeoutSteps
1 parent 12be92d commit b14525d

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/AcceptanceSteps.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public AcceptanceSteps()
6262
protected static string DownstreamUrl(int port, string scheme) => $"{scheme ?? Uri.UriSchemeHttp}://localhost:{port}";
6363
protected static string LoopbackLocalhostUrl(int port, int loopbackIndex = 0) => $"{Uri.UriSchemeHttp}://127.0.0.{++loopbackIndex}:{port}";
6464

65+
protected virtual bool IsCiCd() => IsRunningInGitHubActions();
66+
protected static bool IsRunningInGitHubActions()
67+
=> Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
68+
6569
public virtual FileConfiguration GivenConfiguration(params FileRoute[] routes)
6670
{
6771
var c = new FileConfiguration();

src/Ocelot.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<IncludeSymbols>True</IncludeSymbols>
99
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1010
<!--Package properties-->
11-
<Version>25.0.0-beta.5</Version>
11+
<Version>25.0.0-beta.6</Version>
1212
<!-- <VersionPrefix>0.0.0-dev</VersionPrefix> -->
1313
<PackageId>Ocelot.Testing</PackageId>
1414
<PackageDescription>A shared library for testing the Ocelot core library and its extension packages, including both acceptance and unit tests</PackageDescription>

src/TimeoutSteps.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Ocelot.Configuration.File;
3+
using Shouldly;
4+
using System.Diagnostics;
5+
using System.Net;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Ocelot.Testing;
9+
10+
public class TimeoutSteps : AcceptanceSteps
11+
{
12+
public static int Ms(int seconds) => 1000 * seconds;
13+
14+
public FileConfiguration GivenConfiguration(int port, int? routeTimeout = null, int? globalTimeout = null)
15+
{
16+
var route = GivenDefaultRoute(port);
17+
route.Timeout = routeTimeout;
18+
var configuration = GivenConfiguration(route);
19+
configuration.GlobalConfiguration.Timeout = globalTimeout;
20+
return configuration;
21+
}
22+
23+
public virtual void GivenThereIsAServiceRunningOn(int port, HttpStatusCode statusCode, int timeout,
24+
[CallerMemberName] string response = nameof(TimeoutSteps))
25+
{
26+
async Task MapBodyWithTimeout(HttpContext context)
27+
{
28+
await Task.Delay(timeout);
29+
context.Response.StatusCode = (int)statusCode;
30+
await context.Response.WriteAsync(response);
31+
}
32+
handler.GivenThereIsAServiceRunningOn(port, MapBodyWithTimeout);
33+
}
34+
35+
public async Task<Stopwatch> WatchWhenIGetUrlOnTheApiGateway(string? upstream = null)
36+
{
37+
var watcher = Stopwatch.StartNew();
38+
await WhenIGetUrlOnTheApiGateway(upstream ?? "/");
39+
watcher.Stop();
40+
return watcher;
41+
}
42+
43+
public static void ThenTimeoutIsInRange(Stopwatch watcher, int lowDurationMs, int highDurationMs)
44+
{
45+
var expectedLowDuration = TimeSpan.FromMilliseconds(lowDurationMs);
46+
var expectedHighDuration = TimeSpan.FromMilliseconds(highDurationMs);
47+
watcher.Elapsed.ShouldBeGreaterThan(expectedLowDuration);
48+
watcher.Elapsed.ShouldBeLessThan(expectedHighDuration);
49+
}
50+
}

0 commit comments

Comments
 (0)