Пример #1
0
        public void Test_ThreadAbort_WithFatalException()
        {
            TestFunction2 function = new TestFunction2();

            function.SetExecutionListener(_executionListenerMock);

            WxeStep step1 = MockRepository.GenerateMock <WxeStep> ();

            step1.Expect(mock => mock.Execute(_context)).WhenCalled(invocation => Thread.CurrentThread.Abort());
            function.Add(step1);

            var fatalExecutionException = new WxeFatalExecutionException(new Exception("Pause exception"), null);

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionPause(_context)).Throw(fatalExecutionException);
            }
            _mockRepository.ReplayAll();

            try
            {
                function.Execute(_context);
                Assert.Fail();
            }
            catch (WxeFatalExecutionException actualException)
            {
                Assert.That(actualException, Is.SameAs(fatalExecutionException));
                Thread.ResetAbort();
            }
        }
        public void SetExecutionListener()
        {
            TestFunction2 function = new TestFunction2();

            function.SetExecutionListener(_executionListenerMock);
            Assert.That(function.ExecutionListener, Is.SameAs(_executionListenerMock));
        }
Пример #3
0
        public void Test_FailAfterExceptionAndFailInListener()
        {
            TestFunction2 function = new TestFunction2();

            function.SetExecutionListener(_executionListenerMock);

            WxeStep   step1         = MockRepository.GenerateMock <WxeStep> ();
            Exception stepException = new Exception("StepException");

            step1.Expect(mock => mock.Execute(_context)).Throw(stepException);
            function.Add(step1);

            Exception listenerException = new Exception("ListenerException");

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionFail(_context, stepException)).Throw(listenerException);
            }
            _mockRepository.ReplayAll();

            try
            {
                function.Execute(_context);
                Assert.Fail();
            }
            catch (WxeFatalExecutionException actualException)
            {
                Assert.That(actualException.InnerException, Is.SameAs(stepException));
                Assert.That(actualException.OuterException, Is.SameAs(listenerException));
            }

            _mockRepository.VerifyAll();
        }
Пример #4
0
        public void CreateTransactionStrategy_WithParentTransaction()
        {
            ITransactionMode transactionMode = new CreateChildIfParentTransactionMode(true, new TestTransactionFactory());

            WxeFunction parentFunction = new TestFunction2(new CreateRootTransactionMode(true, new TestTransactionFactory()));
            WxeFunction childFunction  = new TestFunction2(transactionMode);

            parentFunction.Add(childFunction);

            WxeStep stepMock = MockRepository.GenerateMock <WxeStep>();

            childFunction.Add(stepMock);

            WxeContextFactory wxeContextFactory = new WxeContextFactory();
            WxeContext        context           = wxeContextFactory.CreateContext(new TestFunction());

            stepMock.Expect(mock => mock.Execute(context)).WhenCalled(
                invocation =>
            {
                TransactionStrategyBase strategy = ((TestFunction2)childFunction).TransactionStrategy;
                Assert.That(strategy, Is.InstanceOf(typeof(ChildTransactionStrategy)));
                Assert.That(((ChildTransactionStrategy)strategy).AutoCommit, Is.True);
                Assert.That(strategy.OuterTransactionStrategy, Is.SameAs(((TestFunction2)parentFunction).TransactionStrategy));
            });

            parentFunction.Execute(context);
        }
Пример #5
0
 public void MinBrent3()
 {
     double xmin=0;
     double eps = 1e-5;
     double realXmin = -0.75;
     int maxCount = 50;
     LineSearch ls = new LineSearch();
     TestFunction2 f = new TestFunction2();
     double[] res = new double[3];
     int counter = ls.FindMinInterval(f,-10,1,30,ref res);
     counter = ls.FindMinimumViaBrent(f,res[0],res[1],res[2],maxCount,eps, ref xmin);
     Assert.IsTrue(counter<maxCount);
     Assert.IsTrue (System.Math.Abs(xmin-realXmin)<eps);
 }
Пример #6
0
 public void MinGold3()
 {
     double xmin=0;
     double eps = 1e-5;
     double realXmin = -0.75;
     int counterMax = 50;
     LineSearch ls = new LineSearch();
     TestFunction2 f = new TestFunction2();
     double [] res = new double[3];
     int counter1 = ls.FindMinInterval(f,-10,1,30,ref res);
     int counter2 = ls.FindMinimumViaGoldenSection(f,res[0],res[1],res[2],counterMax,eps, ref xmin);
     Assert.IsTrue(counter2<counterMax);
     Assert.IsTrue (System.Math.Abs(xmin-realXmin)<eps);
 }
Пример #7
0
        public void MinBrent4()
        {
            double        xmin     = 0;
            double        eps      = 1e-5;
            double        realXmin = -0.75;
            int           maxCount = 50;
            LineSearch    ls       = new LineSearch();
            TestFunction2 f        = new TestFunction2();

            double[] res     = new double[3];
            int      counter = ls.FindMinInterval(f, 1000, 1, 30, ref res);

            counter = ls.FindMinimumViaBrent(f, res[0], res[1], res[2], 50, eps, ref xmin);
            Assert.IsTrue(counter < maxCount);
            Assert.IsTrue(System.Math.Abs(xmin - realXmin) < eps);
        }
Пример #8
0
        public void MinGold4()
        {
            double        xmin       = 0;
            double        eps        = 1e-5;
            double        realXmin   = -0.75;
            int           counterMax = 50;
            LineSearch    ls         = new LineSearch();
            TestFunction2 f          = new TestFunction2();

            double[] res      = new double[3];
            int      counter1 = ls.FindMinInterval(f, 1000, 1, 30, ref res);
            int      counter2 = ls.FindMinimumViaGoldenSection(f, res[0], res[1], res[2], counterMax, eps, ref xmin);

            Assert.IsTrue(counter2 < counterMax);
            Assert.IsTrue(System.Math.Abs(xmin - realXmin) < eps);
        }
        public void SetTransactionMode_AfterExecutionHasStarted_ThrowsInvalidOperationException()
        {
            TestFunction2 function = new TestFunction2();

            function.Add(
                new WxeDelegateStep(
                    () => Assert.That(
                        () => function.SetTransactionMode(WxeTransactionMode <TestTransactionFactory> .CreateRoot),
                        Throws.InvalidOperationException
                        .With.Message.EqualTo("The TransactionMode cannot be set after the TransactionStrategy has been initialized."))));

            WxeContextFactory contextFactory = new WxeContextFactory();
            var context = contextFactory.CreateContext(function);

            function.Execute(context);
        }
Пример #10
0
        public void Test_ReEntryAfterThreadAbort()
        {
            TestFunction2 function = new TestFunction2();

            function.SetExecutionListener(_executionListenerMock);

            WxeStep step1 = MockRepository.GenerateMock <WxeStep> ();

            step1.Expect(mock => mock.Execute(_context)).WhenCalled(invocation => Thread.CurrentThread.Abort()).Repeat.Once();
            function.Add(step1);

            WxeStep step2 = MockRepository.GenerateMock <WxeStep>();

            step2.Expect(mock => mock.Execute(_context));
            function.Add(step2);

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionPause(_context));
            }
            _mockRepository.ReplayAll();

            try
            {
                function.Execute(_context);
                Assert.Fail();
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
            }

            _mockRepository.VerifyAll();
            _mockRepository.BackToRecordAll();

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionStop(_context));
            }
            _mockRepository.ReplayAll();

            function.Execute(_context);

            _mockRepository.VerifyAll();
        }
Пример #11
0
        public void SetTransactionMode()
        {
            TestFunction2        function          = new TestFunction2();
            ITransactionStrategy actualTransaction = null;

            function.Add(new WxeDelegateStep(() => actualTransaction = function.Transaction));
            function.SetTransactionMode(WxeTransactionMode <TestTransactionFactory> .CreateRoot);

            WxeContextFactory contextFactory = new WxeContextFactory();
            var context = contextFactory.CreateContext(function);

            Assert.That(function.Transaction, Is.InstanceOf <NullTransactionStrategy>());

            function.Execute(context);

            Assert.That(actualTransaction, Is.InstanceOf <RootTransactionStrategy>());
        }
Пример #12
0
        public void Test_NoException()
        {
            TestFunction2 function = new TestFunction2();

            function.SetExecutionListener(_executionListenerMock);

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionStop(_context));
            }

            _mockRepository.ReplayAll();

            function.Execute(_context);

            _mockRepository.VerifyAll();
        }
Пример #13
0
        public void Test_WithTransactionStrategy()
        {
            ITransactionMode        transactionModeMock     = _mockRepository.StrictMock <ITransactionMode>();
            TestFunction2           function                = new TestFunction2(transactionModeMock);
            TransactionStrategyBase transactionStrategyMock = MockRepository.GenerateMock <TransactionStrategyBase>();

            transactionModeMock.Expect(mock => mock.CreateTransactionStrategy(function, _context)).Return(transactionStrategyMock);
            transactionStrategyMock.Expect(mock => mock.CreateExecutionListener(Arg <IWxeFunctionExecutionListener> .Is.NotNull))
            .Return(_executionListenerMock);

            using (_mockRepository.Ordered())
            {
                _executionListenerMock.Expect(mock => mock.OnExecutionPlay(_context));
                _executionListenerMock.Expect(mock => mock.OnExecutionStop(_context));
            }

            _mockRepository.ReplayAll();

            function.Execute(_context);

            _mockRepository.VerifyAll();
            Assert.That(function.ExecutionListener, Is.SameAs(_executionListenerMock));
        }
Пример #14
0
        public void Test_UseNullListener()
        {
            TestFunction2 function = new TestFunction2();

            function.Execute(_context);
        }
Пример #15
0
        public void GetTransaction_BeforeTransactionStrategyInitialized()
        {
            TestFunction2 function = new TestFunction2();

            Assert.That(function.Transaction, Is.InstanceOf <NullTransactionStrategy>());
        }
Пример #16
0
        public void GetExecutionListener()
        {
            TestFunction2 function = new TestFunction2();

            Assert.That(function.ExecutionListener, Is.InstanceOf(typeof(NullExecutionListener)));
        }
Пример #17
0
        public void SetExecutionListenerNull()
        {
            TestFunction2 function = new TestFunction2();

            Assert.That(() => function.SetExecutionListener(null), Throws.TypeOf <ArgumentNullException>());
        }