Skip to content

Instantly share code, notes, and snippets.

@chribben
Created February 1, 2012 21:19
Show Gist options
  • Save chribben/1719535 to your computer and use it in GitHub Desktop.
Save chribben/1719535 to your computer and use it in GitHub Desktop.
Events that aren't published
//Test
[TestFixture]
public class TestSendMessageReceiveEvent
{
private IWindsorContainer _container;
private readonly Guid _aggregateId = Guid.NewGuid();
private static readonly ILog _Logger = LogManager.GetLogger(typeof (TestSendMessageReceiveEvent));
private readonly ManualResetEvent _received = new ManualResetEvent(false);
public TestSendMessageReceiveEvent()
{
BasicConfigurator.Configure();
}
public IServiceBus LocalBus { get; private set; }
[Given]
public void installation_of_infrastructure_objects()
{
_Logger.Info("Installing stuff");
_container = new WindsorContainer().Register(Component.For<ManualResetEvent>().Instance(_received));
_container.Register(Component.For<MyEventConsumer>().LifeStyle.Singleton);
_container.Register(Component.For<IServiceBus>().UsingFactoryMethod(() => ServiceBusFactory.New(x => {x.ReceiveFrom("loopback://localhost/mt_client"); x.Subscribe(conf => conf.LoadFrom(_container)); })));
LocalBus = _container.Resolve<IServiceBus>();
}
[When]
public void sending_a_command()
{
LocalBus.Publish(new DoSomething(_aggregateId));
}
[Then]
public void corresponding_event_should_be_received_by_consumer()
{
Assert.AreEqual(LocalBus.HasSubscription<SomethingDone>().Count(), 1, "No subscription for the SomethingDone was found.");
_received.WaitOne(5000).ShouldBeTrue();
}
//Event handler
public class MyEventConsumer : Consumes<SomethingDone>.All
{
private readonly ManualResetEvent _received;
public MyEventConsumer(ManualResetEvent received)
{
_received = received;
}
public void Consume(SomethingDone message)
{
_received.Set();
}
}
//Command handler
public class DoSomethingCommandHandler : Consumes<DoSomething>.All
{
public void Consume(DoSomething message)
{
var ar = Repository.GetById<SomeAR>(message.ArId);
ar.DoSomething();
Repository.Save(ar, Guid.NewGuid(), null);
}
}
//Domain object
public class SomeDomainObject : AggregateBase
{
public void DoSomething()
{
RaiseEvent(new SomethingDone(Id, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment