public void BeforeWrappedMethod_SetsNoOpWhenThrowsExceptionTooManyTimes()
        {
            var wrapper        = Mock.Create <IWrapper>();
            var trackedWrapper = new TrackedWrapper(wrapper);

            var wrapperMap = new WrapperMap(new List <IWrapper> {
                wrapper
            }, _defaultWrapper, _noOpWrapper);

            Mock.Arrange(() => wrapper.BeforeWrappedMethod(Arg.IsAny <InstrumentedMethodCall>(), Arg.IsAny <IAgent>(), Arg.IsAny <ITransaction>())).Throws(new Exception());
            Mock.Arrange(() => _configurationService.Configuration.WrapperExceptionLimit).Returns(1);
            Mock.Arrange(() => _noOpWrapper.BeforeWrappedMethod(Arg.IsAny <InstrumentedMethodCall>(), Arg.IsAny <IAgent>(), Arg.IsAny <ITransaction>())).OccursOnce();

            var          type              = typeof(System.Web.HttpApplication);
            const string methodName        = "ExecuteStep";
            const string tracerFactoryName = "NewRelic.Agent.Core.Tracer.Factories.DefaultTracerFactory";
            var          invocationTarget  = new object();
            var          arguments         = new object[2];
            var          argumentSignature = "IExecutionStep,System.Boolean&";
            var          metricName        = string.Empty;

            var method     = new Method(type, methodName, argumentSignature);
            var methodCall = new MethodCall(method, invocationTarget, arguments);
            var info       = new InstrumentedMethodInfo(0, methodCall.Method, tracerFactoryName, false, null, null, false);

            Mock.Arrange(() => wrapper.CanWrap(info)).Returns(new CanWrapResponse(true));

            var wrapperService = new WrapperService(_configurationService, wrapperMap, _agent, _agentHealthReporter, _agentTimerService);

            Assert.Throws <Exception>(() => wrapperService.BeforeWrappedMethod(type, methodName, argumentSignature, invocationTarget, arguments, tracerFactoryName, metricName, EmptyTracerArgs, 0));
            Assert.DoesNotThrow(() => wrapperService.BeforeWrappedMethod(type, methodName, argumentSignature, invocationTarget, arguments, tracerFactoryName, metricName, EmptyTracerArgs, 0));
            Mock.Assert(_noOpWrapper);
        }
        public void SetUp()
        {
            _wrapperMap           = Mock.Create <IWrapperMap>();
            _agent                = Mock.Create <IAgent>();
            _configurationService = Mock.Create <IConfigurationService>();
            _agentHealthReporter  = Mock.Create <IAgentHealthReporter>();
            _agentTimerService    = Mock.Create <IAgentTimerService>();

            Mock.Arrange(() => _configurationService.Configuration.WrapperExceptionLimit).Returns(10);

            _defaultWrapper = Mock.Create <IDefaultWrapper>();
            _noOpWrapper    = Mock.Create <INoOpWrapper>();

            _transactionRequiredWrapper = Mock.Create <IWrapper>();
            Mock.Arrange(() => _transactionRequiredWrapper.IsTransactionRequired).Returns(true);

            _wrapperService = new WrapperService(_configurationService, _wrapperMap, _agent, _agentHealthReporter, _agentTimerService);
        }
        public void BeforeWrappedMethod_UsesNoOpWrapper_IfNoMatchingWrapper_AndDefaultWrapperCanWrapReturnsFalse()
        {
            string result     = null;
            var    wrapperMap = new WrapperMap(new List <IWrapper>(), _defaultWrapper, _noOpWrapper);

            Mock.Arrange(() => _noOpWrapper.BeforeWrappedMethod(Arg.IsAny <InstrumentedMethodCall>(), Arg.IsAny <IAgent>(), Arg.IsAny <ITransaction>())).Returns((_, __) => result = "foo");
            Mock.Arrange(() => _defaultWrapper.CanWrap(Arg.IsAny <InstrumentedMethodInfo>())).Returns(new CanWrapResponse(false));

            var          type              = typeof(Class_WrapperService);
            const string methodName        = "MyMethod";
            const string tracerFactoryName = "MyTracer";
            var          target            = new object();
            var          arguments         = new object[0];

            var wrapperService = new WrapperService(_configurationService, wrapperMap, _agent, _agentHealthReporter, _agentTimerService);

            var action = wrapperService.BeforeWrappedMethod(type, methodName, string.Empty, target, arguments, tracerFactoryName, null, EmptyTracerArgs, 0);

            action(null, null);

            Assert.AreEqual("foo", result);
        }