public async Task Test() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); var proxyInstance = (SampleClass)factory.GenerateProxy(interceptor, typeof(SampleClass).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); //SyncMethod proxyInstance.SyncMethod(); Assert.AreEqual(interceptor.Logs.Count, 1); var remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(false, remoteInvoke.FireAndForget); Assert.AreEqual(nameof(proxyInstance.SyncMethod), remoteInvoke.MethodName); Assert.AreEqual(typeof(object), remoteInvoke.ReturnType); Assert.AreEqual(0, remoteInvoke.Args.Length); //AsyncMethod await proxyInstance.AsyncMethod().ConfigureAwait(false); Assert.AreEqual(interceptor.Logs.Count, 1); remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(false, remoteInvoke.FireAndForget); Assert.AreEqual(nameof(proxyInstance.AsyncMethod), remoteInvoke.MethodName); Assert.AreEqual(typeof(object), remoteInvoke.ReturnType); Assert.AreEqual(0, remoteInvoke.Args.Length); }
private async Task test <T>(DefaultProxyFactory factory, MockInterceptor interceptor, params T[] values) { var proxyInstance = (SampleClass <T>)factory.GenerateProxy(interceptor, typeof(SampleClass <T>).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); foreach (var value in values) { //SyncMethod interceptor.RemoteInvokeReturnValue = value; var returnValue = proxyInstance.SyncMethod(); Assert.AreEqual(value, returnValue); Assert.AreEqual(interceptor.Logs.Count, 1); var remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(false, remoteInvoke.FireAndForget); Assert.AreEqual(nameof(proxyInstance.SyncMethod), remoteInvoke.MethodName); Assert.AreEqual(typeof(T), remoteInvoke.ReturnType); Assert.AreEqual(0, remoteInvoke.Args.Length); //AsyncMethod interceptor.RemoteInvokeReturnValue = value; returnValue = await proxyInstance.AsyncMethod().ConfigureAwait(false); Assert.AreEqual(value, returnValue); Assert.AreEqual(interceptor.Logs.Count, 1); remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(false, remoteInvoke.FireAndForget); Assert.AreEqual(nameof(proxyInstance.AsyncMethod), remoteInvoke.MethodName); Assert.AreEqual(typeof(T), remoteInvoke.ReturnType); Assert.AreEqual(0, remoteInvoke.Args.Length); } }
public void TestInvalidTypeHandlingThrowErrorOnCreate() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); interceptor.MockSerializer.CanSerializeInterceptor = _ => false; Assert.Catch <ArgumentException>(() => factory.GenerateProxy(interceptor, typeof(TestInvalidTypeHandlingThrowErrorOnCreateClass).GetConstructor(Type.EmptyTypes))); }
public async Task Test() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); var proxyInstance = (SampleClass)factory.GenerateProxy(interceptor, typeof(SampleClass).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); foreach (var values in new[]
public async Task Test() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); //test primitive await test(factory, interceptor, int.MinValue, 1, int.MaxValue).ConfigureAwait(false); //test struct await test(factory, interceptor, DateTime.MinValue, DateTime.UtcNow, DateTime.MaxValue).ConfigureAwait(false); }
private void test(DefaultProxyFactory factory, MockInterceptor interceptor, ConstructorInfo ctor, params object[] arguments) { var proxyInstance = (SampleClass)factory.GenerateProxy(interceptor, ctor, arguments); Assert.AreEqual(interceptor.Logs.Count, 0); Assert.AreEqual(arguments.Length, proxyInstance.CtorArgs); Assert.AreEqual(arguments[0], proxyInstance.A); Assert.AreEqual(arguments.Length > 1 ? arguments[1] : default(DateTime), proxyInstance.B); Assert.AreEqual(arguments.Length > 2 ? arguments[2] : default(string), proxyInstance.C); }
public void TestEnabled() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); var proxyInstance = (TestEnabledClass)factory.GenerateProxy(interceptor, typeof(TestEnabledClass).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); Assert.Catch <NotSupportedException>(proxyInstance.DisabledMethod); Assert.AreEqual(interceptor.Logs.Count, 0); }
public void TestInvalidTypeHandlingIgnore() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); interceptor.MockSerializer.CanSerializeInterceptor = _ => false; var proxyInstance = (TestInvalidTypeHandlingIgnoreClass)factory.GenerateProxy(interceptor, typeof(TestInvalidTypeHandlingIgnoreClass).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); Assert.Catch <NotSupportedException>(() => proxyInstance.InvalidTypeHandlingMethod()); Assert.AreEqual(interceptor.Logs.Count, 0); }
public void Test() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); var constructors = typeof(SampleClass).GetConstructors(); test(factory, interceptor, constructors.First(ctor => ctor.GetParameters().Length == 3), int.MinValue, DateTime.UtcNow, "Hello World"); test(factory, interceptor, constructors.First(ctor => ctor.GetParameters().Length == 2), int.MinValue, DateTime.UtcNow); test(factory, interceptor, constructors.First(ctor => ctor.GetParameters().Length == 1), int.MinValue); }
public async Task TestFireAndForget() { var factory = new DefaultProxyFactory(); var interceptor = new MockInterceptor(); var proxyInstance = (TestFireAndForgetClass)factory.GenerateProxy(interceptor, typeof(TestFireAndForgetClass).GetConstructor(Type.EmptyTypes)); Assert.AreEqual(interceptor.Logs.Count, 0); interceptor.RemoteInvokeReturnValue = 0; await proxyInstance.FireAndForgetReturnTaskValueMethod().ConfigureAwait(false); Assert.AreEqual(interceptor.Logs.Count, 1); var remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(remoteInvoke.FireAndForget, false); interceptor.RemoteInvokeReturnValue = null; await proxyInstance.FireAndForgetTaskMethod().ConfigureAwait(false); Assert.AreEqual(interceptor.Logs.Count, 1); remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(remoteInvoke.FireAndForget, true); interceptor.RemoteInvokeReturnValue = 1; proxyInstance.FireAndForgetReturnValueMethod(); Assert.AreEqual(interceptor.Logs.Count, 1); remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(remoteInvoke.FireAndForget, false); interceptor.RemoteInvokeReturnValue = null; proxyInstance.FireAndForgetVoidMethod(); Assert.AreEqual(interceptor.Logs.Count, 1); remoteInvoke = interceptor.Logs.Dequeue(); Assert.AreEqual(remoteInvoke.FireAndForget, true); }