Beispiel #1
0
        public void CreateResponsePropertiesCopiesAllProperties()
        {
            KissLog.Http.HttpResponse httpResponse = CommonTestHelpers.Factory.CreateHttpResponse();
            var result = PayloadFactory.Create(httpResponse);

            TestResponseProperties(httpResponse, result);
        }
Beispiel #2
0
        public Models.HealthStatus HealthCheck()
        {
            if (!InitialContextSet)
            {
                InitializeContext();
            }

            byte[] payload = PayloadFactory.BuildHealthCheckPayload(
                DateTime.Now.ToString(), new HealthStatusRequestParams {
                Context = Context
            });

            string invocationResponse = Client.Invoke(payload);

            System.Diagnostics.Debug.WriteLine("HealthCheck[invocationResponse]: " + invocationResponse);

            JsonRpcResponse <HealthStatus> rpcResponse =
                JsonConvert.DeserializeObject <JsonRpcResponse <HealthStatus> >(invocationResponse);

            return(new Models.HealthStatus
            {
                Proxy = new Models.ComponentHealth(rpcResponse.Result.Proxy),
                Smaers = new Models.ComponentHealth(rpcResponse.Result.Smaers),
                Backend = new Models.ComponentHealth(rpcResponse.Result.backend)
            });
        }
Beispiel #3
0
        T IPayloadCache.GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
        {
            try
            {
                IPayloadValue <T> payload = null;

                IPayloadCollection currentCollection;
                do
                {
                    T existingValue;
                    if (_collection.TryGetPayload(out existingValue))
                    {
                        return(existingValue);
                    }

                    IPayloadValue <T> contextProperty = payload ?? (payload = new PayloadValue <T>(payloadFactory()));

                    currentCollection = Volatile.Read(ref _collection);

                    Interlocked.CompareExchange(ref _collection, currentCollection.Add(contextProperty), currentCollection);
                }while (currentCollection == Volatile.Read(ref _collection));

                return(payload.Value);
            }
            catch (Exception exception)
            {
                throw new PayloadFactoryException($"The payload factory faulted: {TypeCache<T>.ShortName}", exception);
            }
        }
        public void OnExceptionIsExecutedWhenApiResultHasException()
        {
            Logger logger = new Logger();

            ExceptionArgs exceptionArgs = null;

            FlushLogArgs            flushArgs = FlushLogArgsFactory.Create(new[] { logger });
            CreateRequestLogRequest request   = PayloadFactory.Create(flushArgs);

            var kisslogApi = new Mock <IPublicApi>();

            kisslogApi.Setup(p => p.CreateRequestLog(It.IsAny <CreateRequestLogRequest>(), It.IsAny <IEnumerable <File> >()))
            .Returns(new ApiResult <RequestLog>
            {
                Exception = new ApiException
                {
                    ErrorMessage = $"Error {Guid.NewGuid()}"
                }
            });

            FlushOptions options = new FlushOptions
            {
                UseAsync    = false,
                OnException = (ExceptionArgs args) =>
                {
                    exceptionArgs = args;
                }
            };

            Flusher.FlushAsync(options, kisslogApi.Object, flushArgs, request).ConfigureAwait(false);

            Assert.IsNotNull(exceptionArgs);
        }
        public T GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
            where T : class
        {
            lock (this)
            {
                if (_cache != null)
                {
                    for (int i = _cache.Count - 1; i >= 0; i--)
                    {
                        if (_cache[i] is T result)
                        {
                            return(result);
                        }
                    }
                }

                T payload = payloadFactory();

                if (_cache != null)
                {
                    _cache.Add(payload);
                }
                else
                {
                    _cache = new List <object>(1)
                    {
                        payload
                    }
                };

                return(payload);
            }
        }
Beispiel #6
0
        public virtual T AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
            where T : class
        {
            if (this is T context)
            {
                return(context);
            }

            if (PayloadCache.TryGetPayload <T>(out var payload))
            {
                return(PayloadCache.AddOrUpdatePayload(addFactory, updateFactory));
            }

            if (_context.TryGetPayload(out payload))
            {
                T Add()
                {
                    return(updateFactory(payload));
                }

                return(PayloadCache.AddOrUpdatePayload(Add, updateFactory));
            }

            return(PayloadCache.AddOrUpdatePayload(addFactory, updateFactory));
        }
        public void ChangesFlushLogArgsFiles()
        {
            Logger logger = new Logger();

            logger.LogAsFile("Content 1", "File1.txt");
            logger.LogAsFile("Content 2", "File2.txt");

            FlushLogArgs             flushArgs = FlushLogArgsFactory.Create(new[] { logger });
            IEnumerable <LoggedFile> files     = flushArgs.Files;

            CreateRequestLogRequest request = PayloadFactory.Create(flushArgs);

            var kisslogApi = new Mock <IPublicApi>();

            kisslogApi.Setup(p => p.CreateRequestLog(It.IsAny <CreateRequestLogRequest>(), It.IsAny <IEnumerable <File> >()))
            .Returns(new ApiResult <RequestLog>());

            Flusher.FlushAsync(new FlushOptions {
                UseAsync = false
            }, kisslogApi.Object, flushArgs, request).ConfigureAwait(false);

            Assert.AreNotSame(files, flushArgs.Files);
            Assert.AreEqual(files.Count(), flushArgs.Files.Count());

            logger.Reset();
        }
Beispiel #8
0
        public void CreateRequestPropertiesCopiesAllProperties()
        {
            KissLog.Http.RequestProperties requestProperties = CommonTestHelpers.Factory.CreateRequestProperties();
            var result = PayloadFactory.Create(requestProperties);

            TestRequestProperties(requestProperties, result);
        }
Beispiel #9
0
        public void CreateExceptionCopiesAllProperties()
        {
            CapturedException exception = CommonTestHelpers.Factory.CreateCapturedException();
            var result = PayloadFactory.Create(exception);

            TestCapturedException(exception, result);
        }
Beispiel #10
0
        public TPayload GetOrAddPayload <TPayload>(PayloadFactory <TPayload> payloadFactory)
            where TPayload : class
        {
            CachedPayload cachedPayload;

            if (_cache.TryGetValue(typeof(TPayload), out cachedPayload))
            {
                return(cachedPayload.GetPayload <TPayload>());
            }

            try
            {
                cachedPayload = _cache.GetOrAdd(typeof(TPayload), x =>
                {
                    TPayload payload = payloadFactory();
                    if (payload != default(TPayload))
                    {
                        return(new CachedPayload <TPayload>(payload));
                    }

                    throw new PayloadNotFoundException("The payload was not found: " + typeof(TPayload).Name);
                });

                return(cachedPayload.GetPayload <TPayload>());
            }
            catch (PayloadNotFoundException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new PayloadFactoryException("The payload factory faulted: " + typeof(TPayload).Name, ex);
            }
        }
Beispiel #11
0
        public void CreateCopiesAllProperties()
        {
            FlushLogArgs args        = CommonTestHelpers.Factory.CreateFlushLogArgs();
            var          logMessages = args.MessagesGroups.SelectMany(p => p.Messages).OrderBy(p => p.DateTime).ToList();

            var result = PayloadFactory.Create(args);

            Assert.IsNotNull(result.SdkName);
            Assert.IsNotNull(result.SdkVersion);
            Assert.AreEqual(args.HttpProperties.Request.StartDateTime, result.StartDateTime);
            Assert.IsTrue(result.DurationInMilliseconds >= 0);
            Assert.AreEqual(args.HttpProperties.Request.MachineName, result.MachineName);
            Assert.AreEqual(args.HttpProperties.Request.IsNewSession, result.IsNewSession);
            Assert.AreEqual(args.HttpProperties.Request.SessionId, result.SessionId);
            Assert.AreEqual(args.HttpProperties.Request.IsAuthenticated, result.IsAuthenticated);
            Assert.IsNull(result.User);
            Assert.AreEqual(logMessages.Count, result.LogMessages.Count());
            Assert.AreEqual(args.Exceptions.Count(), result.Exceptions.Count());
            Assert.AreEqual(args.CustomProperties.Count(), result.CustomProperties.Count);

            for (int i = 0; i < logMessages.Count; i++)
            {
                TestLogMessage(logMessages[i], result.LogMessages.ElementAt(i));
            }

            for (int i = 0; i < args.Exceptions.Count(); i++)
            {
                TestCapturedException(args.Exceptions.ElementAt(i), result.Exceptions.ElementAt(i));
            }
        }
Beispiel #12
0
        public void CreateUriCopiesAllTheProperties()
        {
            Uri uri    = new Uri("http://application/path1/path2?param1=value&param2=anotherValue");
            Url result = PayloadFactory.Create(uri);

            TestUrl(uri, result);
        }
Beispiel #13
0
        public Models.Version Version()
        {
            byte[] payload = PayloadFactory
                             .BuildGetVersionPayload(DateTime.Now.ToString());

            string invocationResponse = Client.Invoke(payload);

            System.Diagnostics.Debug.WriteLine("Version[invocationResponse]: " + invocationResponse);

            JsonRpcResponse <VersionData> rpcResponse =
                JsonConvert.DeserializeObject <JsonRpcResponse <VersionData> >(invocationResponse);

            ThrowOnError(rpcResponse);

            VersionData versions = rpcResponse.Result;

            Models.Version version = new Models.Version
            {
                ClientVersion = new Models.ComponentVersion
                {
                    Version    = versions.ClientVersion.Version,
                    CommitHash = versions.ClientVersion.CommitHash,
                    SourceHash = versions.ClientVersion.SourceHash
                },
                SmaersVersion = new Models.ComponentVersion
                {
                    Version    = versions.SmaersVersion.Version,
                    CommitHash = versions.SmaersVersion.CommitHash,
                    SourceHash = versions.SmaersVersion.SourceHash
                },
                SdkVersion = Constants.SDK_VERSION
            };

            return(version);
        }
        T PipeContext.AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
        {
            if (_right is T context)
            {
                return(context);
            }

            return(_left.AddOrUpdatePayload(addFactory, updateFactory));
        }
        T PipeContext.GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
        {
            if (this is T context)
            {
                return(context);
            }

            return(_context.GetOrAddPayload(payloadFactory));
        }
Beispiel #16
0
        public void CreateLogMessageCopiesAllProperties()
        {
            DateTime startRequestDateTime = DateTime.UtcNow.AddSeconds(-5);

            LogMessage message = CommonTestHelpers.Factory.CreateLogMessage();
            var        result  = PayloadFactory.Create(message, startRequestDateTime);

            TestLogMessage(message, result);
        }
        /// <summary>
        /// Either adds a new payload, or updates an existing payload
        /// </summary>
        /// <param name="addFactory">The payload factory called if the payload is not present</param>
        /// <param name="updateFactory">The payload factory called if the payload already exists</param>
        /// <typeparam name="T">The payload type</typeparam>
        /// <returns></returns>
        public override T AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
        {
            if (this is T context)
            {
                return(context);
            }

            return(ReceiveContext.AddOrUpdatePayload(addFactory, updateFactory));
        }
Beispiel #18
0
        T PipeContext.AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
        {
            if (_payloadCache != null)
            {
                return(_payloadCache.AddOrUpdatePayload(addFactory, updateFactory));
            }

            return(_context.AddOrUpdatePayload(addFactory, updateFactory));
        }
        /// <summary>
        /// Get or add a payload to the context, using the provided payload factory.
        /// </summary>
        /// <param name="payloadFactory">The payload factory, which is only invoked if the payload is not present.</param>
        /// <typeparam name="T">The payload type</typeparam>
        /// <returns></returns>
        public override T GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
        {
            if (this is T context)
            {
                return(context);
            }

            return(ReceiveContext.GetOrAddPayload(payloadFactory));
        }
Beispiel #20
0
        TPayload PipeContext.GetOrAddPayload <TPayload>(PayloadFactory <TPayload> payloadFactory)
        {
            if (_payloadCache != null)
            {
                return(_payloadCache.GetOrAddPayload(payloadFactory));
            }

            return(_context.GetOrAddPayload(payloadFactory));
        }
        T PipeContext.GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
        {
            if (_right is T context)
            {
                return(context);
            }

            return(_left.GetOrAddPayload(payloadFactory));
        }
Beispiel #22
0
 private static void OnNewBoardState(BoardState boardState)
 {
     if (!_sentGameStart)
     {
         SendUpdate(PayloadFactory.GameStart(boardState));
         _sentGameStart = true;
     }
     SendUpdate(PayloadFactory.BoardState(boardState));
 }
Beispiel #23
0
        /// <summary>
        /// Either adds a new payload, or updates an existing payload
        /// </summary>
        /// <param name="addFactory">The payload factory called if the payload is not present</param>
        /// <param name="updateFactory">The payload factory called if the payload already exists</param>
        /// <typeparam name="T">The payload type</typeparam>
        /// <returns></returns>
        public virtual T AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
            where T : class
        {
            if (this is T context)
            {
                return(context);
            }

            return(PayloadCache.AddOrUpdatePayload(addFactory, updateFactory));
        }
Beispiel #24
0
        /// <summary>
        /// Get or add a payload to the context, using the provided payload factory.
        /// </summary>
        /// <param name="payloadFactory">The payload factory, which is only invoked if the payload is not present.</param>
        /// <typeparam name="T">The payload type</typeparam>
        /// <returns></returns>
        public virtual T GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
            where T : class
        {
            if (this is T context)
            {
                return(context);
            }

            return(PayloadCache.GetOrAddPayload(payloadFactory));
        }
Beispiel #25
0
 public static void Stop()
 {
     if (!_running)
     {
         return;
     }
     BoardStateWatcher.Stop();
     SendUpdate(PayloadFactory.GameEnd());
     _running = false;
 }
Beispiel #26
0
        T PipeContext.GetOrAddPayload <T>(PayloadFactory <T> payloadFactory)
        {
            var context = _sourceContext as T;

            if (context != null)
            {
                return(context);
            }

            return(_context.GetOrAddPayload(payloadFactory));
        }
Beispiel #27
0
        /// <summary>
        /// Get or add a payload to the context, using the provided payload factory.
        /// </summary>
        /// <param name="payloadFactory">The payload factory, which is only invoked if the payload is not present.</param>
        /// <typeparam name="TPayload">The payload type</typeparam>
        /// <returns></returns>
        public virtual TPayload GetOrAddPayload <TPayload>(PayloadFactory <TPayload> payloadFactory)
            where TPayload : class
        {
            var context = this as TPayload;

            if (context != null)
            {
                return(context);
            }

            return(_payloadCache.GetOrAddPayload(payloadFactory));
        }
Beispiel #28
0
        T PipeContext.AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
        {
            // can't modify implicit payload types
            var context = _sourceContext as T;

            if (context != null)
            {
                return(context);
            }

            return(_context.AddOrUpdatePayload(addFactory, updateFactory));
        }
Beispiel #29
0
        public void CreateHttpPropertiesCopiesAllProperties()
        {
            KissLog.Http.HttpProperties httpProperties = CommonTestHelpers.Factory.CreateHttpProperties();
            var result = PayloadFactory.Create(httpProperties);

            TestUrl(httpProperties.Request.Url, result.Url);
            Assert.AreEqual(httpProperties.Request.UserAgent, result.UserAgent);
            Assert.AreEqual(httpProperties.Request.HttpMethod, result.HttpMethod);
            Assert.AreEqual(httpProperties.Request.HttpReferer, result.HttpReferer);
            Assert.AreEqual(httpProperties.Request.RemoteAddress, result.RemoteAddress);
            TestRequestProperties(httpProperties.Request.Properties, result.Request);
            TestResponseProperties(httpProperties.Response, result.Response);
        }
        /// <summary>
        /// Either adds a new payload, or updates an existing payload
        /// </summary>
        /// <param name="addFactory">The payload factory called if the payload is not present</param>
        /// <param name="updateFactory">The payload factory called if the payload already exists</param>
        /// <typeparam name="T">The payload type</typeparam>
        /// <returns></returns>
        public virtual T AddOrUpdatePayload <T>(PayloadFactory <T> addFactory, UpdatePayloadFactory <T> updateFactory)
            where T : class
        {
            // can't modify implicit payload types
            var context = this as T;

            if (context != null)
            {
                return(context);
            }

            return(PayloadCache.AddOrUpdatePayload(addFactory, updateFactory));
        }
Beispiel #31
0
 public RequestFactory(IMapFromOneTypeToAnother mapping_gateway, PayloadFactory payload_factory)
 {
   this.mapping_gateway = mapping_gateway;
   this.payload_factory = payload_factory;
 }