public void AsyncScopedLifestyleDispose_WithTransientRegisteredForDisposal_DisposesThatInstance() { // Arrange DisposableCommand transientInstanceToDispose = null; var container = new Container(); container.Options.EnableAutoVerification = false; var lifestyle = new AsyncScopedLifestyle(); container.Register <DisposableCommand>(); container.RegisterInitializer <DisposableCommand>(command => { lifestyle.RegisterForDisposal(container, command); }); var scope = AsyncScopedLifestyle.BeginScope(container); try { transientInstanceToDispose = container.GetInstance <DisposableCommand>(); } finally { // Act scope.Dispose(); } // Assert Assert.IsTrue(transientInstanceToDispose.HasBeenDisposed); }
public void GetCurrentLifetimeScope_AfterMiddleScopeDisposedWhileInnerScopeNotDisposed_ReturnsOuterScope() { // Arrange var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); var instanceToDispose = new DisposableCommand(); using (var outerScope = ThreadScopedLifestyle.BeginScope(container)) { var middleScope = ThreadScopedLifestyle.BeginScope(container); var innerScope = ThreadScopedLifestyle.BeginScope(container); middleScope.Dispose(); // Act Scope actualScope = Lifestyle.Scoped.GetCurrentScope(container); string scopeName = object.ReferenceEquals(actualScope, null) ? "null" : object.ReferenceEquals(actualScope, innerScope) ? "inner" : object.ReferenceEquals(actualScope, middleScope) ? "middle" : object.ReferenceEquals(actualScope, outerScope) ? "outer" : "other"; // Assert Assert.AreSame(outerScope, actualScope, "Expected: outer. Actual: " + scopeName + " scope."); } }
public void LifetimeScopeDispose_WithTransientRegisteredForDisposal_DisposesThatInstance() { // Arrange DisposableCommand transientInstanceToDispose = null; var container = new Container(); container.Options.EnableAutoVerification = false; container.Register <DisposableCommand>(); var lifestyle = new ThreadScopedLifestyle(); container.RegisterInitializer <DisposableCommand>(command => { lifestyle.RegisterForDisposal(container, command); }); using (ThreadScopedLifestyle.BeginScope(container)) { transientInstanceToDispose = container.GetInstance <DisposableCommand>(); // Act } // Assert Assert.IsTrue(transientInstanceToDispose.HasBeenDisposed); }
public void Dispose_ObjectRegisteredForDisposalUsingRequestedCurrentLifetimeScope_DisposesThatInstance() { // Arrange var container = new Container(); container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(Lifestyle.Scoped); using (AsyncScopedLifestyle.BeginScope(container)) { var command = container.GetInstance <DisposableCommand>(); command.Disposing += s => { Lifestyle.Scoped.RegisterForDisposal(container, instanceToDispose); }; // Act } // Assert Assert.IsTrue(instanceToDispose.HasBeenDisposed); }
public void ExecutionContextScopeDispose_WithTransientRegisteredForDisposal_DisposesThatInstance() { // Arrange DisposableCommand transientInstanceToDispose = null; var container = new Container(); var lifestyle = new ExecutionContextScopeLifestyle(); container.RegisterInitializer <DisposableCommand>(command => { lifestyle.RegisterForDisposal(container, command); }); var scope = container.BeginExecutionContextScope(); try { transientInstanceToDispose = container.GetInstance <DisposableCommand>(); } finally { // Act scope.Dispose(); } // Assert Assert.IsTrue(transientInstanceToDispose.HasBeenDisposed); }
public void GetCurrentAsyncScopedLifestyle_AfterMiddleScopeDisposedWhileInnerScopeNotDisposed_ReturnsOuterScope() { // Arrange var container = new Container(); var lifestyle = new AsyncScopedLifestyle(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(lifestyle); using (Scope outerScope = AsyncScopedLifestyle.BeginScope(container)) { Scope middleScope = AsyncScopedLifestyle.BeginScope(container); Scope innerScope = AsyncScopedLifestyle.BeginScope(container); middleScope.Dispose(); // Act Scope actualScope = lifestyle.GetCurrentScope(container); // Assert Assert.AreSame(outerScope, actualScope); } }
public void GetCurrentExecutionContextScope_DisposingAnInnerScope_ShouldNeverCauseToBeSetToInnerScope() { // Arrange var container = new Container(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(new ExecutionContextScopeLifestyle()); using (var outerScope = container.BeginExecutionContextScope()) { var outerMiddleScope = container.BeginExecutionContextScope(); var innerMiddleScope = container.BeginExecutionContextScope(); var innerScope = container.BeginExecutionContextScope(); // This will cause GetCurrentExecutionContextScope to become outerScope. outerMiddleScope.Dispose(); // This should not cause BeginExecutionContextScope to change innerScope.Dispose(); // Act var actualScope = container.GetCurrentExecutionContextScope(); // Assert Assert.AreSame(outerScope, actualScope, "Even though the inner middle scope never got disposed, the inner scope should not " + "this scope upon disposal. The outer scope should retain focus."); } }
public void GetCurrentLifetimeScope_DisposingAnInnerScope_ShouldNeverCauseToBeSetToInnerScope() { // Arrange var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); var instanceToDispose = new DisposableCommand(); using (var outerScope = ThreadScopedLifestyle.BeginScope(container)) { var outerMiddleScope = ThreadScopedLifestyle.BeginScope(container); var innerMiddleScope = ThreadScopedLifestyle.BeginScope(container); var innerScope = ThreadScopedLifestyle.BeginScope(container); // This will cause GetCurrentLifetimeScope to become outerScope. outerMiddleScope.Dispose(); // This should not cause BeginLifetimeScope to change innerScope.Dispose(); // Act Scope actualScope = Lifestyle.Scoped.GetCurrentScope(container); // Assert Assert.AreSame(outerScope, actualScope, "Even though the inner middle scope never got disposed, the inner scope should not " + "this scope upon disposal. The outer scope should retain focus."); } }
public void GetCurrentExecutionContextScope_DisposingTheMiddleScopeBeforeInnerScope_ReturnsOuterScope() { // Arrange var container = new Container(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(new ExecutionContextScopeLifestyle()); using (var outerScope = container.BeginExecutionContextScope()) { var middleScope = container.BeginExecutionContextScope(); var innerScope = container.BeginExecutionContextScope(); middleScope.Dispose(); innerScope.Dispose(); // Act var actualScope = container.GetCurrentExecutionContextScope(); // Assert Assert.AreSame(outerScope, actualScope, "Since the middle scope is already disposed, the current scope should be the outer."); } }
public void LifetimeScopeDispose_WithWhenScopeEndsRegistration_CallsTheRegisteredActionBeforeCallingDispose() { // Arrange bool delegateHasBeenCalled = false; DisposableCommand instanceToDispose = null; var container = new Container(); var lifestyle = new LifetimeScopeLifestyle(); container.Register <DisposableCommand, DisposableCommand>(lifestyle); container.RegisterInitializer <DisposableCommand>(command => { LifetimeScopeLifestyle.WhenCurrentScopeEnds(container, () => { Assert.IsFalse(command.HasBeenDisposed, "The action should be called before disposing the instance, because users are " + "to use those instances."); delegateHasBeenCalled = true; }); }); using (container.BeginLifetimeScope()) { instanceToDispose = container.GetInstance <DisposableCommand>(); // Act } // Assert Assert.IsTrue(delegateHasBeenCalled, "Delegate is expected to be called."); }
public void GetCurrentLifetimeScope_AfterMiddleScopeDisposedWhileInnerScopeNotDisposed_ReturnsOuterScope() { // Arrange var container = new Container(); var instanceToDispose = new DisposableCommand(); using (var outerScope = container.BeginLifetimeScope()) { var middleScope = container.BeginLifetimeScope(); var innerScope = container.BeginLifetimeScope(); middleScope.Dispose(); // Act var actualScope = container.GetCurrentLifetimeScope(); string scopeName = object.ReferenceEquals(actualScope, null) ? "null" : object.ReferenceEquals(actualScope, innerScope) ? "inner" : object.ReferenceEquals(actualScope, middleScope) ? "middle" : object.ReferenceEquals(actualScope, outerScope) ? "outer" : "other"; // Assert Assert.AreSame(outerScope, actualScope, "Actual: " + scopeName + " scope."); } }
public void GetCurrentLifetimeScope_DisposingTheMiddleScopeBeforeInnerScope_ReturnsOuterScope() { // Arrange var container = new Container(); container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle(); var instanceToDispose = new DisposableCommand(); using (Scope outerScope = ThreadScopedLifestyle.BeginScope(container)) { Scope middleScope = ThreadScopedLifestyle.BeginScope(container); Scope innerScope = ThreadScopedLifestyle.BeginScope(container); middleScope.Dispose(); innerScope.Dispose(); // Act Scope actualScope = Lifestyle.Scoped.GetCurrentScope(container); // Assert Assert.AreSame(outerScope, actualScope, "Since the middle scope is already disposed, the current scope should be the outer."); } }
public void GetCurrentAsyncScopedLifestyle_DisposingTheMiddleScopeBeforeInnerScope_ReturnsOuterScope() { // Arrange var container = new Container(); var lifestyle = new AsyncScopedLifestyle(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(lifestyle); using (Scope outerScope = AsyncScopedLifestyle.BeginScope(container)) { Scope middleScope = AsyncScopedLifestyle.BeginScope(container); Scope innerScope = AsyncScopedLifestyle.BeginScope(container); middleScope.Dispose(); innerScope.Dispose(); // Act Scope actualScope = lifestyle.GetCurrentScope(container); // Assert Assert.AreSame(outerScope, actualScope, "Since the middle scope is already disposed, the current scope should be the outer."); } }
public async Task when_program_is_cancelled_then_executing_command_is_disposed() { var commandFactory = new CommandFactory(); var program = new Program(output, commandFactory, "test"); var command = new DisposableCommand(); // Register the command commandFactory.RegisterCommand("test", () => Mock.Of <CommandDescriptor>(), x => command); // Cancel the program while the command is executed command.Executing += async(sender, e) => await program.CancelAsync(); // Run the program await program.RunAsync(); Assert.True(command.IsDisposed); }
public void GetCurrentLifetimeScope_AfterMiddleScopeDisposedWhileInnerScopeNotDisposed_ReturnsOuterScope() { // Arrange var container = new Container(); var instanceToDispose = new DisposableCommand(); using (var outerScope = container.BeginLifetimeScope()) { var middleScope = container.BeginLifetimeScope(); var innerScope = container.BeginLifetimeScope(); middleScope.Dispose(); // Act var actualScope = container.GetCurrentLifetimeScope(); // Assert Assert.AreSame(outerScope, actualScope); } }
public void Dispose_ObjectRegisteredForDisposalUsingRequestedCurrentLifetimeScope_DisposesThatInstance() { // Arrange var container = new Container(); var instanceToDispose = new DisposableCommand(); container.Register <DisposableCommand>(new ExecutionContextScopeLifestyle()); using (container.BeginExecutionContextScope()) { var command = container.GetInstance <DisposableCommand>(); command.Disposing += s => { container.GetCurrentExecutionContextScope().RegisterForDisposal(instanceToDispose); }; // Act } // Assert Assert.IsTrue(instanceToDispose.HasBeenDisposed); }
public BackgroundThread(DisposableCommand command_to_execute, IWorkerThread worker_thread) { this.worker_thread = worker_thread; worker_thread.DoWork += (sender, e) => command_to_execute.run(); worker_thread.Disposed += (sender, e) => command_to_execute.Dispose(); }
public BackgroundThread(DisposableCommand command_to_execute) : this(command_to_execute, new WorkerThread()) {}