-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from lanegoolsby/chaos
(feat) Add Chaos Engineering
- Loading branch information
Showing
16 changed files
with
322 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Mockaco.AspNetCore/Chaos/Strategies/ChaosStrategyBehavior.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,17 @@ | ||
using System.Net; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal class ChaosStrategyBehavior : IChaosStrategy | ||
{ | ||
public Task Response(HttpResponse httpResponse) | ||
{ | ||
httpResponse.StatusCode = (int)HttpStatusCode.ServiceUnavailable; | ||
|
||
var bodyBytes = Encoding.UTF8.GetBytes($"Error {httpResponse.StatusCode}: {HttpStatusCode.ServiceUnavailable}"); | ||
|
||
return httpResponse.Body.WriteAsync(bodyBytes, 0, bodyBytes.Length, default); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Mockaco.AspNetCore/Chaos/Strategies/ChaosStrategyException.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,17 @@ | ||
using System.Net; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal class ChaosStrategyException : IChaosStrategy | ||
{ | ||
public Task Response(HttpResponse httpResponse) | ||
{ | ||
httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError; | ||
|
||
var bodyBytes = Encoding.UTF8.GetBytes($"Error {httpResponse.StatusCode}: {HttpStatusCode.InternalServerError}"); | ||
|
||
return httpResponse.Body.WriteAsync(bodyBytes, 0, bodyBytes.Length); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Mockaco.AspNetCore/Chaos/Strategies/ChaosStrategyLatency.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,23 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal class ChaosStrategyLatency : IChaosStrategy | ||
{ | ||
private readonly ILogger<ChaosStrategyLatency> _logger; | ||
private readonly IOptions<ChaosOptions> _options; | ||
|
||
public ChaosStrategyLatency(ILogger<ChaosStrategyLatency> logger, IOptions<ChaosOptions> options) | ||
{ | ||
_logger = logger; | ||
_options = options; | ||
} | ||
public Task Response(HttpResponse httpResponse) | ||
{ | ||
var responseDelay = new Random().Next(_options.Value.MinimumLatencyTime, _options.Value.MaximumLatencyTime); | ||
_logger.LogInformation($"Chaos Latency (ms): {responseDelay}"); | ||
return Task.Delay(responseDelay); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Mockaco.AspNetCore/Chaos/Strategies/ChaosStrategyResult.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,17 @@ | ||
using System.Net; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal class ChaosStrategyResult : IChaosStrategy | ||
{ | ||
public Task Response(HttpResponse httpResponse) | ||
{ | ||
httpResponse.StatusCode = (int)HttpStatusCode.BadRequest; | ||
|
||
var bodyBytes = Encoding.UTF8.GetBytes($"Error {httpResponse.StatusCode}: {HttpStatusCode.BadRequest}"); | ||
|
||
return httpResponse.Body.WriteAsync(bodyBytes, 0, bodyBytes.Length); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Mockaco.AspNetCore/Chaos/Strategies/ChaosStrategyTimeout.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,27 @@ | ||
using System.Net; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal class ChaosStrategyTimeout : IChaosStrategy | ||
{ | ||
private readonly IOptions<ChaosOptions> _options; | ||
|
||
public ChaosStrategyTimeout(IOptions<ChaosOptions> options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public async Task Response(HttpResponse httpResponse) | ||
{ | ||
await Task.Delay(_options.Value.TimeBeforeTimeout); | ||
|
||
httpResponse.StatusCode = (int)HttpStatusCode.RequestTimeout; | ||
|
||
var bodyBytes = Encoding.UTF8.GetBytes($"Error {httpResponse.StatusCode}: {HttpStatusCode.RequestTimeout}"); | ||
|
||
await httpResponse.Body.WriteAsync(bodyBytes, 0, bodyBytes.Length, default); | ||
} | ||
} |
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,8 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Mockaco.Chaos.Strategies; | ||
|
||
internal interface IChaosStrategy | ||
{ | ||
Task Response(HttpResponse httpResponse); | ||
} |
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
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
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
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,73 @@ | ||
using System.Net; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Mockaco.Chaos.Strategies; | ||
|
||
namespace Mockaco; | ||
|
||
internal class ChaosMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IEnumerable<IChaosStrategy> _strategies; | ||
private readonly ILogger<ChaosMiddleware> _logger; | ||
private readonly IOptions<ChaosOptions> _options; | ||
|
||
private List<int> ErrorList { get; set; } | ||
private int Counter { get; set; } | ||
|
||
public ChaosMiddleware( | ||
RequestDelegate next, | ||
IEnumerable<IChaosStrategy> strategies, | ||
ILogger<ChaosMiddleware> logger, | ||
IOptions<ChaosOptions> options) | ||
{ | ||
_next = next; | ||
_strategies = strategies; | ||
_logger = logger; | ||
_options = options; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
if (!_options.Value.Enabled) | ||
{ | ||
return; | ||
} | ||
|
||
Counter++; | ||
if (Counter > 100) | ||
Counter = 1; | ||
|
||
if (Counter == 1) | ||
ErrorList = GenerateErrorList(_options.Value.ChaosRate); | ||
|
||
if (ErrorList.Contains(Counter)) | ||
{ | ||
var selected = _strategies.Random(); | ||
_logger.LogInformation($"Chaos: {selected?.ToString()}"); | ||
if (selected != null) await selected.Response(httpContext.Response); | ||
} | ||
|
||
if (httpContext.Response.StatusCode != (int)HttpStatusCode.OK) | ||
return; | ||
|
||
|
||
await _next(httpContext); | ||
} | ||
|
||
private List<int> GenerateErrorList(int rate) | ||
{ | ||
var list = new List<int>(); | ||
while (list.Count < rate) | ||
{ | ||
var item = new Random().Next(1, 100); | ||
if (!list.Contains(item)) | ||
{ | ||
list.Add(item); | ||
} | ||
} | ||
|
||
return list.OrderBy(x => x).ToList(); | ||
} | ||
} |
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,19 @@ | ||
namespace Mockaco; | ||
|
||
public class ChaosOptions | ||
{ | ||
public bool Enabled { get; set; } | ||
public int ChaosRate { get; set; } | ||
public int MinimumLatencyTime { get; set; } | ||
public int MaximumLatencyTime { get; set; } | ||
public int TimeBeforeTimeout { get; set; } | ||
|
||
public ChaosOptions() | ||
{ | ||
Enabled = true; | ||
ChaosRate = 10; | ||
MinimumLatencyTime = 500; | ||
MaximumLatencyTime = 3000; | ||
TimeBeforeTimeout = 10000; | ||
} | ||
} |
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
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
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
22 changes: 22 additions & 0 deletions
22
test/Mockaco.AspNetCore.Tests/Extensions/EnumerableExtensionTests.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,22 @@ | ||
namespace Mockaco.Tests.Extensions; | ||
|
||
public class EnumerableExtensionTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(Data))] | ||
public void Select_Random_IEnumerables(List<object> source) | ||
{ | ||
IEnumerable<object> enummerables = source; | ||
|
||
object selected = enummerables.Random(); | ||
|
||
Assert.Contains(selected, source); | ||
} | ||
|
||
public static IEnumerable<object[]> Data() | ||
{ | ||
yield return new object[] { new List<object> { 1, 2, 3 } }; | ||
yield return new object[] { new List<object> { "a", "b", "c" } }; | ||
yield return new object[] { new List<object> { "a1", "c2" } }; | ||
} | ||
} |
Oops, something went wrong.