Ejemplo n.º 1
0
        /// <summary>
        /// Creates an <see cref="IEndPointSource"/> from the specified host IP address or name and port.
        /// </summary>
        /// <param name="host">The host name of IP address of the StatsD server.</param>
        /// <param name="port">The port number to use for the end point.</param>
        /// <param name="endpointCacheDuration">The optional period of time to cache the end point value for.</param>
        /// <returns>
        /// The created instance of <see cref="IEndPointSource"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="host"/> is either <see langword="null"/> or the empty string.
        /// </exception>
        public static IEndPointSource MakeEndPointSource(string host, int port, TimeSpan?endpointCacheDuration)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentException("The StatsD host IP address or name is null or empty.", nameof(host));
            }

            IEndPointSource source;

            if (IPAddress.TryParse(host, out IPAddress address))
            {
                // If we were given an IP instead of a hostname,
                // we can happily keep it the life of this class
                var value = new IPEndPoint(address, port);
                source = new SimpleEndpointSource(value);
            }
            else
            {
                // We have a host name, so we use DNS lookup
                source = new DnsLookupIpEndpointSource(host, port);

                if (endpointCacheDuration.HasValue)
                {
                    source = new CachedEndpointSource(source, endpointCacheDuration.Value);
                }
            }

            return(source);
        }
Ejemplo n.º 2
0
        public static void CachedValueIsReturnedFromInner()
        {
            var mockInner = new Mock <IEndPointSource>();

            mockInner.Setup(x => x.GetEndpoint()).Returns(MakeTestIpEndPoint());

            var cachedEndpoint = new CachedEndpointSource(mockInner.Object, TimeSpan.FromMinutes(5));

            var value = cachedEndpoint.GetEndpoint();

            value.ShouldNotBeNull();
            value.ShouldBe(MakeTestIpEndPoint());

            mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(1));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an <see cref="IEndPointSource"/> from the specified <see cref="EndPoint"/>.
        /// </summary>
        /// <param name="endpoint">The <see cref="EndPoint"/> to create the end point source for.</param>
        /// <param name="endpointCacheDuration">The optional period of time to cache the end point value for.</param>
        /// <returns>
        /// The created instance of <see cref="IEndPointSource"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="endpoint"/> is <see langword="null"/>.
        /// </exception>
        public static IEndPointSource MakeEndPointSource(EndPoint endpoint, TimeSpan?endpointCacheDuration)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }

            IEndPointSource source = new SimpleEndpointSource(endpoint);

            if (endpointCacheDuration.HasValue)
            {
                source = new CachedEndpointSource(source, endpointCacheDuration.Value);
            }

            return(source);
        }
Ejemplo n.º 4
0
        public static async Task CachedValueIsReturnedAgainAfterExpiry()
        {
            var mockInner = new Mock <IEndPointSource>();

            mockInner.Setup(x => x.GetEndpoint()).Returns(MakeTestIpEndPoint());

            var cachedEndpoint = new CachedEndpointSource(mockInner.Object, TimeSpan.FromSeconds(1));

            var value1 = cachedEndpoint.GetEndpoint();
            var value2 = cachedEndpoint.GetEndpoint();

            await Task.Delay(1500);

            var value3 = cachedEndpoint.GetEndpoint();
            var value4 = cachedEndpoint.GetEndpoint();

            mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(2));
        }