public void RegisteringAnInstanceInAThreadSetsPerThreadLifetimeManagerWhenResolvingInOtherThreads() { IUnityContainer container = new UnityContainer() .RegisterType <object>(new PerThreadLifetimeManager()); LifetimeManager manager = new PerThreadLifetimeManager(); object registered = new object(); object result1A = null; object result1B = null; object result2A = null; object result2B = null; container.RegisterInstance(registered, manager); TestSupport.Barrier barrier = new TestSupport.Barrier(2); RunInParallel( delegate { result1A = container.Resolve <object>(); barrier.Await(); result1B = container.Resolve <object>(); }, delegate { result2A = container.Resolve <object>(); barrier.Await(); result2B = container.Resolve <object>(); }); object result = container.Resolve <object>(); Assert.IsNotNull(result1A); Assert.IsNotNull(result2A); Assert.IsNotNull(result); Assert.AreNotSame(result1A, result2A); Assert.AreNotSame(registered, result1A); Assert.AreNotSame(registered, result2A); Assert.AreSame(result1A, result1B); Assert.AreSame(result2A, result2B); Assert.AreSame(registered, result); }
public void LifetimeManagerReturnsDifferentValuesForEachThread() { LifetimeManager ltm = new PerThreadLifetimeManager(); string one = "one"; string two = "two"; string three = "three"; object valueOne = null; object valueTwo = null; object ValueThree = null; TestSupport.Barrier barrier = new TestSupport.Barrier(3); RunInParallel( delegate { ltm.SetValue(one); barrier.Await(); valueOne = ltm.GetValue(); }, delegate { ltm.SetValue(three); barrier.Await(); ValueThree = ltm.GetValue(); }, delegate { ltm.SetValue(two); barrier.Await(); valueTwo = ltm.GetValue(); }); Assert.AreSame(one, valueOne); Assert.AreSame(two, valueTwo); Assert.AreSame(three, ValueThree); }
public void RegisteringAnInstanceInAThreadSetsPerThreadLifetimeManagerWhenResolvingInOtherThreads() { IUnityContainer container = new UnityContainer() .RegisterType<object>(new PerThreadLifetimeManager()); LifetimeManager manager = new PerThreadLifetimeManager(); object registered = new object(); object result1A = null; object result1B = null; object result2A = null; object result2B = null; container.RegisterInstance(registered, manager); TestSupport.Barrier barrier = new TestSupport.Barrier(2); RunInParallel( delegate { result1A = container.Resolve<object>(); barrier.Await(); result1B = container.Resolve<object>(); }, delegate { result2A = container.Resolve<object>(); barrier.Await(); result2B = container.Resolve<object>(); }); object result = container.Resolve<object>(); Assert.IsNotNull(result1A); Assert.IsNotNull(result2A); Assert.IsNotNull(result); Assert.AreNotSame(result1A, result2A); Assert.AreNotSame(registered, result1A); Assert.AreNotSame(registered, result2A); Assert.AreSame(result1A, result1B); Assert.AreSame(result2A, result2B); Assert.AreSame(registered, result); }