public void FunctionWithParameters_IsInterceptedAndRemoteMethodIsInvoked() { string interceptPrefix = "Intercepted:"; var interceptor = CallInterceptor.For <IInterceptableComponent>().Func <int, DateTime, TimeSpan, string>( (component, arg1, arg2, arg3) => component.Function(arg1, arg2, arg3), (data, arg1, arg2, arg3) => { if (arg1 == 123) { data.Intercepted = true; var realResult = data.MakeRemoteCall() ?? string.Empty; return(interceptPrefix + realResult.ToString()); } // not intercepted return(null); }); ZyanConnection.CallInterceptors.Add(interceptor); var proxy = ZyanConnection.CreateProxy <IInterceptableComponent>(); // not intercepted var result = proxy.Function(321, DateTime.Now, TimeSpan.FromSeconds(1)); Assert.IsFalse(string.IsNullOrEmpty(result)); Assert.IsFalse(result.StartsWith(interceptPrefix)); // intercepted result = proxy.Function(123, DateTime.Today, TimeSpan.FromSeconds(1)); Assert.AreNotEqual(interceptPrefix, result); Assert.IsTrue(result.StartsWith(interceptPrefix)); }
public void FunctionWithParameters_IsIntercepted() { const string interceptedResult = "Intercepted!"; var interceptor = CallInterceptor.For <IInterceptableComponent>().Func <int, DateTime, TimeSpan, string>( (component, arg1, arg2, arg3) => component.Function(arg1, arg2, arg3), (data, arg1, arg2, arg3) => { if (arg1 == 123) { data.Intercepted = true; return(interceptedResult); } // not intercepted return(null); }); ZyanConnection.CallInterceptors.Add(interceptor); var proxy = ZyanConnection.CreateProxy <IInterceptableComponent>(); // not intercepted var result = proxy.Function(321, DateTime.Now, TimeSpan.FromSeconds(1)); Assert.AreNotEqual(interceptedResult, result); // intercepted result = proxy.Function(123, DateTime.Today, TimeSpan.FromSeconds(1)); Assert.AreEqual(interceptedResult, result); }
public void GenericFunctionWithParameters_IsIntercepted() { var interceptor = CallInterceptor.For <IInterceptableComponent>().Func <int, Guid, bool>( (component, arg1, arg2) => component.GenericFunction(arg1, arg2), (data, arg1, arg2) => { if (arg1 == 123) { data.Intercepted = true; return(true); } // not intercepted return(false); }); ZyanConnection.CallInterceptors.Add(interceptor); var proxy = ZyanConnection.CreateProxy <IInterceptableComponent>(); // not intercepted var result = proxy.GenericFunction(321, Guid.Empty); Assert.IsFalse(result); // intercepted result = proxy.GenericFunction(123, Guid.Empty); Assert.IsTrue(result); }
public void CallInterceptorRegistrationThreadingTest() { var interceptor = CallInterceptor.For <IInterceptableComponent>().Action(c => c.Procedure(), c => { c.Intercepted = true; }); ZyanConnection.CallInterceptors.Clear(); var proxy = ZyanConnection.CreateProxy <IInterceptableComponent>(); var count = 100000; var action1 = new Action(async() => { for (var i = 0; i < count; i++) { ZyanConnection.CallInterceptors.Add(interceptor); await Task.Yield(); } }); var action2 = new Action(async() => { for (var i = 0; i < count; i++) { proxy.Procedure(); await Task.Delay(0); } }); Task.WaitAll(Task.Run(action1), Task.Run(action2)); }
public void VoidMethodWithParameters_IsIntercepted() { var flag = false; var interceptor = CallInterceptor.For <IInterceptableComponent>().Action <long, TimeSpan, short, IInterceptableComponent>( (component, arg1, arg2, arg3, arg4) => component.Procedure(arg1, arg2, arg3, arg4), (data, arg1, arg2, arg3, arg4) => { if (arg1 == 123) { flag = true; data.Intercepted = true; } }); ZyanConnection.CallInterceptors.Add(interceptor); var proxy = ZyanConnection.CreateProxy <IInterceptableComponent>(); // not intercepted proxy.Procedure(321, TimeSpan.FromSeconds(1), 0, null); Assert.IsFalse(flag); // intercepted proxy.Procedure(123, TimeSpan.FromSeconds(1), 0, null); Assert.IsTrue(flag); }
public void CallInterceptionBug() { using (var host = new ZyanComponentHost("FirstTestServer", 18888)) { host.RegisterComponent <ITestService, TestService>(ActivationType.Singleton); using (var connection = new ZyanConnection("tcp://127.0.0.1:18888/FirstTestServer") { CallInterceptionEnabled = true }) { // add a call interceptor for the TestService.TestMethod connection.CallInterceptors.Add(CallInterceptor.For <ITestService>().Action(service => service.TestMethod(), data => { data.Intercepted = true; data.MakeRemoteCall(); data.ReturnValue = nameof(TestService); })); var testService = connection.CreateProxy <ITestService>(null); var result = testService.TestMethod(); Assert.AreEqual(nameof(TestService), result); } } }
public void Builder_ForFunc() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Func <int>(c => c.Function(), data => 0); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Function", interceptor.MemberName); Assert.AreEqual(0, interceptor.ParameterTypes.Length); }
public void Builder_ForAction() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action(c => c.Procedure(), data => Console.WriteLine("Procedure called.")); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(0, interceptor.ParameterTypes.Length); }
public void Builder_ForActionWith1Argument() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action <int>( (c, arg1) => c.Procedure(arg1), (data, arg1) => Console.WriteLine("Procedure called: {0}.", arg1)); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(1, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(int), interceptor.ParameterTypes[0]); }
public void Builder_ForFuncWith1Argument() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Func <char, byte>( (c, ch) => c.Function(ch), (data, ch) => (byte)ch); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Function", interceptor.MemberName); Assert.AreEqual(1, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(char), interceptor.ParameterTypes[0]); }
public void Builder_ForFuncWith2Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Func <string, int, char>( (c, s, i) => c.Function(s, i), (data, s, i) => s[0]); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Function", interceptor.MemberName); Assert.AreEqual(2, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(string), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(int), interceptor.ParameterTypes[1]); }
public void Builder_ForActionWith2Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action <string, char>( (c, arg1, arg2) => c.Procedure(arg1, arg2), (data, arg1, arg2) => Console.WriteLine("Procedure called: {0}, {1}.", arg1, arg2)); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(2, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(string), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(char), interceptor.ParameterTypes[1]); }
public void Builder_ForFuncWith3Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Func <int, DateTime, TimeSpan, string>( (c, i, d, t) => c.Function(i, d, t), (data, i, d, t) => i.ToString()); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Function", interceptor.MemberName); Assert.AreEqual(3, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(int), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(DateTime), interceptor.ParameterTypes[1]); Assert.AreEqual(typeof(TimeSpan), interceptor.ParameterTypes[2]); }
public void Builder_ForActionWith3Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action <decimal, Guid, DateTime>( (c, arg1, arg2, arg3) => c.Procedure(arg1, arg2, arg3), (data, arg1, arg2, arg3) => Console.WriteLine("Procedure called: {0}, {1}, {3}.", arg1, arg2, arg3)); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(3, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(decimal), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(Guid), interceptor.ParameterTypes[1]); Assert.AreEqual(typeof(DateTime), interceptor.ParameterTypes[2]); }
public void Builder_ForActionWith4Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action <long, TimeSpan, short, IInterceptableComponent>( (c, arg1, arg2, arg3, arg4) => c.Procedure(arg1, arg2, arg3, arg4), (data, arg1, arg2, arg3, arg4) => Console.WriteLine("Procedure called: {0}, {1}, {2}, {3}.", arg1, arg2, arg3, arg4)); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(4, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(long), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(TimeSpan), interceptor.ParameterTypes[1]); Assert.AreEqual(typeof(short), interceptor.ParameterTypes[2]); Assert.AreEqual(typeof(IInterceptableComponent), interceptor.ParameterTypes[3]); }
public void Builder_ForFuncWith4Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Func <bool, byte, char, int, IDisposable>( (c, b, by, ch, i) => c.Function(b, by, ch, i), (data, b, by, ch, i) => new MemoryStream()); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Function", interceptor.MemberName); Assert.AreEqual(4, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(bool), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(byte), interceptor.ParameterTypes[1]); Assert.AreEqual(typeof(char), interceptor.ParameterTypes[2]); Assert.AreEqual(typeof(int), interceptor.ParameterTypes[3]); }
public void Builder_ForActionWith5Arguments() { var interceptor = CallInterceptor .For <IInterceptableComponent>() .Action <object, ushort, sbyte, IDisposable, ulong>( (c, arg1, arg2, arg3, arg4, arg5) => c.Procedure(arg1, arg2, arg3, arg4, arg5), (data, arg1, arg2, arg3, arg4, arg5) => Console.WriteLine("Procedure called: {0}, {1}, {2}, {3}, {4}.", arg1, arg2, arg3, arg4, arg5)); Assert.IsNotNull(interceptor); Assert.AreEqual(MemberTypes.Method, interceptor.MemberType); Assert.AreEqual("Procedure", interceptor.MemberName); Assert.AreEqual(5, interceptor.ParameterTypes.Length); Assert.AreEqual(typeof(object), interceptor.ParameterTypes[0]); Assert.AreEqual(typeof(ushort), interceptor.ParameterTypes[1]); Assert.AreEqual(typeof(sbyte), interceptor.ParameterTypes[2]); Assert.AreEqual(typeof(IDisposable), interceptor.ParameterTypes[3]); Assert.AreEqual(typeof(ulong), interceptor.ParameterTypes[4]); }
public void CallInterceptionBug() { const string instanceName = "FirstTestServer"; const int port = 18888; var namedService1 = nameof(NamedService1); var namedService2 = nameof(NamedService2); using (var host = new ZyanComponentHost(instanceName, port)) { // register 3 service instances by the same interface: 2 with unique name, one is unnamed // named service1 has reference to service2 as child host.RegisterComponent <ITestService, NamedService1>(namedService1, ActivationType.Singleton); host.RegisterComponent <ITestService, NamedService2>(namedService2, ActivationType.Singleton); host.RegisterComponent <ITestService, UnnamedService>(ActivationType.Singleton); using (var connection = new ZyanConnection($"tcp://127.0.0.1:{port}/{instanceName}") { CallInterceptionEnabled = true }) { // add a call interceptor for the TestService.TestMethod connection.CallInterceptors.Add(CallInterceptor.For <ITestService>() .WithUniqueNameFilter(@"NamedService\d+") .Action(service => service.TestMethod(), data => { data.ReturnValue = $"intercepted_{data.MakeRemoteCall()}"; data.Intercepted = true; })); // for unnamed service connection.CallInterceptors .For <ITestService>() .Add(c => c.TestMethod(), data => { data.ReturnValue = $"intercepted_unnamed_{data.MakeRemoteCall()}"; data.Intercepted = true; }); connection.CallInterceptors .For <ITestService>() .WithUniqueNameFilter(".*") // for all services does not matter named or not .Add(c => c.EnumerateProcedure(), action => { var result = (IEnumerable <string>)action.MakeRemoteCall(); var intercepted = result.Select(r => $"intercepted_{r}"); action.ReturnValue = intercepted.ToList(); action.Intercepted = true; }); // intercept and return children like a collection of proxies on client side // suppress remote call connection.CallInterceptors.For <ITestService>() .WithUniqueNameFilter(".*") .Add(c => c.GetChildren(), action => { action.Intercepted = true; var childNames = connection.CreateProxy <ITestService>(action.InvokerUniqueName)? .GetChildrenName() .ToList(); var children = new List <ITestService>(); foreach (var childName in childNames) { children.Add(connection.CreateProxy <ITestService>(childName)); } // prevent remote call //action.MakeRemoteCall(); action.ReturnValue = children; }); var namedClient1 = connection.CreateProxy <ITestService>(namedService1); var namedClient2 = connection.CreateProxy <ITestService>(namedService2); var unnamedClient = connection.CreateProxy <ITestService>(); // assert names Assert.AreEqual(namedClient1.Name, namedService1); Assert.AreEqual(namedClient2.Name, namedService2); Assert.AreEqual(unnamedClient.Name, nameof(UnnamedService)); // assert method class interception result var named1_TestMethod_Result = namedClient1.TestMethod(); var named2_TestMethod_Result = namedClient2.TestMethod(); var unnamed_TestMethod_Result = unnamedClient.TestMethod(); Assert.AreEqual($"intercepted_{namedService1}", named1_TestMethod_Result); Assert.AreEqual($"intercepted_{namedService2}", named2_TestMethod_Result); Assert.AreEqual($"intercepted_unnamed_{nameof(UnnamedService)}", unnamed_TestMethod_Result); // enumerate procedure: all class are handled by single interceptor var named1_enumerate_result = namedClient1.EnumerateProcedure(); var named2_enumerate_result = namedClient2.EnumerateProcedure(); var unnnamed_enumerate_result = unnamedClient.EnumerateProcedure(); Assert.AreEqual(1, named1_enumerate_result.Count()); Assert.IsTrue(named1_enumerate_result.All(r => string.Equals(r, $"intercepted_{namedService1}"))); Assert.AreEqual(2, named2_enumerate_result.Count()); Assert.IsTrue(named2_enumerate_result.All(r => string.Equals(r, $"intercepted_{namedService2}"))); Assert.IsFalse(unnnamed_enumerate_result.Any()); } } }