Exemplo n.º 1
0
        public void TryFallback_NonPositiveCount_ThrowsArgumentException()
        {
            Action action   = () => { };
            Action fallback = () => { };

            Assert.Throws <ArgumentException>(() => FaultTolerance.TryFallback <Exception>(action, -1, fallback));
        }
Exemplo n.º 2
0
        public void TryFallbackMultipleExceptions_Failed_RunRelevantFallback()
        {
            int    count  = 0;
            Action action = () =>
            {
                count++;
                if (count == 1)
                {
                    throw new NotImplementedException();
                }
                else if (count == 2)
                {
                    throw new ArithmeticException();
                }
            };

            bool notImplementedExceptionFallbackHadRun = false;
            bool arithmeticExceptionFallbackHadRun     = false;

            var exceptions = new Dictionary <Type, Action>();

            exceptions.Add(typeof(NotImplementedException), () => notImplementedExceptionFallbackHadRun = true);
            exceptions.Add(typeof(ArithmeticException), () => arithmeticExceptionFallbackHadRun         = true);

            FaultTolerance.TryFallback(exceptions, action, 2);

            Assert.False(notImplementedExceptionFallbackHadRun);
            Assert.True(arithmeticExceptionFallbackHadRun);
        }
Exemplo n.º 3
0
        public void TryFallback_Failed_RunFallback()
        {
            bool fallbackHadRun = false;

            Action action   = () => throw new NotImplementedException();
            Action fallback = () => fallbackHadRun = true;

            FaultTolerance.TryFallback <NotImplementedException>(action, 2, fallback);

            Assert.True(fallbackHadRun);
        }
Exemplo n.º 4
0
        public void TryFallback_NotAllTypesDerivedFromException_ThrowsArgumentException()
        {
            Action action   = () => { };
            Action fallback = () => { };

            var exceptionFallbacks = new Dictionary <Type, Action>();

            exceptionFallbacks.Add(typeof(int), fallback);
            exceptionFallbacks.Add(typeof(string), fallback);

            Assert.Throws <ArgumentException>(() => FaultTolerance.TryFallback(exceptionFallbacks, action, -1));
        }
Exemplo n.º 5
0
        public void TryFallback_Succed_NoFallbackRun()
        {
            bool fallbackHadRun = false;

            int    count  = 1;
            Action action = () =>
            {
                if (count < 3)
                {
                    count++;
                    throw new ArithmeticException();
                }
            };

            Action fallback = () => fallbackHadRun = true;

            FaultTolerance.TryFallback <ArithmeticException>(action, 3, fallback);

            Assert.False(fallbackHadRun);
        }
Exemplo n.º 6
0
        public void TryFallback_Failed_NoFallbackRun_ThrowsException()
        {
            bool fallbackHadRun = false;

            int    count  = 1;
            Action action = () =>
            {
                if (count < 3)
                {
                    count++;
                    throw new ArithmeticException();
                }
                else
                {
                    throw new ArgumentException();
                }
            };
            Action fallback = () => fallbackHadRun = true;

            Assert.Throws <ArgumentException>(() => FaultTolerance.TryFallback <ArithmeticException>(action, 3, fallback));

            Assert.False(fallbackHadRun);
        }