Skip to content

Instantly share code, notes, and snippets.

@logicbomb
Created May 15, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logicbomb/2701817 to your computer and use it in GitHub Desktop.
Save logicbomb/2701817 to your computer and use it in GitHub Desktop.
Azure service bus integration test
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;
using MassTransit;
using MassTransit.Services.Routing.Configuration;
using MassTransit.Transports.AzureServiceBus.Configuration;
using NUnit.Framework;
namespace Integration.Tests.MassTransit
{
[TestFixture]
public class AzureServiceBusTests
{
private IServiceBus _publisher;
private IServiceBus _host;
private IList<TestMessage> _messages;
[SetUp]
public void SetUp()
{
var hostReceiveFrom = getEndpointUrl("host");
var publisherReceiveFrom = getEndpointUrl("publisher");
configurePublisher(publisherReceiveFrom, hostReceiveFrom);
configureHost(hostReceiveFrom);
_messages = new List<TestMessage>();
}
[Test]
public void Message_should_be_sent_once()
{
_messages.Clear();
var msg = new TestMessage();
_publisher.Publish(msg);
Thread.Sleep(1500);
Assert.AreEqual(1, _messages.Count);
}
private void configureHost(Uri hostUrl)
{
_host= ServiceBusFactory.New(config =>
{
config.ReceiveFrom(hostUrl);
config.UseAzureServiceBus();
config.UseAzureServiceBusRouting();
config.Subscribe(
s => s.Handler<TestMessage>(msg => _messages.Add(msg))
);
});
var bus = new RoutingConfigurator().Create(_host);
bus.Start(_host);
}
private void configurePublisher(Uri receiveFrom, Uri hostUrl)
{
var configurator = new RoutingConfigurator();
configurator.Route<TestMessage>().To(hostUrl);
_publisher = ServiceBusFactory.New(config =>
{
config.ReceiveFrom(receiveFrom);
config.UseAzureServiceBus();
config.UseAzureServiceBusRouting();
});
var bus = configurator.Create(_publisher);
bus.Start(_publisher);
}
private static Uri getEndpointUrl(string applicationName)
{
var owner = ConfigurationManager.AppSettings["asb.Owner"];
var key = ConfigurationManager.AppSettings["asb.Key"];
var nmSpace = ConfigurationManager.AppSettings["asb.Namespace"];
return new Uri(string.Concat("azure-sb://", owner, ":", key, "@", nmSpace, "/", applicationName));
}
}
public class TestMessage
{
public TestMessage()
{
Identifier = Guid.NewGuid();
}
public Guid Identifier { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment