Пример #1
0
        public ChaosInvocationResp ProcessInvocation(ChaosInvocation chaosInvocation)
        {
            try
            {
                var realImplementObject = _serviceResolver.GetService(chaosInvocation.InterfaceTypeFullName);
                var realImplementType   = realImplementObject.GetType();
                var realImplementInfo   = ReflectionClass.Reflection(realImplementType);

                var mi = FindMethod(realImplementInfo, chaosInvocation);

                var requestParameters = chaosInvocation.Parameters;
                var args = requestParameters.DeserializeToArguments(_serializer, _typeFinder)
                           .ToArray();

                var returnValue = mi.Func(realImplementObject, args);

                var invocationReply = ToChaosInvocationResp(chaosInvocation, returnValue);
                return(invocationReply);
            }
            catch (Exception ex)
            {
                var invocationReply = ToChaosInvocationResp(chaosInvocation, (object)null);
                invocationReply.Exception = SerializeException.CreateFromException(ex);
                return(invocationReply);
            }
        }
Пример #2
0
        public ChaosInvocation CreateChaosInvocation(Type implementType,
                                                     MethodInfo invocationMethod,
                                                     object[] invocationArguments)
        {
            var chaosInvocation = new ChaosInvocation()
            {
                InterfaceTypeFullName = implementType.FullName,
                MethodName            = invocationMethod.Name,
                GenericTypes          = GetGenericTypes(invocationMethod).ToArray(),
                Parameters            = invocationMethod.GetChaosParameters(_serializer, invocationArguments)
            };

            if (invocationMethod.ReturnType != typeof(void))
            {
                chaosInvocation.ReturnTypeFullName = invocationMethod.ReturnType.FullName;
                if (invocationMethod.ReturnType.IsTaskType())
                {
                    chaosInvocation.IsReturnTask = true;
                    var resultType = invocationMethod.ReturnType.GenericTypeArguments[0];
                    chaosInvocation.ReturnTypeFullName = resultType.FullName;
                }
            }

            return(chaosInvocation);
        }
Пример #3
0
        public object Send(ChaosInvocation invocation)
        {
            var req = invocation.ToAnyProto();

            var reply = _client.SendInvocation(req);

            var invocationResp = reply.ConvertTo <ChaosInvocationResp>();

            if (invocationResp.DataTypeFullName == null)
            {
                return(null);
            }

            return(_chaosConverter.ToData(invocationResp));
        }
Пример #4
0
        private ChaosInvocationResp ToChaosInvocationResp(ChaosInvocation invocation,
                                                          object returnValue)
        {
            var invocationReply = new ChaosInvocationResp()
            {
                DataTypeFullName = invocation.ReturnTypeFullName
            };

            if (invocation.IsReturnTask)
            {
                var taskResult = returnValue.GetTaskResult();
                invocationReply.Data = _serializer.Serialize(taskResult.value);
            }
            else
            {
                invocationReply.Data = _serializer.Serialize(returnValue);
            }

            return(invocationReply);
        }
Пример #5
0
        private bool IsMatchMethod(MethodInfo methodInfo, ChaosInvocation request)
        {
            if (methodInfo.Name != request.MethodName)
            {
                return(false);
            }

            var parameterInfos = methodInfo.GetParameters();

            if (parameterInfos.Length != request.Parameters.Count)
            {
                return(false);
            }

            var isAllSame = parameterInfos.Zip(request.Parameters.Select(x => _typeFinder.Find(x.ParameterType)),
                                               (x, y) => x.ParameterType == y)
                            .All(x => x == true);

            return(isAllSame);
        }
Пример #6
0
        public object Call(ChaosInvocation chaosInvocation)
        {
            var promise = new PromiseInvocation()
            {
                Invocation = chaosInvocation
            };

            Publish(promise);

            if (!promise.Wait(_config.WaitTimeout))
            {
                throw new TimeoutException();
            }

            if (promise.Exception != null)
            {
                throw promise.Exception;
            }

            return(promise.Result);
        }
Пример #7
0
        public object Send(ChaosInvocation invocation)
        {
            var data = _binarySerializer.Serialize(invocation);

            object resp = null;

            lock (_lock)
            {
                _singal = new AutoResetEvent(false);
                _client.Send(data);

                if (!_singal.WaitOne(_config.Value.WaitTimeout))
                {
                    throw new TimeoutException();
                }

                resp = _chaosConverter.ToData(_reply);
            }

            return(resp);
        }
Пример #8
0
        private (MethodInfo MethodInfo, Func <object, object[], object> Func) FindMethod(ReflectionClass clazz,
                                                                                         ChaosInvocation request)
        {
            foreach (var method in clazz.AllMethods)
            {
                if (!IsMatchMethod(method.MethodInfo, request))
                {
                    continue;
                }

                return(method.MethodInfo, method.Func);
            }

            foreach (var method in clazz.AllGenericMethods)
            {
                if (!IsMatchMethod(method.MethodInfo, request))
                {
                    continue;
                }

                var genericTypes = request.GenericTypes
                                   .Select(x => _typeFinder.Find(x.ParameterType))
                                   .ToArray();

                return(method.MethodInfo, method.GetFunc(genericTypes));
            }

            throw new EntryPointNotFoundException(request.MethodName);
        }