Last active
December 11, 2015 22:48
-
-
Save rantri/4671715 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NUnit.Framework; | |
using MassTransit; | |
using System.Threading; | |
using System.Diagnostics; | |
namespace Messaging | |
{ | |
public class ValidRequest { } | |
public class InvalidRequest { } | |
public class Replay { } | |
[TestFixture] | |
public class Masstransit_Requst_Responed_Test | |
{ | |
public IServiceBus LocalBus { get; set; } | |
public IServiceBus RemoteBus { get; set; } | |
static readonly ManualResetEvent completeEvent = new ManualResetEvent(false); | |
[SetUp] | |
public void Setup() | |
{ | |
LocalBus = CreateBus("local"); | |
RemoteBus = CreateBus("remote"); | |
completeEvent.Reset(); | |
RemoteBus.SubscribeConsumer<SomeConsumer>(); | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
LocalBus.Dispose(); | |
RemoteBus.Dispose(); | |
LocalBus = null; | |
RemoteBus = null; | |
} | |
[Test] | |
public void RequstResponed_ValidRequest_RespondHandeled() | |
{ | |
bool responedHendeled = false; | |
bool faultHendeled = false; | |
completeEvent.WaitOne(TimeSpan.FromSeconds(2), true); | |
LocalBus.PublishRequestAsync(new ValidRequest(), x => | |
{ | |
x.Handle<Replay>(r => | |
{ | |
responedHendeled = true; | |
completeEvent.Set(); | |
}); | |
x.HandleFault((c, f) => | |
{ | |
faultHendeled = true; | |
completeEvent.Set(); | |
}); | |
}); | |
completeEvent.WaitOne(TimeSpan.FromSeconds(20), true); | |
Assert.That(responedHendeled, Is.True); | |
Assert.That(faultHendeled, Is.False); | |
} | |
[Test] | |
public void RequstResponed_InvalidRequest_FaultHandeled() | |
{ | |
bool responedHendeled = false; | |
bool faultHendeled = false; | |
completeEvent.WaitOne(TimeSpan.FromSeconds(2), true); | |
LocalBus.PublishRequestAsync(new InvalidRequest(), x => | |
{ | |
x.Handle<Replay>(r => | |
{ | |
responedHendeled = true; | |
completeEvent.Set(); | |
}); | |
x.HandleFault((c, f) => | |
{ | |
faultHendeled = true; | |
completeEvent.Set(); | |
}); | |
}); | |
completeEvent.WaitOne(TimeSpan.FromSeconds(20), true); | |
Assert.That(responedHendeled, Is.False); | |
Assert.That(faultHendeled, Is.True); | |
} | |
private IServiceBus CreateBus(string name) | |
{ | |
var bus = ServiceBusFactory.New(sbc => | |
{ | |
sbc.UseMsmq(); | |
sbc.VerifyMsmqConfiguration(); | |
sbc.UseMulticastSubscriptionClient(); | |
sbc.SetCreateTransactionalQueues(false); | |
sbc.ReceiveFrom("msmq://localhost/" + name); | |
sbc.UseJsonSerializer(); | |
sbc.SetNetwork("Ran"); | |
sbc.SetPurgeOnStartup(true); | |
}); | |
return bus; | |
} | |
} | |
public class SomeConsumer : Consumes<ValidRequest>.Context, Consumes<InvalidRequest>.Context | |
{ | |
public void Consume(IConsumeContext<ValidRequest> context) | |
{ | |
Debug.WriteLine("Request Handled"); | |
context.Respond<Replay>(new Replay()); | |
} | |
public void Consume(IConsumeContext<InvalidRequest> message) | |
{ | |
Debug.WriteLine("Request Handled"); | |
throw new InvalidOperationException("Some Invalid Request!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I update the SetCreateTransactionalQueues(false);
And add a Reset to the event (Must be done when running the tests together)