public void Test_basic_interception_chain() { var counter = new Counter(); var a = new SimpleInterceptor(counter); var b = new SimpleInterceptor(counter); var c = new SimpleInterceptor(counter); var inv = new TestInvocation(); Assert.IsFalse(inv.Proceeded); Assert.IsFalse(inv.Invoked); var chain = new AopInterceptorChain(new[] { a, b, c }); chain.Intercept(inv); // check invocation ultimately invoked, but NOT via the Proceed() method Assert.IsFalse(inv.Proceeded); Assert.IsTrue(inv.Invoked); // check each interceptor called in correct order Assert.AreEqual(0, a.InterceptIndex); Assert.AreEqual(1, b.InterceptIndex); Assert.AreEqual(2, c.InterceptIndex); }
public void Test_CacheableRequest_TtlZeroCacheDirective() { var target = new MyService(); var request = new CacheableRequest(); var response = new object(); var directive = new ResponseCachingDirective(true, TimeSpan.Zero, ResponseCachingSite.Server); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); Assert.IsTrue(invocation.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); // check that response was not cached var cache = new TestCacheClient(); var cacheEntry = cache.Get(request.GetCacheKey(), new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public void Test_retry_interceptor() { var counter = new Counter(); var a = new SimpleInterceptor(counter); var b = new RetryInterceptor(10); var c = new SimpleInterceptor(counter); var inv = new TestInvocation(); Assert.AreEqual(0, inv.ProceedCount); Assert.AreEqual(0, inv.InvokeCount); var chain = new AopInterceptorChain(new IInterceptor[] { a, b, c }); chain.Intercept(inv); // check invocation ultimately invoked 10 times, but NOT via the Proceed() method Assert.AreEqual(0, inv.ProceedCount); Assert.AreEqual(10, inv.InvokeCount); // check each interceptor called correct number of times Assert.AreEqual(1, a.InterceptCount); Assert.AreEqual(1, b.InterceptCount); Assert.AreEqual(10, c.InterceptCount); }
public void ThrowExceptionIfInnerAdviceCannotBeBuild() { //Arrange var testInvocation = new TestInvocation(); var sut = new LazyAdvice <TestAdviceWithoutParameterlessConstructor>(() => null); //Act + Assert Expect.WillThrow <CannotInstantiateAdviceException>(() => sut.ApplyAdvice(testInvocation), e => e.Message.Contains(typeof(TestAdviceWithoutParameterlessConstructor).Name)); }
public void NoInvocationsMatchesTest() { //Arrange var sut = new NeverApplyAdvice(); var invocation = new TestInvocation(); //Act var invocationMatches = sut.IsApplicableFor(invocation); //Assert Assert.IsFalse(invocationMatches); }
public void AllInvocationsMatchesTest() { //Arrange var sut = new AlwaysApplyAdvice(); var invocation = new TestInvocation(); //Act var invocationMatches = sut.IsApplicableFor(invocation); //Assert Assert.IsTrue(invocationMatches); }
public void InvocationEnricher_Enrich_PropertiesAdded() { var invocation = new TestInvocation(); var invocationEnricher = new InvocationEnricher(invocation); var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>()); invocationEnricher.Enrich(logEvent, null); Assert.AreEqual(3, logEvent.Properties.Count); Assert.AreEqual($"\"{invocation.TargetType.Namespace}\"", logEvent.Properties[InvocationEnricher.NamespacePropertyName].ToString()); Assert.AreEqual($"\"{invocation.TargetType.Name}\"", logEvent.Properties[InvocationEnricher.TypePropertyName].ToString()); Assert.AreEqual($"\"{invocation.Method.Name}\"", logEvent.Properties[InvocationEnricher.MethodPropertyName].ToString()); }
public void CreateAndCallProceedOfInnerAdvice() { //Arrange var testInvocation = new TestInvocation(); var mockedAdvice = new Mock <IAdvice>(); var sut = new LazyAdvice <IAdvice>(() => mockedAdvice.Object); //Act sut.ApplyAdvice(testInvocation); //Assert mockedAdvice.Verify(x => x.ApplyAdvice(testInvocation), Times.Once()); }
public void InvocationEnricher_GivenTestInvocation_WhenEnricherApplied_ThenPropertiesAdded() { var invocation = new TestInvocation(); var invocationEnricher = new InvocationEnricher(invocation); var logEvent = new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Information, null, new MessageTemplate(new List <MessageTemplateToken>()), new List <LogEventProperty>()); invocationEnricher.Enrich(logEvent, null); logEvent.Properties.Count.Should().Be(3); logEvent.Properties[InvocationEnricher.NamespacePropertyName].ToString().Should().Be($@"""{invocation.TargetType.Namespace}"""); logEvent.Properties[InvocationEnricher.TypePropertyName].ToString().Should().Be($@"""{invocation.TargetType.Name}"""); logEvent.Properties[InvocationEnricher.MethodPropertyName].ToString().Should().Be($@"""{invocation.Method.Name}"""); }
public void TestCacheConfigurationDocument() { var cache = new TestCacheClient(); cache.ClearCache(); var documentKey = new ConfigurationDocumentKey("Test", new Version(1, 0), null, ""); var cacheKey = ((IDefinesCacheKey)documentKey).GetCacheKey(); var service = new TestConfigurationService(); object request = new GetConfigurationDocumentRequest(documentKey); object response = new GetConfigurationDocumentResponse(documentKey, DateTime.Now, DateTime.Now, "Test"); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("GetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); Assert.AreEqual(response, cacheEntry); request = new SetConfigurationDocumentRequest(documentKey, "Test"); response = new SetConfigurationDocumentResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("SetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public void Test_empty_interception_chain() { var inv = new TestInvocation(); Assert.IsFalse(inv.Proceeded); Assert.IsFalse(inv.Invoked); var chain = new AopInterceptorChain(new IInterceptor[0]); chain.Intercept(inv); // check invocation ultimately invoked, but NOT via the Proceed() method Assert.IsFalse(inv.Proceeded); Assert.IsTrue(inv.Invoked); }
public void TestCacheListSettingsGroups() { var cache = new TestCacheClient(); cache.ClearCache(); var service = new TestConfigurationService(); object request = new ListSettingsGroupsRequest(); object response = new ListSettingsGroupsResponse(new List <SettingsGroupDescriptor>()); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("ListSettingsGroups", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); request = new ImportSettingsGroupRequest( new SettingsGroupDescriptor("Test", new Version(1, 0), "Test", "Test", true), new List <SettingsPropertyDescriptor>(new[] { new SettingsPropertyDescriptor("Test", "Test", "Test", SettingScope.User, "Test") })); response = new ImportSettingsGroupResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("ImportSettingsGroup", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public static async Task PerformTest(TestInvocation testAct) { var redis = CreateRedisManager(); try { await testAct(redis, NewKeyName()); } catch { await redis.Server().FlushDatabaseAsync(); throw; } await Task.Delay(100); }
public void Test_CacheableRequest_TypicalCacheDirective() { var target = new MyService(); var request = new CacheableRequest(); var response = new object(); var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); // check invocation proceeded and return value set Assert.IsTrue(invocation.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); // check that response was cached var cache = new TestCacheClient(); var cacheEntry = cache.Get(request.GetCacheKey(), new CacheGetOptions("")); Assert.AreEqual(response, cacheEntry); // check that it was cached in the correct region var region = cache.GetRegion(request.GetCacheKey()); Assert.AreEqual(typeof(MyService).FullName + ".MyServiceOperation", region); // second invocation var invocation2 = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; // check 2nd invocation did not proceed, but return value is still set correctly from cache Assert.IsFalse(invocation2.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); }
public void Test_NonCacheableRequest_TypicalCacheDirective() { var target = new MyService(); var request = new NonCacheableRequest(); var response = new object(); var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; // a non-null cache directive on a non-cacheable request type should throw var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); }
public void Test_NonCacheableRequest_NullCacheDirective() { var target = new MyService(); var request = new NonCacheableRequest(); var response = new object(); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; var advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); Assert.IsTrue(invocation.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); }
public void Test_interceptor_does_not_proceed() { var counter = new Counter(); var a = new SimpleInterceptor(counter, false); var inv = new TestInvocation(); Assert.IsFalse(inv.Proceeded); Assert.IsFalse(inv.Invoked); var chain = new AopInterceptorChain(new[] { a }); chain.Intercept(inv); // check invocation not invoked Assert.IsFalse(inv.Proceeded); Assert.IsFalse(inv.Invoked); // check each interceptor called in correct order Assert.AreEqual(0, a.InterceptIndex); }
public void TestCacheConfigurationDocument() { var cache = new TestCacheClient(); cache.ClearCache(); var documentKey = new ConfigurationDocumentKey("Test", new Version(1, 0), null, ""); var cacheKey = ((IDefinesCacheKey) documentKey).GetCacheKey(); var service = new TestConfigurationService(); object request = new GetConfigurationDocumentRequest(documentKey); object response = new GetConfigurationDocumentResponse(documentKey, DateTime.Now, DateTime.Now, "Test"); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("GetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); Assert.AreEqual(response, cacheEntry); request = new SetConfigurationDocumentRequest(documentKey, "Test"); response = new SetConfigurationDocumentResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("SetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public void TestCacheListSettingsGroups() { var cache = new TestCacheClient(); cache.ClearCache(); var service = new TestConfigurationService(); object request = new ListSettingsGroupsRequest(); object response = new ListSettingsGroupsResponse(new List<SettingsGroupDescriptor>()); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("ListSettingsGroups", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); request = new ImportSettingsGroupRequest( new SettingsGroupDescriptor("Test", new Version(1,0), "Test", "Test", true), new List<SettingsPropertyDescriptor>(new[]{new SettingsPropertyDescriptor("Test", "Test", "Test", SettingScope.User, "Test") })); response = new ImportSettingsGroupResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("ImportSettingsGroup", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public void Test_basic_interception_chain() { var counter = new Counter(); var a = new SimpleInterceptor(counter); var b = new SimpleInterceptor(counter); var c = new SimpleInterceptor(counter); var inv = new TestInvocation(); Assert.IsFalse(inv.Proceeded); Assert.IsFalse(inv.Invoked); var chain = new AopInterceptorChain(new[] {a, b, c}); chain.Intercept(inv); // check invocation ultimately invoked, but NOT via the Proceed() method Assert.IsFalse(inv.Proceeded); Assert.IsTrue(inv.Invoked); // check each interceptor called in correct order Assert.AreEqual(0, a.InterceptIndex); Assert.AreEqual(1, b.InterceptIndex); Assert.AreEqual(2, c.InterceptIndex); }