Наследование: IExceptionDetailService
Пример #1
0
        public void Proxy_OnAfterInvoke_IfExceptionAndIfRetryCountUsedUp_IsNotFired()
        {
            var serviceSub = Substitute.For<IExceptionDetailService>();

            serviceSub
                .Method(Arg.Any<string>())
                .Throws<Exception>();

            var service = new ExceptionDetailService(serviceSub);

            int attempts = 0; // number of times method has been attempted to be called
            bool fired = false; // true if OnAfterInvoke event was fired
            var proxy = service.StartHostAndProxy<IExceptionDetailService>(c =>
            {
                c.MaximumRetries(5);
                c.RetryOnException<FaultException<ExceptionDetail>>();
                c.SetDelayPolicy(() => new ConstantDelayPolicy(TimeSpan.FromMilliseconds(10)));
                c.OnBeforeInvoke += (sender, args) => attempts++;
                c.OnAfterInvoke += (sender, args) => fired = true;
            });

            try
            {
                proxy.Method("test");
            }
            catch { }

            Assert.AreEqual(6, attempts, "Assumption failed: Should attempt to call service method 6 times");
            Assert.IsFalse(fired, "OnAfterInvoke was called when it should not have been!");
        }
Пример #2
0
        public void Proxy_OnBeforeInvoke_IfRetry_FiredManyTimes()
        {
            var resetEvent = new AutoResetEvent(false);

            // set up a service method that throws first two times, and completes on the third time
            var serviceSub = Substitute.For<IExceptionDetailService>();           
            var service = new ExceptionDetailService(serviceSub);

            int mockFireCount = 0;

            serviceSub
                .When(m => m.Method(Arg.Any<string>()))
                .Do(_ =>
                {
                    mockFireCount++;
                    if (mockFireCount < 3)
                        throw new Exception();
                });

            int fireCount = 0;
            var proxy = service.StartHostAndProxy<IExceptionDetailService>(c =>
            {
                c.MaximumRetries(10);
                c.RetryOnException<FaultException<ExceptionDetail>>();

                OnInvokeHandler handler = (sender, args) =>
                {
                    fireCount++;
                    Assert.AreEqual(fireCount > 1, args.IsRetry, "IsRetry is not set correctly");
                    Assert.AreEqual(fireCount - 1, args.RetryCounter, "RetryCounter is not set correctly");
                    Assert.AreEqual(typeof(IExceptionDetailService), args.ServiceType, "ServiceType is not set correctly");
                    
                    if (fireCount >= 2)
                        resetEvent.Set();
                };

                c.OnBeforeInvoke += handler;
            });

            proxy.Method("test");

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
                Assert.Fail("OnBeforeInvoke probably not called");

            Assert.AreEqual(3, fireCount, "Not called three times!");
        }