Example #1
0
        public void ShouldReturnTheSameImplementationInstanceWithEachResolutionRequest()
        {
            var container               = new Container();
            var lifecycleManager        = new SingletonLifecycleManager();
            var registration            = new Registration(typeof(IAmATestInterface), typeof(ImplementationTest), lifecycleManager);
            var firstInstanceReference  = lifecycleManager.GetImplementationInstance(container, registration);
            var secondInstanceReference = lifecycleManager.GetImplementationInstance(container, registration);

            Assert.Same(firstInstanceReference, secondInstanceReference);
        }
            public void ReturnsTheSameInstanceEvenWhenCalledMultipleTimes()
            {
                // arrange
                Func <A> factory = () => new A();
                var      singletonLifecycleManager = new SingletonLifecycleManager <A>(factory);

                // act
                var a1 = singletonLifecycleManager.GetObject();
                var a2 = singletonLifecycleManager.GetObject();

                // assert
                Assert.NotNull(a1);
                Assert.NotNull(a2);
                Assert.Equal(a1, a2);
            }
            public void ReturnsTheSameInstanceEvenWhenCalledFromSeparateThreads()
            {
                // arrange
                Func <A> factory = () => new A();
                var      singletonLifecycleManager = new SingletonLifecycleManager <A>(factory);

                A a1 = null;
                A a2 = null;

                // act
                var thread1 = new Thread(() => a1 = singletonLifecycleManager.GetObject());
                var thread2 = new Thread(() => a2 = singletonLifecycleManager.GetObject());

                thread1.Start();
                thread2.Start();

                thread1.Join();
                thread2.Join();

                // assert
                Assert.NotNull(a1);
                Assert.NotNull(a2);
                Assert.Equal(a1, a2);
            }