Пример #1
0
        public async Task SerializedResultInvokedAsyncMethodResult()
        {
            var    eventCount       = 0;
            object serializedResult = null;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.AsyncMethodCall(It.IsAny <int>(), It.IsAny <string>()), CaptureOptions.SerializedResult)
                        .Adapt(new ForTestingPurposes());

            var result = await proxy.AsyncMethodCall(1, "one");

            Thread.Sleep(1000);

            Assert.Equal(1, eventCount);
            Assert.Equal(result, JsonConvert.DeserializeObject((string)serializedResult));

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                serializedResult = @event[nameof(CaptureOptions.SerializedResult)];
            }
        }
Пример #2
0
        public async Task AsyncActionDoesNotAllowSerializedResultOption()
        {
            var eventCount     = 0;
            var includesResult = true;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.AsyncAction(It.IsAny <int>(), It.IsAny <string>()), CaptureOptions.SerializedResult)
                        .Adapt(new ForTestingPurposes());

            await proxy.AsyncAction(0, "zero");

            Thread.Sleep(1000);

            Assert.Equal(1, eventCount);
            Assert.False(includesResult);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                includesResult = @event.ContainsKey(nameof(CaptureOptions.SerializedResult));
            }
        }
Пример #3
0
        public void MultipleNonCachedInvocationsYieldsMultipleInvocations()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>().Adapt(instance);

            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");

            Assert.Equal <uint>(3, instance.MethodCallInvocationCount);
        }
Пример #4
0
        public void MultipleCachedInvocationsYieldsSingleActualInvocation()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.MethodCall(0, "zero"), For.Ever())
                           .Adapt(instance);

            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");

            Assert.Equal <uint>(1, instance.MethodCallInvocationCount);
        }
Пример #5
0
        public void ExpiredInvocationsYieldsMultipleInvocations()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.MethodCall(0, "zero"), While.Result.True <string>((result, dt) => false))
                           .Adapt(instance);

            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");
            proxy.MethodCall(1, "one");
            proxy.MethodCall(2, "two");

            Assert.Equal <uint>(4, instance.MethodCallInvocationCount);
        }
Пример #6
0
        public void MultipleCacheExpectationsYieldExpectedResult()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.MethodCall(It.IsAny <int>(), "zero"), For.Ever())
                           .Adapt(instance);

            proxy.MethodCall(0, "zero");
            var result0 = proxy.MethodCall(0, "zero");

            proxy.MethodCall(1, "zero");
            var result1 = proxy.MethodCall(1, "zero");

            Assert.Equal <uint>(2, instance.MethodCallInvocationCount);
            Assert.Equal("0zero", result0);
            Assert.Equal("1zero", result1);
        }
Пример #7
0
        public void MixedFuzzyInvocationsYieldsMultipleInvocations()
        {
            var instance = new ForTestingPurposes();

            var proxy = new PerMethodAdapter <IForTestingPurposes>()
                        .Cache(x => x.MethodCall(It.IsAny <int>(), "zero"), For.Ever())
                        .Adapt(instance);

            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");
            proxy.MethodCall(1, "zero");
            proxy.MethodCall(1, "zero");
            proxy.MethodCall(2, "zero");
            proxy.MethodCall(2, "zero");

            Assert.Equal <uint>(3, instance.MethodCallInvocationCount);
        }
Пример #8
0
        public void ClassProxyTargetOnlyVirtualMethodsAreCached()
        {
            var instance = new ForTestingPurposes();

            var proxy = new PerMethodAdapter <ForTestingPurposes>()
                        .Cache(x => x.MethodCall(It.IsAny <int>(), "zero"), For.Ever())
                        .Cache(x => x.VirtualMethodCall(It.IsAny <int>(), "zero"), For.Ever())
                        .Adapt(instance);

            proxy.MethodCall(0, "zero");
            proxy.MethodCall(0, "zero");
            proxy.VirtualMethodCall(0, "zero");
            proxy.VirtualMethodCall(0, "zero");

            Assert.Equal <uint>(2, proxy.MethodCallInvocationCount);
            Assert.Equal <uint>(1, instance.VirtualMethodCallInvocationCount);
            Assert.Equal <uint>(0, instance.MethodCallInvocationCount);
        }
Пример #9
0
        public void MultipleMemberInvocationsYieldsSingleInvocation()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.Member, For.Ever())
                           .Adapt(instance);

            proxy.Member = "test";
            // ReSharper disable once RedundantAssignment
            var result = proxy.Member;

            instance.Member = "not equal to test";

            result = proxy.Member;

            Assert.Equal <uint>(1, instance.MemberGetInvocationCount);
            Assert.Equal("test", result);
        }
Пример #10
0
        public void NonProfiledMethodDoesNotProfile()
        {
            var eventCount = 0;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Adapt(new ForTestingPurposes());

            proxy.MethodCall(0, "zero");

            Assert.Equal(0, eventCount);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
            }
        }
Пример #11
0
        public async Task ExpiredResultYieldsMultipleActualInvocations()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.AsyncMethodCall(It.IsAny <int>(), "zero"), While.Result.True <string>((s, dt) => false))
                           .Adapt(instance);

            // ReSharper disable once NotAccessedVariable
            // ReSharper disable once RedundantAssignment
            await proxy.AsyncMethodCall(0, "zero");

            // I hate to have to do this, but otherwise the second
            // invocation may complete before the first invocation
            // is added to cache.
            Thread.Sleep(2000);

            await proxy.AsyncMethodCall(0, "zero");

            Assert.Equal <uint>(2, instance.AsyncMethodCallInvocationCount);
        }
Пример #12
0
        public async Task MultipleCachedAsyncInvocationsYieldsSingleInstanceInvocation()
        {
            var instance = new ForTestingPurposes();
            var proxy    = new PerMethodAdapter <IForTestingPurposes>()
                           .Cache(x => x.AsyncMethodCall(It.IsAny <int>(), "zero"), For.Ever())
                           .Adapt(instance);

            // ReSharper disable once NotAccessedVariable
            // ReSharper disable once RedundantAssignment
            var result = await proxy.AsyncMethodCall(0, "zero");

            // I hate to have to do this, but otherwise the second
            // invocation may complete before the first invocation
            // is added to cache.
            Thread.Sleep(2000);

            result = await proxy.AsyncMethodCall(0, "zero");

            Assert.Equal <uint>(1, instance.AsyncMethodCallInvocationCount);
            Assert.Equal("0zero", result);
        }
Пример #13
0
        public void DeclaringTypeMatchesInvokedMethod()
        {
            var    eventCount = 0;
            object typeName   = null;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.MethodCall(0, "zero"), CaptureOptions.DeclaringTypeName)
                        .Adapt(new ForTestingPurposes());

            proxy.MethodCall(0, "zero");

            Assert.Equal(1, eventCount);
            Assert.Equal(typeof(ForTestingPurposes).FullName, typeName);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                typeName = @event[nameof(CaptureOptions.DeclaringTypeName)];
            }
        }
Пример #14
0
        public void SerializedResultMatchesFuzzyInvokedMethodResult()
        {
            var    eventCount       = 0;
            object serializedResult = null;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.MethodCall(It.IsAny <int>(), It.IsAny <string>()), CaptureOptions.SerializedResult)
                        .Adapt(new ForTestingPurposes());

            var result = proxy.MethodCall(7, "eight");

            Assert.Equal(1, eventCount);
            Assert.Equal(result, JsonConvert.DeserializeObject((string)serializedResult));

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                serializedResult = @event[nameof(CaptureOptions.SerializedResult)];
            }
        }
Пример #15
0
        public void SerializedParametersDoesNotThrowForPropertyGetter()
        {
            var    eventCount      = 0;
            object serializedInput = null;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.Member, CaptureOptions.SerializedInputParameters)
                        .Adapt(new ForTestingPurposes());

            var _ = proxy.Member;

            Assert.Equal(1, eventCount);
            Assert.Equal("[]", serializedInput);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                serializedInput = @event[nameof(CaptureOptions.SerializedInputParameters)];
            }
        }
Пример #16
0
        public void SynchronousActionDoesNotAllowSerializedResultOption()
        {
            var eventCount     = 0;
            var includesResult = true;

            var proxy = new PerMethodAdapter <IForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.SynchronousAction(It.IsAny <int>(), It.IsAny <int>(), It.IsNotNull <string>()), CaptureOptions.SerializedResult)
                        .Adapt(new ForTestingPurposes());

            proxy.SynchronousAction(0, 1, "two");

            Assert.Equal(1, eventCount);
            Assert.False(includesResult);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                includesResult = @event.ContainsKey(nameof(CaptureOptions.SerializedResult));
            }
        }
Пример #17
0
        public void DefaultOptionsYieldExpectedDictionarySize()
        {
            var eventCount = 0;
            var itemCount  = 0;

            var proxy = new PerMethodAdapter <ForTestingPurposes>
                        (
                Process.Lean(EventProcessor)
                        )
                        .Profile(x => x.VirtualMethodCall(It.IsAny <int>(), It.IsAny <string>()))
                        .Adapt(new ForTestingPurposes());

            proxy.VirtualMethodCall(0, "zero");

            Assert.Equal(1, eventCount);
            Assert.Equal(BitFunctions.CountSet((int)CaptureOptions.Default), itemCount);

            void EventProcessor(IDictionary <string, object> @event)
            {
                eventCount++;
                itemCount = @event.Count;
            }
        }