/// <summary> /// Subscribe the handler for the given event and adds the unsubscription /// to the DisposableContanier so it is called on Dispose. /// </summary> /// <param name="container">The container where the event unsubscription is added.</param> /// <param name="type">The type which contains the static event.</param> /// <param name="eventName">The name of the event. It is case sensitive.</param> /// <param name="handler">The handler which are subscripted to the event.</param> public static void AddEventSubscription(this DisposableContainer container, Type type, string eventName, Action handler) { container.AddManagedResource(new EventSubscription(type, eventName, handler)); }
public void AddCleanupAction_adds_CleanupAction_to_the_DisposableContainer() { var container = new DisposableContainer(); var mock1 = new Mock <Action>(); container.AddCleanupAction(mock1.Object); container.Dispose(); mock1.Verify(d => d(), Times.Once); }
public void Dispose_disposes_only_once() { var container = new DisposableContainer(); var mock = new Mock <IDisposable>(); container.AddManagedResource(mock.Object); container.Dispose(); container.Dispose(); mock.Verify(d => d.Dispose(), Times.Once); }
public void AddManagedResource_adds_Disposables_which_is_called_on_dispose() { var container = new DisposableContainer(); var mock1 = new Mock <IDisposable>(); var mock2 = new Mock <IDisposable>(); container.AddManagedResource(mock1.Object); container.AddManagedResource(mock2.Object); container.Dispose(); mock1.Verify(d => d.Dispose(), Times.Once); mock2.Verify(d => d.Dispose(), Times.Once); }
public void Dispose_doesnt_stop_when_a_dispose_failed() { var container = new DisposableContainer(); var mock1 = new Mock <IDisposable>(); mock1.Setup(e => e.Dispose()).Throws(new Exception()); var mock2 = new Mock <IDisposable>(); container.AddManagedResource(mock1.Object); container.AddManagedResource(mock2.Object); container.Dispose(); mock2.Verify(d => d.Dispose(), Times.Once); }
public void AddEventSubscription_adds_static_handler_delegate_to_the_DisposableContainer() { var container = new DisposableContainer(); var instanceMock = new Mock <IDummyObserver>(); var args = new EventArgs(); container.AddEventSubscription <object, EventArgs>( typeof(DummyEventSource), nameof(DummyEventSource.StaticHandler1), instanceMock.Object.EventHandler); DummyEventSource.InvokeStaticHandler1(args); container.Dispose(); DummyEventSource.InvokeStaticHandler1(args); instanceMock.Verify(d => d.EventHandler(It.IsAny <object>(), It.IsAny <EventArgs>()), Times.Once); }
public void Dispose_calls_DisposeFailed_on_Exception() { var container = new DisposableContainer(); var mock1 = new Mock <IDisposable>(); var ex = new Exception(); mock1.Setup(e => e.Dispose()).Throws(ex); IDisposable resultDisposable = null; Exception resultException = null; container.DisposeFailed += (d, e) => { resultDisposable = d; resultException = e; }; container.AddManagedResource(mock1.Object); container.Dispose(); resultDisposable.Should().BeSameAs(mock1.Object); resultException.Should().BeSameAs(ex); }
/// <summary> /// Adds an action that is executed on Dispose. /// </summary> /// <param name="container">The container to add the action.</param> /// <param name="cleanupAction">the action to add.</param> public static void AddCleanupAction(this DisposableContainer container, Action cleanupAction) { container.AddManagedResource(new CleanupAction(cleanupAction)); }