forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new middleware, SimulatedLatencyMiddleware, has been added to simulate latency in a designated range. This can be used for testing purposes by delaying a request for a random amount of time. Additionally, an extension method for IApplicationBuilder has been implemented to conveniently add this functionality to the application's pipeline.
- Loading branch information
1 parent
26518a3
commit 662a076
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/bundles/Elsa.Server.Web/Middleware/SimulatedLatencyMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace Elsa.Server.Web.Middleware; | ||
|
||
/// <summary> | ||
/// Simulates latency by delaying the request for a random amount of time between the specified minimum and maximum durations. | ||
/// </summary> | ||
/// <param name="next"></param> | ||
/// <param name="min"></param> | ||
/// <param name="max"></param> | ||
[UsedImplicitly] | ||
public class SimulatedLatencyMiddleware( | ||
RequestDelegate next, | ||
TimeSpan min, | ||
TimeSpan max) | ||
{ | ||
private readonly int _minDelayInMs = (int)min.TotalMilliseconds; | ||
private readonly int _maxDelayInMs = (int)max.TotalMilliseconds; | ||
private readonly ThreadLocal<Random> _random = new(() => new Random()); | ||
|
||
/// <summary> | ||
/// Invokes the middleware. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
[UsedImplicitly] | ||
public async Task Invoke(HttpContext context) | ||
{ | ||
int delayInMs = _random.Value!.Next( | ||
_minDelayInMs, | ||
_maxDelayInMs | ||
); | ||
|
||
await Task.Delay(delayInMs); | ||
await next(context); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Provides extension methods for the <see cref="IApplicationBuilder"/> interface. | ||
/// </summary> | ||
public static class AppBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the <see cref="SimulatedLatencyMiddleware"/> to the application's request pipeline. | ||
/// </summary> | ||
/// <param name="app"></param> | ||
/// <param name="min"></param> | ||
/// <param name="max"></param> | ||
public static IApplicationBuilder UseSimulatedLatency( | ||
this IApplicationBuilder app, | ||
TimeSpan min, | ||
TimeSpan max) | ||
{ | ||
return app.UseMiddleware<SimulatedLatencyMiddleware>(min, max); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters