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(); }
public void InstanceIsNotTrackedForDeactivation() { var instance = new NotifiesWhenDisposed(); kernel.Inject(instance); kernel.Dispose(); instance.IsDisposed.Should().BeFalse(); }
public void StrategyDisposesInstanceIfItIsDisposable() { var instance = new NotifiesWhenDisposed(); var reference = new InstanceReference { Instance = instance }; this.strategy.Deactivate(this.contextMock.Object, reference); instance.IsDisposed.Should().BeTrue(); }
private NotifiesWhenDisposed GetInstanceFromSeparateThread() { NotifiesWhenDisposed instance = null; ThreadStart callback = () => instance = this.kernel.Get <NotifiesWhenDisposed>(); var thread = new Thread(callback); thread.Start(); thread.Join(); return(instance); }
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(); }
public void DisposalCascadesThroughScopesThatNotifyWhenDisposed() { MobileInfantry rico; MoritaAssaultRifle rifle; var scope = new NotifiesWhenDisposed(); var kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false }); kernel.Bind <IWarrior>().To <MobileInfantry>().InScope(ctx => RequestScope.Current).WithParameter(new Parameter("scope", scope, true)).OnDeactivation(_ => scope.Dispose()); kernel.Bind <IWeapon>().To <MoritaAssaultRifle>().InScope(ctx => ((Parameter)ctx.Parameters.First()).ValueCallback(ctx, null)); using (new RequestScope()) { rico = (MobileInfantry)kernel.Get <IWarrior>(); rifle = (MoritaAssaultRifle)rico.Weapon; } // if scopes are chained to the request scope "properly", then all services // are disposed when the request scope is disposed rifle.IsDisposed.Should().BeTrue(); }