Last active
September 19, 2018 11:23
-
-
Save maldworth/6871757c3d4ebc73544dc319ad7f7638 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using Decorators; | |
using MessageObservers; | |
using System; | |
using System.Collections.Generic; | |
public class ConsumersTestHarness | |
{ | |
readonly BusTestHarness _testHarness; | |
readonly IList<Action<IReceiveEndpointConfigurator>> _consumerConfigurations; | |
public ConsumersTestHarness(BusTestHarness testHarness, string queueName) | |
{ | |
_testHarness = testHarness; | |
_consumerConfigurations = new List<Action<IReceiveEndpointConfigurator>>(); | |
if (string.IsNullOrWhiteSpace(queueName)) | |
testHarness.OnConfigureReceiveEndpoint += ConfigureReceiveEndpoint; | |
else | |
testHarness.OnConfigureBus += configurator => ConfigureNamedReceiveEndpoint(configurator, queueName); | |
} | |
public ReceivedMessageList Add<TConsumer>(IConsumerFactory<TConsumer> consumerFactory) | |
where TConsumer : class, IConsumer | |
{ | |
var consumed = new ReceivedMessageList(_testHarness.TestTimeout); | |
var decorator = new TestConsumerFactoryDecorator<TConsumer>(consumerFactory, consumed); | |
_consumerConfigurations.Add(cfg => cfg.Consumer(decorator)); | |
return consumed; | |
} | |
protected virtual void ConfigureReceiveEndpoint(IReceiveEndpointConfigurator configurator) | |
{ | |
foreach (var cfg in _consumerConfigurations) | |
{ | |
cfg(configurator); | |
} | |
} | |
protected virtual void ConfigureNamedReceiveEndpoint(IBusFactoryConfigurator configurator, string queueName) | |
{ | |
configurator.ReceiveEndpoint(queueName, x => | |
{ | |
foreach (var cfg in _consumerConfigurations) | |
{ | |
cfg(x); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment