AddCleanupAction() 공개 메소드

Adds a delegate to an action that will be executed on cleanup
public AddCleanupAction ( System.Action action ) : void
action System.Action A delegate to the action to perform
리턴 void
        public void CleaupActionIsCalledAfterInitialize()
        {
            bool cleanupWasCalled = false;
            var context = new TestExecutionScopesManager("dummy", ctx => { });
            context.AddCleanupAction(() => cleanupWasCalled = true);

            context.EndIsolationScope();
            Assert.IsTrue(cleanupWasCalled);
        }
 public void CanAddCleanupActionAfterOneLevelWasPoppoed()
 {
     var context = new TestExecutionScopesManager("dummy", Functions.EmptyAction<IIsolationScope>());
     context.BeginIsolationScope("Level1", Functions.EmptyAction<IIsolationScope>());
     context.EndIsolationScope();
     var cleanupCalled = false;
     context.AddCleanupAction(() => cleanupCalled = true);
     context.EndIsolationScope();
     Assert.IsTrue(cleanupCalled, "Cleanup action hasn't been called");
 }
 public void CleanupActionInNestedIsolationLevelIsCalledOnlyOnPop()
 {
     bool cleaupWasCalled = false;
     var context = new TestExecutionScopesManager("dummy", ctx => { });
     context.BeginIsolationScope("dummyIsolationLevel", ctx => { });
     context.AddCleanupAction(() => cleaupWasCalled = true);
     context.EndIsolationScope();
     Assert.IsTrue(cleaupWasCalled);
     cleaupWasCalled = false;
     context.EndIsolationScope();
     Assert.IsFalse(cleaupWasCalled);
 }
        public void DoesNothingIfInitializeIsNull()
        {
            bool outerCleanupIsCalled = false, innerCleanupIsCalled = false;

            Action<IIsolationScope> nullInitialize = null;

            var context = new TestExecutionScopesManager("OuterScope", nullInitialize);
            context.AddCleanupAction(() => outerCleanupIsCalled = true);

            context.BeginIsolationScope("InnerScope", nullInitialize);
            context.AddCleanupAction(() => innerCleanupIsCalled = true);

            context.EndIsolationScope();
            Assert.IsTrue(innerCleanupIsCalled);
            context.EndIsolationScope();
            Assert.IsTrue(outerCleanupIsCalled);
        }
        public void WhenMultipleCleanupActionsThrowExceptionsAnAggregatedExceptionIsThrown()
        {
            var context = new TestExecutionScopesManager("dummy", Functions.EmptyAction<IIsolationScope>());
            var ex1 = new Exception("1st Exception");
            var ex2 = new Exception("2nd Exception");

            context.AddCleanupAction(() =>
            {
                throw ex1;
            });

            context.AddCleanupAction(() =>
            {
                throw ex2;
            });

            var aggregatedEx = TestUtils.ExpectException<AggregateException>(() => context.EndIsolationScope());

            Assert.AreEqual(2, aggregatedEx.InnerExceptions.Count, "Invalid number of inner exceptions");
            Assert.IsTrue(aggregatedEx.InnerExceptions.Contains(ex1), "1st exception is not found in the aggergate exception");
            Assert.IsTrue(aggregatedEx.InnerExceptions.Contains(ex2), "2nd exception is not found in the aggergate exception");
        }
        public void StackTraceOfCleanupActionContainsTheOriginalLineItWasThrown()
        {
            var manager = new TestExecutionScopesManager("dummy", Functions.EmptyAction<IIsolationScope>());
            manager.AddCleanupAction(MethodThatThrowsException);

            var ex = TestUtils.ExpectException<Exception>(manager.EndIsolationScope);

            StringAssert.Contains(ex.StackTrace, "MethodThatThrowsException");
        }
        public void ExceptionIsThrownIfCallingAddCleanupActionFromWithinACleaupAction()
        {
            var context = new TestExecutionScopesManager("dummy", ctx => { });
            context.AddCleanupAction(() =>
            {
                context.AddCleanupAction(() => { Assert.Fail("This code should never be called!"); });
            });

            TestUtils.ExpectException<InvalidOperationException>(() =>
            {
                context.EndIsolationScope();
            });
        }
        public void ExceptionInNestedLevelInitialization()
        {
            var calledActions = new List<string>();
            var context = new TestExecutionScopesManager("dummy", ctx => { });
            context.AddCleanupAction(() => calledActions.Add("action1"));

            var ex = TestUtils.ExpectException<Exception>(() => context.BeginIsolationScope("nested", ctx =>
            {
                context.AddCleanupAction(() => calledActions.Add("action2"));
                throw new Exception("DummyExceptionMessage");
            }));
            Assert.AreEqual("DummyExceptionMessage", ex.Message);

            Assert.AreEqual("action2", calledActions.Content());

            calledActions.Clear();
            context.EndIsolationScope();
            Assert.AreEqual("action1", calledActions.Content());
        }