public void Should_publish_request_with_address() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); routeFinder.RequestQueue.Returns("RequestQueue"); InternalDependencies.RpcQueueHelper = Substitute.For<IRpcQueueHelper>(); var client = new BurrowRpcClientCoordinator<ISomeService>(null, routeFinder); var res = new RpcRequest { MethodName = "TryParse", Id = Guid.NewGuid(), UtcExpiryTime = DateTime.UtcNow.AddSeconds(20), }; tunnel.When(x => x.Publish(Arg.Any<RpcRequest>(), Arg.Any<string>())) .Do(callInfo => { var waithHandler = client.GetCachedWaitHandlers()[res.Id]; waithHandler.WaitHandle.Set(); waithHandler.Response = new RpcResponse { ChangedParams = new Dictionary<string, object> { { "result", "1000" } } }; }); // Action client.Send(res); // Assert tunnel.Received(1).Publish(Arg.Any<RpcRequest>(), "RequestQueue"); }
public void Intercept(IInvocation invocation) { var method = invocation.Method; var attributes = method.GetCustomAttributes(true).Select(x => x as Attribute).ToArray(); var isAsync = _methodFilters.Any(filter => filter.IsAsync(method, attributes)); _methodFilters.ForEach(filter => filter.CheckValid(method, attributes, isAsync)); var @params = method.GetParameters().ToList(); var args = new Dictionary<string, object>(); for(int i=0 ; i < invocation.Arguments.Length; i++) { args.Add(@params[i].Name, invocation.Arguments[i]); } var request = new RpcRequest { Params = args, MemberType = method.MemberType, MethodName = method.Name, MethodSignature = InternalDependencies.MethodMatcher.GetMethodSignature(method), DeclaringType = method.DeclaringType.FullName, }; var timeToLiveAttribute = attributes.LastOrDefault(x => x is RpcTimeToLiveAttribute); if (timeToLiveAttribute != null) { var att = (RpcTimeToLiveAttribute) timeToLiveAttribute; if (att.Seconds > 0) { request.UtcExpiryTime = DateTime.UtcNow.AddSeconds(att.Seconds); } } if (isAsync) { _clientCoordinator.SendAsync(request); return; } var response = _clientCoordinator.Send(request); if (response == null) { throw new Exception("RpcResponse is null for some reason. It's always expected to be something not null, probably there's something wrong with the bloody Coordinator"); } MapResponseResult(invocation, @params, response); }
public void Should_throw_exeception_if_timeout() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); routeFinder.UniqueResponseQueue.Returns("ISomeService.ResponseQueue"); InternalDependencies.RpcQueueHelper = Substitute.For<IRpcQueueHelper>(); var client = new BurrowRpcClientCoordinator<ISomeService>(null, routeFinder); var res = new RpcRequest { UtcExpiryTime = DateTime.UtcNow.AddSeconds(1) }; // Action client.Send(res); }
public void Should_return_null_if_not_match_anything() { // Arrange var matcher = new MethodMatcher(); var request = new RpcRequest { DeclaringType = typeof(ISomeService).FullName, MethodName = "MethodNotFound", MemberType = MemberTypes.Method, MethodSignature = matcher.GetMethodSignature(new DynamicMethod("MethodNotFound", typeof(void), null, true)) }; // Action var method = matcher.Match<ISomeService>(request); // Assert Assert.IsNull(method); }
public void Should_return_method_info_if_match() { // Arrange var matcher = new MethodMatcher(); var request = new RpcRequest { DeclaringType = typeof(ISomeService).FullName, MethodName = "TryParse", MemberType = MemberTypes.Method, MethodSignature = matcher.GetMethodSignature(typeof(ISomeService).GetMethod("TryParse")) }; // Action var method = matcher.Match<ISomeService>(request); // Assert Assert.IsNotNull(method); }
public void Should_publish_nothing_if_msg_is_Async() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); var instance = Substitute.For<ISomeService>(); var server = new BurrowRpcServerCoordinator<ISomeService>(instance, routeFinder, "queue-connnection-string", "10"); var request = new RpcRequest { Id = Guid.NewGuid(), }; // Action server.Start(); server.HandleMesage(request); // Assert tunnel.DidNotReceive().Publish(Arg.Any<RpcResponse>(), Arg.Any<string>()); }
public void Should_publish_respones_with_Exception_if_method_not_match() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); var instance = Substitute.For<ISomeService>(); var server = new BurrowRpcServerCoordinator<ISomeService>(instance, routeFinder, "queue-connnection-string", "10"); var request = new RpcRequest { Id = Guid.NewGuid(), ResponseAddress = "Address" }; // Action server.Start(); server.HandleMesage(request); // Assert tunnel.Received(1).Publish(Arg.Is<RpcResponse>(arg => arg.Exception != null && arg.RequestId == request.Id), "Address"); }
public void Should_print_warn_msg_and_return_if_msg_is_expired() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); var instance = Substitute.For<ISomeService>(); var server = new BurrowRpcServerCoordinator<ISomeService>(instance, routeFinder, "queue-connnection-string", "10"); var request = new RpcRequest { UtcExpiryTime = DateTime.UtcNow.AddSeconds(-10) }; // Action server.HandleMesage(request); // Assert Global.DefaultWatcher.Received(1).WarnFormat(Arg.Any<string>(), Arg.Any<object[]>()); tunnel.DidNotReceive().Publish(Arg.Any<RpcResponse>(), Arg.Any<string>()); }
public void Should_publish_request_without_address() { // Arrange InternalDependencies.RpcQueueHelper = Substitute.For<IRpcQueueHelper>(); var client = new BurrowRpcClientCoordinator<ISomeService>(null, Substitute.For<IRpcRouteFinder>()); var res = new RpcRequest { ResponseAddress = "abc" }; // Action client.SendAsync(res); // Assert tunnel.Received(1).Publish(Arg.Any<RpcRequest>(), Arg.Any<string>()); Assert.IsNull(res.ResponseAddress); }
public void Should_invoke_method_on_real_instance_and_map_response_params() { // Arrange var routeFinder = Substitute.For<IRpcRouteFinder>(); var instance = Substitute.For<ISomeService>(); var server = new BurrowRpcServerCoordinator<ISomeService>(instance, routeFinder, "queue-connnection-string", "10"); var request = new RpcRequest { Id = Guid.NewGuid(), ResponseAddress = "Address", MethodName = "Search", Params = new Dictionary<string, object> { { "page", 1 }, {"query", new SomeMessage { Name = "vantheshark" }}} }; var methodInfo = typeof(ISomeService).GetMethod("Search"); methodMatcher.Match<ISomeService>(request) .Returns(methodInfo); var returnValue = new List<SomeMessage> { new SomeMessage { Money = "$1" }, new SomeMessage { Money = "$1" } }; instance.Search(1, Arg.Is<SomeMessage>(arg => arg.Name == "vantheshark")) .Returns(returnValue); // Action server.Start(); server.HandleMesage(request); // Assert tunnel.Received(1).Publish(Arg.Is<RpcResponse>(arg => arg.Exception == null && arg.ReturnValue == returnValue && arg.ChangedParams["query"] is SomeMessage && 1.Equals(arg.ChangedParams["page"]) && arg.RequestId == request.Id), "Address"); }
public virtual void SendAsync(RpcRequest request) { request.ResponseAddress = null; // Don't need response _tunnel.Publish(request, _routeFinder.RequestQueue); }