public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageCollectedAndCacheIsPruned()
        {
            this.kernel.Bind <NotifiesWhenDisposed>().ToSelf().InThreadScope();
            var cache = this.kernel.Components.Get <ICache>();

            NotifiesWhenDisposed instance = null;

            ThreadStart callback = () => instance = this.kernel.Get <NotifiesWhenDisposed>();

            var thread = new Thread(callback);

            thread.Start();
            thread.Join();

            thread = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            cache.Prune();

            instance.Should().NotBeNull();
            instance.IsDisposed.Should().BeTrue();
        }
Esempio n. 2
0
        public void InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageCollectedAndCacheIsPruned()
        {
            this.kernel.Bind <NotifiesWhenDisposed>().ToSelf().InThreadScope();
            var cache = this.kernel.Components.Get <ICache>();

            // Use separate method to allow thread/scope to be finalized
            NotifiesWhenDisposed instance = GetInstanceFromSeparateThread();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            cache.Prune();

            instance.Should().NotBeNull();
            instance.IsDisposed.Should().BeTrue();
        }