public void LifetimeManagerReturnsValueThatWasSetOnSameThread()
        {
            LifetimeManager ltm = new PerThreadLifetimeManager();
            string expected = "Here's the value";

            ltm.SetValue(expected);
            object result = ltm.GetValue();
            Assert.AreSame(expected, result);
        }
        public void DifferentLifetimeContainerInstancesHoldDifferentObjects()
        {
            LifetimeManager ltm1 = new PerThreadLifetimeManager();
            LifetimeManager ltm2 = new PerThreadLifetimeManager();
            string expected1 = "Here's the first value";
            string expected2 = "Here's the second value";

            ltm1.SetValue(expected1);
            ltm2.SetValue(expected2);

            object result1 = ltm1.GetValue();
            object result2 = ltm2.GetValue();
            Assert.AreSame(expected1, result1);
            Assert.AreSame(expected2, result2);
        }
        public void LifetimeManagerReturnsNullIfCalledOnADifferentThreadFromTheOneThatSetTheValue()
        {
            LifetimeManager ltm = new PerThreadLifetimeManager();
            string expected = "Here's the value";

            ltm.SetValue(expected);

            // Providing dummy initializers so we can prove the values are different coming out of the LTM
            var otherThreadResult = new object();

            RunInParallel(() => { otherThreadResult = ltm.GetValue(); });

            Assert.AreSame(expected, ltm.GetValue());
            Assert.IsNull(otherThreadResult);
        }
        public void WorksWithConstructorAndPropertyInjection()
        {
            // --- Arrange
            var ltManager = new PerThreadLifetimeManager
                {
                    ServiceObjectType = typeof (SampleObject),
                    ConstructionParameters = new object[] {12},
                    Properties = new PropertySettingsCollection(new List<PropertySettings>
                        {
                            new PropertySettings("Property1", "45"),
                            new PropertySettings("Property2", "hello")
                        })
                };

            // --- Act
            SampleObject instance1 = null,
                instance2 = null,
                instance3 = null,
                instance4 = null;
            // ReSharper disable ImplicitlyCapturedClosure
            var thread1 = new Thread(() =>
            {
                instance1 = ltManager.GetObject() as SampleObject;
                instance2 = ltManager.GetObject() as SampleObject;
            });
            var thread2 = new Thread(() =>
            {
                instance3 = ltManager.GetObject() as SampleObject;
                instance4 = ltManager.GetObject() as SampleObject;
            });
            // ReSharper restore ImplicitlyCapturedClosure
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            // --- Assert
            instance1.ShouldNotBeNull();
            instance1.ShouldBeSameAs(instance2);
            instance3.ShouldNotBeNull();
            instance3.ShouldBeSameAs(instance4);
            instance1.ShouldNotBeSameAs(instance3);
            instance1.Property1.ShouldEqual(45);
            instance1.Property2.ShouldEqual("hello");
            instance3.Property1.ShouldEqual(45);
            instance3.Property2.ShouldEqual("hello");
        }
        public void WorksWithConstructorParams1()
        {
            // --- Arrange
            var ltManager = new PerThreadLifetimeManager
            {
                ServiceObjectType = typeof(SampleObject),
                ConstructionParameters = new object[] { 23, "hey" }
            };

            // --- Act
            SampleObject instance1 = null,
                instance2 = null,
                instance3 = null,
                instance4 = null;
            // ReSharper disable ImplicitlyCapturedClosure
            var thread1 = new Thread(() =>
            {
                instance1 = ltManager.GetObject() as SampleObject;
                instance2 = ltManager.GetObject() as SampleObject;
            });
            var thread2 = new Thread(() =>
            {
                instance3 = ltManager.GetObject() as SampleObject;
                instance4 = ltManager.GetObject() as SampleObject;
            });
            // ReSharper restore ImplicitlyCapturedClosure
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            // --- Assert
            instance1.ShouldNotBeNull();
            instance1.ShouldBeSameAs(instance2);
            instance3.ShouldNotBeNull();
            instance3.ShouldBeSameAs(instance4);
            instance1.ShouldNotBeSameAs(instance3);
            instance1.Property1.ShouldEqual(23);
            instance1.Property2.ShouldEqual("hey");
            instance3.Property1.ShouldEqual(23);
            instance3.Property2.ShouldEqual("hey");
        }
        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);
        }
 public void NewLifetimeManagerReturnsNullForObject()
 {
     LifetimeManager ltm = new PerThreadLifetimeManager();
     Assert.IsNull(ltm.GetValue());
 }
        public void WorksWithTypeOnly()
        {
            // --- Arrange
            var ltManager = new PerThreadLifetimeManager
                {
                    ServiceObjectType = typeof(SampleObject)
                };

            // --- Act
            SampleObject instance1 = null,
                instance2 = null,
                instance3 = null,
                instance4 = null;

            object threadInstance1 = null;
            object threadInstance2 = null;

            // ReSharper disable ImplicitlyCapturedClosure
            var thread1 = new Thread(() =>
                {
                    instance1 = ltManager.GetObject() as SampleObject;
                    instance2 = ltManager.GetObject() as SampleObject;
                    threadInstance1 = ltManager.Instance;
                });
            var thread2 = new Thread(() =>
                {
                    instance3 = ltManager.GetObject() as SampleObject;
                    instance4 = ltManager.GetObject() as SampleObject;
                    threadInstance2 = ltManager.Instance;
                });
            // ReSharper restore ImplicitlyCapturedClosure
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            // --- Assert
            instance1.ShouldNotBeNull();
            instance1.ShouldBeSameAs(instance2);
            instance3.ShouldNotBeNull();
            instance3.ShouldBeSameAs(instance4);
            instance1.ShouldNotBeSameAs(instance3);
            threadInstance1.ShouldNotBeSameAs(threadInstance2);
            instance1.Property1.ShouldEqual(SampleObject.DEFAULT_INT);
            instance1.Property2.ShouldEqual(SampleObject.DEFAULT_STRING);
            instance3.Property1.ShouldEqual(SampleObject.DEFAULT_INT);
            instance3.Property2.ShouldEqual(SampleObject.DEFAULT_STRING);
        }