Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2010 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/400980 to your computer and use it in GitHub Desktop.
Save anonymous/400980 to your computer and use it in GitHub Desktop.
Tests for contextual lifestyle in Windsor
namespace Castle.Windsor.Tests
{
using System;
using System.Diagnostics;
using Autofac;
using Castle.MicroKernel.Registration;
using NUnit.Framework;
[TestFixture]
public class Gdsghdshsdhs
{
[SetUp]
public void SetUp()
{
root = new WindsorContainer();
root.Kernel.AddSubSystem("scope", new ScopeSubsystem());
}
private IWindsorContainer root;
[Test]
public void PerfTest()
{
var stopwatch = Stopwatch.StartNew();
const int count = 100000;
for (var i = 0; i < count; i++)
{
using (root.BeginScope())
{
}
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds); // 33 in release mode on my machine
}
[Test]
public void PerfTestAutofac()
{
var stopwatch = Stopwatch.StartNew();
var container = new ContainerBuilder().Build();
const int count = 100000;
for (var i = 0; i < count; i++)
{
using (container.BeginLifetimeScope())
{
}
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds); //105 in release mode on my machine
}
[Test]
public void ResolutionsByContext()
{
root.Register(Component.For<Model1>().LifeStyle.Transient,
Component.For<Model2>().LifeStyle.Transient,
Component.For<IRepository>().ImplementedBy<Repository1>().LifeStyle.Transient,
Component.For<IRepository>().ImplementedBy<Repository2>().LifeStyle.Transient,
Component.For<ISession>().ImplementedBy<Session>().LifeStyle.Scoped());
Model1 model1;
Model2 model2;
ISession session1, session2;
using (root.BeginScope())
{
model1 = root.Resolve<Model1>();
session1 = model1.First.Session;
Assert.AreSame(model1.First.Session, model1.Second.Session);
Assert.AreSame(root.Resolve<ISession>(), root.Resolve<ISession>());
using (var context2 = root.BeginScope())
{
model2 = root.Resolve<Model2>();
session2 = model2.Second.Session;
Assert.AreNotSame(model1.First.Session, model2.Second.Session);
var anotherModel2 = root.Resolve<Model2>();
Assert.AreSame(anotherModel2.Second.Session, model2.Second.Session);
Assert.AreSame(session2, root.Resolve<ISession>());
Assert.AreNotSame(session1, session2);
}
Assert.IsTrue(session2.IsDisposed);
Assert.IsFalse(session1.IsDisposed);
}
Assert.IsTrue(session1.IsDisposed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment