Пример #1
0
        public void Options_CanUseInetUtils()
        {
            // arrange
            var mockNetUtils = new Mock <InetUtils>(null, null);

            mockNetUtils.Setup(n => n.FindFirstNonLoopbackHostInfo()).Returns(new HostInfo()
            {
                Hostname = "FromMock", IpAddress = "254.254.254.254"
            }).Verifiable();
            var appSettings = new Dictionary <string, string> {
                { "consul:discovery:UseNetUtils", "true" }
            };
            var config = new ConfigurationBuilder().AddInMemoryCollection(appSettings).Build();
            var opts   = new ConsulDiscoveryOptions()
            {
                NetUtils = mockNetUtils.Object
            };

            config.GetSection(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX).Bind(opts);

            // act
            opts.ApplyNetUtils();

            // assert
            Assert.Equal("FromMock", opts.HostName);
            Assert.Equal("254.254.254.254", opts.IpAddress);
            mockNetUtils.Verify(n => n.FindFirstNonLoopbackHostInfo(), Times.Once);
        }
Пример #2
0
        public void Options_CanUseInetUtilsWithoutReverseDnsOnIP()
        {
            // arrange
            var noSlowReverseDNSQuery = new Stopwatch();

            noSlowReverseDNSQuery.Start();
            var appSettings = new Dictionary <string, string> {
                { "consul:discovery:UseNetUtils", "true" }, { "spring:cloud:inet:SkipReverseDnsLookup", "true" }
            };
            var config = new ConfigurationBuilder().AddInMemoryCollection(appSettings).Build();
            var opts   = new ConsulDiscoveryOptions()
            {
                NetUtils = new InetUtils(config.GetSection(InetOptions.PREFIX).Get <InetOptions>())
            };

            config.GetSection(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX).Bind(opts);

            // act
            opts.ApplyNetUtils();
            noSlowReverseDNSQuery.Stop();

            // assert
            Assert.NotNull(opts.HostName);
            Assert.InRange(noSlowReverseDNSQuery.ElapsedMilliseconds, 0, 1500); // testing with an actual reverse dns query results in around 5000 ms
        }
Пример #3
0
        public void Options_DontUseInetUtilsByDefault()
        {
            var mockNetUtils = new Mock <InetUtils>(null, null);

            mockNetUtils.Setup(n => n.FindFirstNonLoopbackHostInfo()).Returns(new HostInfo()
            {
                Hostname = "FromMock", IpAddress = "254.254.254.254"
            }).Verifiable();
            var config = new ConfigurationBuilder().Build();
            var opts   = new ConsulDiscoveryOptions()
            {
                NetUtils = mockNetUtils.Object
            };

            config.GetSection(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX).Bind(opts);

            opts.ApplyNetUtils();

            mockNetUtils.Verify(n => n.FindFirstNonLoopbackHostInfo(), Times.Never);
        }
Пример #4
0
 /// <summary>
 /// Perform post-configuration on ConsulDiscoveryOptions
 /// </summary>
 /// <param name="config">Application Configuration</param>
 /// <param name="options">ConsulDiscoveryOptions to configure</param>
 /// <param name="netOptions">Optional InetOptions</param>
 public static void UpdateDiscoveryOptions(IConfiguration config, ConsulDiscoveryOptions options, InetOptions netOptions)
 {
     options.NetUtils = new InetUtils(netOptions);
     options.ApplyNetUtils();
     options.ApplyConfigUrls(config.GetAspNetCoreUrls(), ConfigurationUrlHelpers.WILDCARD_HOST);
 }