/// <summary> /// Invoke <paramref name="disposeAction"/> on the dispose of the object. /// /// If parent object is disposed or being disposed, the disposable will be disposed immedialy. /// </summary> /// <param name="disposeAction"></param> /// <returns>filesystem</returns> public FileSystemProvider AddDisposeAction(Action <FileSystemProvider> disposeAction) { // Argument error if (disposeAction == null) { throw new ArgumentNullException(nameof(disposeAction)); } // Parent is disposed/ing if (IsDisposing) { disposeAction(this); return(this); } // Adapt to IDisposable IDisposable disposable = new DisposeAction <FileSystemProvider>(disposeAction, this); // Add to list lock (m_disposelist_lock) disposeList.Add(disposable); // Check parent again if (IsDisposing) { lock (m_disposelist_lock) disposeList.Remove(disposable); disposable.Dispose(); return(this); } // OK return(this); }
public void NotCallDisposeActionTwice() { //Given var actionCalled = false; var disposeAction = new DisposeAction(() => { actionCalled = true; }); disposeAction.Dispose(); actionCalled = false; //When disposeAction.Dispose(); //Then Assert.False(actionCalled); }
/// <summary> /// Invoke <paramref name="disposeAction"/> on the dispose of the object. /// /// If parent object is disposed or being disposed, the <paramref name="disposeAction"/> is executed immediately. /// </summary> /// <param name="disposeAction"></param> /// <param name="state"></param> /// <returns>true if was added to list, false if was disposed right away</returns> bool IDisposeList.AddDisposeAction(Action <object> disposeAction, object state) { // Argument error if (disposeAction == null) { throw new ArgumentNullException(nameof(disposeAction)); } // Parent is disposed/ing if (IsDisposing) { disposeAction(this); return(false); } // Adapt to IDisposable IDisposable disposable = new DisposeAction <object>(disposeAction, state); // Add to list lock (m_disposelist_lock) disposeList.Add(disposable); // Check parent again if (IsDisposing) { lock (m_disposelist_lock) disposeList.Remove(disposable); disposable.Dispose(); return(false); } // OK return(true); }
public void DisposeAction_CanDisposeTwice() { int isCalled = 0; using (var sut = new DisposeAction(() => isCalled++)) { Assert.Equal(0, isCalled); sut.Dispose(); Assert.Equal(1, isCalled); } Assert.Equal(1, isCalled); }
public void RunActionOnDispose() { //Given var actionCalled = false; var disposeAction = new DisposeAction(() => { actionCalled = true; }); //When disposeAction.Dispose(); //Then Assert.True(actionCalled); }
public void Dispose_When_UsedWithAsyncAwait_Then_DisposeShouldNotReturnUntilCompleted() { var manualResetEvent = new ManualResetEventSlim(false); var testee = new DisposeAction(async() => { await Task.Delay(20); manualResetEvent.Set(); }); testee.Dispose(); manualResetEvent.IsSet.Should().BeTrue(); }
public void Dispose_Test() { var id = 0; var disposeAction = new DisposeAction(() => { id = 100; }); disposeAction.Dispose(); Assert.Equal(100, id); var disposeAction2 = DisposeAction.Empty; disposeAction2.Dispose(); }
public void Dispose_CallAction() { _DisposeAction.Dispose(); _Action.Received(1).Invoke(); }