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)]; } }
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)); } }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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++; } }
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); }
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); }
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)]; } }
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)]; } }
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)]; } }
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)); } }
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; } }