Example #1
0
        public void Disposable_services_should_be_disposed_when_released()
        {
            DisposableFoo.ResetDisposedCount();
            Container.Register(Component.For <DisposableFoo>().LifeStyle.Transient);

            var foo = Container.Resolve <DisposableFoo>();

            Container.Release(foo);

            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #2
0
        public void Ending_scope_releases_component()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifestyleScoped());

            using (Container.BeginScope())
            {
                Container.Resolve <DisposableFoo>();
            }

            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
        public void When_interceptor_throws_previous_dependencies_get_released()
        {
            DisposableFoo.ResetDisposedCount();
            container.Register(
                Component.For <ThrowInCtorInterceptor>().LifeStyle.Transient,
                Component.For <DisposableFoo>().LifeStyle.Transient,
                Component.For <UsesDisposableFoo>().LifeStyle.Transient
                .Interceptors <ThrowInCtorInterceptor>()
                );

            Assert.Throws <ComponentActivatorException>(() => container.Resolve <UsesDisposableFoo>());
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
        public void Releasing_lazy_releases_requested_component()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Transient);

            var lazy = Container.Resolve <Lazy <DisposableFoo> >();

            Assert.AreEqual(0, DisposableFoo.DisposedCount);
            var value = lazy.Value;

            Container.Release(lazy);
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #5
0
        public void Scoped_component_is_not_tracked_by_the_release_policy()
        {
            DisposableFoo foo;

            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Scoped());

            using (Container.BeginScope())
            {
                foo = Container.Resolve <DisposableFoo>();
                Assert.IsFalse(Kernel.ReleasePolicy.HasTrack(foo));
            }
        }
Example #6
0
        public void Releasing_component_depending_on_a_factory_releases_what_was_pulled_from_it()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Transient,
                               Component.For <UsesDisposableFooDelegate>().LifeStyle.Transient);
            var dependsOnFoo = Container.Resolve <UsesDisposableFooDelegate>();

            dependsOnFoo.GetFoo();

            Assert.AreEqual(0, DisposableFoo.DisposedCount);
            Container.Release(dependsOnFoo);
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #7
0
        public void Scoped_component_is_not_released_by_call_to_container_Release()
        {
            DisposableFoo foo;

            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Scoped());

            using (Container.BeginScope())
            {
                foo = Container.Resolve <DisposableFoo>();
                Container.Release(foo);
                Assert.AreEqual(0, DisposableFoo.DisposedCount);
            }
        }
        public void Factory_does_not_reference_components_after_they_are_released()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Transient,
                               Component.For <UsesDisposableFooDelegate>().LifeStyle.Transient);
            var dependsOnFoo = Container.Resolve <UsesDisposableFooDelegate>();

            var tracker = ReferenceTracker.Track(() => dependsOnFoo.GetFoo());

            Assert.AreEqual(0, DisposableFoo.DisposedCount);
            Container.Release(dependsOnFoo);
            Assert.AreEqual(1, DisposableFoo.DisposedCount);

            tracker.AssertNoLongerReferenced();
        }
Example #9
0
        public void Scoped_component_is_bound_to_the_innermost_scope()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Scoped());

            using (Container.BeginScope())
            {
                using (Container.BeginScope())
                {
                    Container.Resolve <DisposableFoo>();
                    Assert.AreEqual(0, DisposableFoo.DisposedCount);
                }
                Assert.AreEqual(1, DisposableFoo.DisposedCount);
            }
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #10
0
        public void Release_doesnt_stop_tracking_component_singleton_until_container_is_disposed()
        {
            DisposableFoo.ResetDisposedCount();
            container.Register(Singleton <DisposableFoo>());

            var tracker = ReferenceTracker.Track(() => container.Resolve <DisposableFoo>());

            tracker.AssertStillReferencedAndDo(foo => container.Release(foo));

            tracker.AssertStillReferenced();
            Assert.AreEqual(0, DisposableFoo.DisposedCount);

            container.Dispose();

            tracker.AssertNoLongerReferenced();
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #11
0
        public void Disposable_singleton_dependency_of_transient_open_generic_is_disposed()
        {
            DisposableFoo.ResetDisposedCount();
            Container.Register(
                Component.For(typeof(GenericComponent <>)).LifeStyle.Transient,
                Component.For <DisposableFoo>().LifeStyle.Singleton
                );

            var depender = Container.Resolve <GenericComponent <DisposableFoo> >();
            var weak     = new WeakReference(depender.Value);

            depender = null;
            Container.Dispose();
            GC.Collect();

            Assert.AreEqual(1, DisposableFoo.DisposedCount);
            Assert.IsFalse(weak.IsAlive);
        }
Example #12
0
        public void Factory_does_not_referece_components_after_theyve_been_released()
        {
            DisposableFoo.ResetDisposedCount();

            Container.Register(Component.For <DisposableFoo>().LifeStyle.Transient,
                               Component.For <UsesDisposableFooDelegate>().LifeStyle.Transient);
            var dependsOnFoo = Container.Resolve <UsesDisposableFooDelegate>();
            var foo          = dependsOnFoo.GetFoo();

            Assert.AreEqual(0, DisposableFoo.DisposedCount);
            Container.Release(dependsOnFoo);
            Assert.AreEqual(1, DisposableFoo.DisposedCount);

            var weakFoo = new WeakReference(foo);

            foo = null;
            GC.Collect();
            Assert.IsFalse(weakFoo.IsAlive);
        }
Example #13
0
        public void Nested_container_and_scope_used_together_dont_cause_components_to_be_released_twice()
        {
            DisposableFoo.ResetDisposedCount();
            Container.Register(Component.For <IWindsorContainer>().LifeStyle.Scoped()
                               .UsingFactoryMethod(k =>
            {
                var container = new WindsorContainer();
                container.Register(Component.For <DisposableFoo>().LifestyleScoped());

                k.AddChildKernel(container.Kernel);
                return(container);
            }));
            using (Container.BeginScope())
            {
                var child = Container.Resolve <IWindsorContainer>();
                child.Resolve <DisposableFoo>();
            }

            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
        public void Release_doesnt_stop_tracking_component_singleton_until_container_is_disposed()
        {
            DisposableFoo.ResetDisposedCount();
            container.Register(Singleton <DisposableFoo>());
            var foo     = container.Resolve <DisposableFoo>();
            var fooWeak = new WeakReference(foo);

            container.Release(foo);
            foo = null;
            GC.Collect();

            Assert.IsTrue(fooWeak.IsAlive);
            Assert.AreEqual(0, DisposableFoo.DisposedCount);

            container.Dispose();
            GC.Collect();

            Assert.IsFalse(fooWeak.IsAlive);
            Assert.AreEqual(1, DisposableFoo.DisposedCount);
        }
Example #15
0
        public void Disposable_singleton_dependency_of_transient_open_generic_is_disposed()
        {
            DisposableFoo.ResetDisposedCount();
            Container.Register(
                Component.For(typeof(GenericComponent <>)).LifeStyle.Transient,
                Component.For <DisposableFoo>().LifeStyle.Singleton
                );

            var tracker = ReferenceTracker
                          .Track(() =>
            {
                var depender = Container.Resolve <GenericComponent <DisposableFoo> >();
                return(depender.Value);
            });

            Container.Dispose();

            Assert.AreEqual(1, DisposableFoo.DisposedCount);
            tracker.AssertNoLongerReferenced();
        }