Skip to content

Instantly share code, notes, and snippets.

@ruslander
Created August 7, 2014 20:17
Show Gist options
  • Save ruslander/dc5b9fe265b2f7da6530 to your computer and use it in GitHub Desktop.
Save ruslander/dc5b9fe265b2f7da6530 to your computer and use it in GitHub Desktop.
nsb.acceptance.testing.cs
[TestFixture]
public abstract class Given_a_clean_environment
{
[SetUp]
public void Setup()
{
Init();
Given();
When();
}
protected abstract void Given();
protected abstract void When();
protected IBus publiser;
private void Init()
{
foreach (MessageQueue queue in MessageQueue.GetPrivateQueuesByMachine(Environment.MachineName))
{
queue.Purge();
}
publiser = Config.publiser;
}
}
// test
[SetUpFixture]
public class Config
{
public static NServiceBusHostRunner HostRunner;
public static IBus publiser;
[SetUp]
public void SetUp()
{
// the tupe of impersonated endpoint
HostRunner = new NServiceBusHostRunner();
HostRunner.Start("company.product.service.endpoint");
}
[TearDown]
public void TearDown()
{
HostRunner.Dispose();
}
}
// sender impersonation
sender = Configure.With()
.Log4Net()
.DefaultBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(true)
.UnicastBus()
.LoadMessageHandlers()
.InMemoryFaultManagement()
.UseInMemoryTimeoutPersister()
.CreateBus()
.Start(() => Configure.Instance.ForInstallationOn<Windows>().Install());
//publisher
publiser = Configure
.With()
.DefineEndpointName("copmany.product.service.endpoint")
.DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"))
.DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"))
.DefiningMessagesAs(t => t.Namespace == "Messages")
.Log4Net()
.DefaultBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(true)
.UnicastBus()
.LoadMessageHandlers()
.InMemoryFaultManagement()
.UseInMemoryTimeoutPersister()
.MsmqSubscriptionStorage()
.CreateBus()
.Start(() => Configure.Instance.ForInstallationOn<Windows>().Install());
// runner
public class NServiceBusHostRunner : IDisposable
{
private Task _nonBlocking;
private readonly List<AppDomain> _services = new List<AppDomain>();
public static readonly string[] ServiceDirs;
private bool _disposed;
static NServiceBusHostRunner()
{
var sourceRootDir = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..", "..", ".."));
ServiceDirs = (from d in Directory.GetDirectories(sourceRootDir)
let bin = Path.Combine(d, "bin", "Debug")
where File.Exists(GetServiceConfig(bin))
where File.Exists(Path.Combine(bin, "NServiceBus.Host.exe"))
select bin).ToArray();
}
public NServiceBusHostRunner()
{
}
public void Start(string endpointName)
{
_nonBlocking = new Task(() =>
{
var tasks = new List<Task>();
foreach (var serviceDir in ServiceDirs)
{
string applicationName = GetServiceName(serviceDir);
if (applicationName.Equals(endpointName) == false)
continue;
var domainInfo = new AppDomainSetup
{
ConfigurationFile = GetServiceConfig(serviceDir),
ApplicationBase = serviceDir,
PrivateBinPath = serviceDir,
ShadowCopyFiles = "true",
ApplicationName = applicationName,
};
var appDomain = AppDomain.CreateDomain(applicationName ?? String.Empty, null, domainInfo);
tasks.Add(Task.Factory.StartNew(() =>
{
_services.Add(appDomain);
try
{
appDomain.ExecuteAssemblyByName("NServiceBus.Host");
}
catch (AppDomainUnloadedException)
{
}
}, TaskCreationOptions.LongRunning));
}
Task.WaitAll(tasks.ToArray());
});
_nonBlocking.Start();
const int nSecons = 15;
Thread.Sleep(TimeSpan.FromSeconds(nSecons));
}
private static string GetServiceConfig(string servicePath)
{
var dirName = GetServiceName(servicePath);
if (dirName == null)
throw new InvalidOperationException();
return Path.Combine(servicePath, dirName + ".dll.config");
}
private static string GetServiceName(string servicePath)
{
return Path.GetFileName(Path.GetFullPath(Path.Combine(servicePath, "..", "..")));
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
Task.WaitAll(_services.Select(domain => Task.Factory.StartNew(() => AppDomain.Unload(domain))).ToArray());
}
_services.Clear();
_disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment