-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add filter #507
Closed
Closed
add filter #507
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/ProjectOrigin.Vault/Extensions/RandomDelayConfigurationExtensions.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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MassTransit; | ||
using MassTransit.Configuration; | ||
|
||
namespace ProjectOrigin.Vault.Extensions; | ||
|
||
public static class RandomDelayConfigurationExtensions | ||
{ | ||
public static void UseRandomDelay<TConsumer, TMessage>(this IConsumerConfigurator<TConsumer> configurator, int minDelayInMilliseconds = 1, | ||
int maxDelayInMilliseconds = 1500) | ||
where TConsumer : class, IConsumer<TMessage> | ||
where TMessage : class | ||
{ | ||
configurator.AddPipeSpecification(new RandomDelaySpecification<TConsumer, TMessage>(minDelayInMilliseconds, maxDelayInMilliseconds)); | ||
} | ||
} | ||
|
||
public class RandomDelaySpecification<TConsumer, TMessage>(int minDelayInMilliseconds, int maxDelayInMilliseconds) | ||
: IPipeSpecification<ConsumerConsumeContext<TConsumer>> | ||
where TConsumer : class, IConsumer<TMessage> | ||
where TMessage : class | ||
{ | ||
public void Apply(IPipeBuilder<ConsumerConsumeContext<TConsumer>> builder) | ||
{ | ||
builder.AddFilter(new RandomDelayFilter<TConsumer>(minDelayInMilliseconds, maxDelayInMilliseconds)); | ||
} | ||
|
||
public IEnumerable<ValidationResult> Validate() | ||
{ | ||
if (minDelayInMilliseconds < 0) | ||
yield return this.Failure("RandomDelayFilter", "minDelayInMilliseconds cannot be negative"); | ||
|
||
if (maxDelayInMilliseconds <= minDelayInMilliseconds) | ||
yield return this.Failure("RandomDelayFilter", | ||
"maxDelayInMilliseconds must be greater than minDelayInMilliseconds"); | ||
} | ||
} | ||
|
||
public class RandomDelayFilter<TConsumer>(int minDelayInMilliseconds, int maxDelayInMilliseconds) : IFilter<ConsumerConsumeContext<TConsumer>> | ||
where TConsumer : class | ||
{ | ||
public async Task Send(ConsumerConsumeContext<TConsumer> context, | ||
IPipe<ConsumerConsumeContext<TConsumer>> next) | ||
{ | ||
var delay = Random.Shared.Next(minDelayInMilliseconds, maxDelayInMilliseconds + 1); | ||
|
||
context.GetOrAddPayload(() => new RandomDelayPayload { Delay = delay }); | ||
|
||
await Task.Delay(delay); | ||
await next.Send(context); | ||
} | ||
|
||
public void Probe(ProbeContext context) | ||
{ | ||
var scope = context.CreateFilterScope("random-delay"); | ||
scope.Add("description", $"Adds a random delay between {minDelayInMilliseconds}-{maxDelayInMilliseconds} ms to each message."); | ||
} | ||
} | ||
|
||
public class RandomDelayPayload | ||
{ | ||
public int Delay { get; set; } | ||
} |
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
84 changes: 84 additions & 0 deletions
84
test/ProjectOrigin.Vault.Tests/Extensions/RandomDelayConfigurationExtensionsTests.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,84 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MassTransit; | ||
using MassTransit.Internals; | ||
using MassTransit.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ProjectOrigin.Vault.Extensions; | ||
using Xunit; | ||
|
||
namespace ProjectOrigin.Vault.Tests.Extensions; | ||
|
||
public class RandomDelayConfigurationExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task Should_Apply_RandomDelay_To_Each_Message() | ||
{ | ||
const int minDelayInMilliseconds = 5; | ||
const int maxDelayInMilliseconds = 1000; | ||
await using var provider = new ServiceCollection() | ||
.AddMassTransitTestHarness(cfg => | ||
{ | ||
cfg.AddConsumer<TestConsumer>(consumerCfg => | ||
{ | ||
consumerCfg.UseRandomDelay<TestConsumer, TestMessage>(minDelayInMilliseconds, maxDelayInMilliseconds); | ||
}); | ||
|
||
cfg.UsingInMemory((context, inMemoryQueue) => { inMemoryQueue.ConfigureEndpoints(context); }); | ||
}) | ||
.BuildServiceProvider(true); | ||
|
||
var harness = provider.GetRequiredService<ITestHarness>(); | ||
await harness.Start(); | ||
|
||
try | ||
{ | ||
for (var i = 0; i < 5; i++) | ||
{ | ||
await harness.Bus.Publish(new TestMessage()); | ||
} | ||
|
||
Assert.True(await harness.Consumed.Any<TestMessage>(), | ||
"Expected the test harness to consume TestMessage, but none was found."); | ||
|
||
var consumerHarness = harness.GetConsumerHarness<TestConsumer>(); | ||
|
||
var consumedContexts = await consumerHarness | ||
.Consumed | ||
.SelectAsync<TestMessage>() | ||
.Take(5) | ||
.ToListAsync(); | ||
|
||
Assert.Equal(5, consumedContexts.Count); | ||
|
||
var delays = consumedContexts | ||
.Select(ctx => | ||
ctx.Context.TryGetPayload<RandomDelayPayload>(out var payload) | ||
? payload.Delay | ||
: 0) | ||
.ToList(); | ||
|
||
Assert.Equal(5, delays.Count); | ||
|
||
Assert.All(delays, d => Assert.InRange(d, minDelayInMilliseconds, maxDelayInMilliseconds)); | ||
|
||
Assert.Equal(5, delays.Distinct().Count()); | ||
} | ||
finally | ||
{ | ||
await harness.Stop(); | ||
} | ||
} | ||
} | ||
|
||
public record TestMessage; | ||
|
||
public class TestConsumer : IConsumer<TestMessage> | ||
{ | ||
public Task Consume(ConsumeContext<TestMessage> context) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
test/ProjectOrigin.Vault.Tests/Extensions/RandomDelaySpecificationTests.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,47 @@ | ||
using System.Threading.Tasks; | ||
using MassTransit; | ||
using ProjectOrigin.Vault.Extensions; | ||
using Xunit; | ||
|
||
namespace ProjectOrigin.Vault.Tests.Extensions | ||
{ | ||
public class RandomDelaySpecificationTests | ||
{ | ||
[Fact] | ||
public void Validate_ShouldReturnErrorIfMinDelayIsNegative() | ||
{ | ||
var specification = new RandomDelaySpecification<TestConsumer, TestMessage>(-1, 100); | ||
|
||
var error = Assert.Single(specification.Validate()); | ||
|
||
Assert.Equal("RandomDelayFilter", error.Key); | ||
Assert.Equal("minDelayInMilliseconds cannot be negative", error.Message); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnErrorIfMaxDelayIsLessThanOrEqualToMinDelay() | ||
{ | ||
var specification = new RandomDelaySpecification<TestConsumer, TestMessage>(5, 5); | ||
|
||
var error = Assert.Single(specification.Validate()); | ||
|
||
Assert.Equal("RandomDelayFilter", error.Key); | ||
Assert.Equal("maxDelayInMilliseconds must be greater than minDelayInMilliseconds", error.Message); | ||
} | ||
|
||
[Fact] | ||
public void Validate_ShouldReturnNoErrorsForValidRange() | ||
{ | ||
var specification = new RandomDelaySpecification<TestConsumer, TestMessage>(0, 10); | ||
|
||
Assert.Empty(specification.Validate()); | ||
} | ||
|
||
private class TestMessage; | ||
|
||
private class TestConsumer : IConsumer<TestMessage> | ||
{ | ||
public Task Consume(ConsumeContext<TestMessage> context) => Task.CompletedTask; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is indeed the solution, then why don't we simply add the await Task.Delay(randomSecondsBetweenXAndY); in the command handler?
(This task could be a two-liner :))