From 68f6b6fa6f032c168710ed34b3a93c713635bf31 Mon Sep 17 00:00:00 2001 From: "kulbachnyi.v" Date: Fri, 29 Dec 2023 14:06:02 +0500 Subject: [PATCH] fix: refactored hostname provider --- .../ReplicaInfoBuilder_Tests.cs | 46 +++++++++++-------- .../IReplicaInfoBuilder.cs | 2 +- .../PublicAPI.Unshipped.txt | 2 +- Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs | 10 ++-- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs b/Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs index 815333c..fef0da7 100644 --- a/Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs +++ b/Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs @@ -64,7 +64,7 @@ public void Should_be_configurable() .SetReleaseDate("released now") .SetDependencies(new List {"dep-a", "dep-b"}) .SetTags(new TagCollection {"tag1", {"tag2", "value"}}) - .SetVpnHostnameProvider(() => "newHostname"), + .SetHostnameProvider(() => "newHostname"), false); info.ReplicaInfo.Environment.Should().Be("custom-environment"); @@ -88,21 +88,19 @@ public void Should_be_configurable() properties[ReplicaInfoKeys.Host].Should().Be("newHostname"); } - [TestCase(null)] - [TestCase("newhostname")] - [TestCase("")] - public void Should_build_url_from_parts(string hostname) + [Test] + public void Should_build_url_from_parts() { var info = ReplicaInfoBuilder.Build( setup => setup .SetScheme("https") .SetPort(123) .SetUrlPath("vostok") - .SetVpnHostnameProvider(() => hostname), + .SetHostnameProvider(() => "testhost"), false) .ReplicaInfo; - var host = string.IsNullOrEmpty(hostname) ? EnvironmentInfo.Host.ToLowerInvariant() : hostname; + var host = "testhost"; info.Replica.Should().Be($"https://{host}:123/vostok"); @@ -204,25 +202,37 @@ public void Should_not_configure_hostname_when_uri_set() var info = ReplicaInfoBuilder.Build( setup => setup .SetUrl(url) - .SetVpnHostnameProvider(() => "newHostname"), + .SetHostnameProvider(() => "newHostname"), false ); info.ReplicaInfo.Replica.Should().Be("https://github.com:123/vostok"); } - [Test] - public void Should_configure_hostname_when_uri_not_set() + [TestCase(null)] + [TestCase("newHostname")] + [TestCase("")] + public void Should_correctly_build_url_with_hostname_provider(string hostname) { var info = ReplicaInfoBuilder.Build( - setup => setup - .SetUrl(null) - .SetPort(123) - .SetVpnHostnameProvider(() => "newhostname"), - false - ); - - info.ReplicaInfo.Replica.Should().Be("http://newhostname:123/"); + setup => setup + .SetScheme("https") + .SetPort(123) + .SetHostnameProvider(() => hostname), + false) + .ReplicaInfo; + + var expectedHostname = string.IsNullOrEmpty(hostname) ? EnvironmentInfo.Host : hostname; + var host = expectedHostname.ToLowerInvariant(); + + info.Replica.Should().Be($"https://{host}:123/"); + + var properties = info.Properties; + + properties[ReplicaInfoKeys.Replica].Should().Be($"https://{host}:123/"); + + properties[ReplicaInfoKeys.Url].Should().BeEquivalentTo($"https://{host}:123/"); + properties[ReplicaInfoKeys.Host].Should().Be(expectedHostname); } } } \ No newline at end of file diff --git a/Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs b/Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs index d942798..bd559cf 100644 --- a/Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs +++ b/Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs @@ -74,7 +74,7 @@ public interface IReplicaInfoBuilder /// Default value: null /// Should not be called in conjunction with . /// - IReplicaInfoBuilder SetVpnHostnameProvider([CanBeNull] Func vpnHostnameProvider); + IReplicaInfoBuilder SetHostnameProvider([CanBeNull] Func vpnHostnameProvider); /// /// Sets build commit hash. diff --git a/Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt b/Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt index 58e6860..69cf9fd 100644 --- a/Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt +++ b/Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt @@ -1 +1 @@ -Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetVpnHostnameProvider(System.Func vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder \ No newline at end of file +Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetHostnameProvider(System.Func vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder \ No newline at end of file diff --git a/Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs b/Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs index 43eadbb..3e9c574 100644 --- a/Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs +++ b/Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs @@ -34,7 +34,7 @@ internal class ReplicaInfoBuilder : IReplicaInfoBuilder private List dependencies; private TagCollection tags; - private Func vpnHostnameProvider; + private Func hostnameProvider; private ReplicaInfoBuilder(bool useFQDN) { @@ -59,14 +59,14 @@ public static ServiceBeaconInfo Build(ReplicaInfoSetup setup, bool useFQDN) public ServiceBeaconInfo Build() { - var vpnHostname = vpnHostnameProvider.Invoke(); + var vpnHostname = hostnameProvider?.Invoke(); if (!string.IsNullOrEmpty(vpnHostname)) { host = vpnHostname; } url ??= BuildUrl(); - replica ??= url.ToString(); + replica ??= url?.ToString() ?? $"{host}({EnvironmentInfo.ProcessId})"; if (url != null) { @@ -172,9 +172,9 @@ public IReplicaInfoBuilder SetUrlPath(string path) return this; } - public IReplicaInfoBuilder SetVpnHostnameProvider(Func vpnHostnameProvider) + public IReplicaInfoBuilder SetHostnameProvider(Func vpnHostnameProvider) { - this.vpnHostnameProvider = vpnHostnameProvider; + this.hostnameProvider = vpnHostnameProvider; return this; }