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 BeforeWrappedMethod_PassesCorrectParametersToWrapperLoader()
        {
            Mock.Arrange(() => _wrapperMap.Get(Arg.IsAny <InstrumentedMethodInfo>())).Returns(() => new TrackedWrapper(Mock.Create <IWrapper>()));

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

            _wrapperService.BeforeWrappedMethod(type, methodName, string.Empty, target, arguments, tracerFactoryName, null, EmptyTracerArgs, 0);

            var method                = new Method(type, methodName, string.Empty);
            var expectedMethodCall    = new MethodCall(method, target, arguments);
            var instrumetedMethodInfo = new InstrumentedMethodInfo(0, expectedMethodCall.Method, tracerFactoryName, false, null, null, false);

            Mock.Assert(() => _wrapperMap.Get(instrumetedMethodInfo));
        }
        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);
        }