Skip to content

Commit

Permalink
fix: refactored hostname provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbachnyi.v committed Dec 29, 2023
1 parent 6457632 commit 68f6b6f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
46 changes: 28 additions & 18 deletions Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Should_be_configurable()
.SetReleaseDate("released now")
.SetDependencies(new List<string> {"dep-a", "dep-b"})
.SetTags(new TagCollection {"tag1", {"tag2", "value"}})
.SetVpnHostnameProvider(() => "newHostname"),
.SetHostnameProvider(() => "newHostname"),
false);

info.ReplicaInfo.Environment.Should().Be("custom-environment");
Expand All @@ -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");

Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public interface IReplicaInfoBuilder
/// <para>Default value: <c>null</c></para>
/// <para>Should not be called in conjunction with <see cref="SetUrl"/>.</para>
/// </summary>
IReplicaInfoBuilder SetVpnHostnameProvider([CanBeNull] Func<string> vpnHostnameProvider);
IReplicaInfoBuilder SetHostnameProvider([CanBeNull] Func<string> vpnHostnameProvider);

/// <summary>
/// <para>Sets build commit hash.</para>
Expand Down
2 changes: 1 addition & 1 deletion Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetVpnHostnameProvider(System.Func<string> vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetHostnameProvider(System.Func<string> vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
10 changes: 5 additions & 5 deletions Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ReplicaInfoBuilder : IReplicaInfoBuilder
private List<string> dependencies;
private TagCollection tags;

private Func<string> vpnHostnameProvider;
private Func<string> hostnameProvider;

private ReplicaInfoBuilder(bool useFQDN)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -172,9 +172,9 @@ public IReplicaInfoBuilder SetUrlPath(string path)
return this;
}

public IReplicaInfoBuilder SetVpnHostnameProvider(Func<string> vpnHostnameProvider)
public IReplicaInfoBuilder SetHostnameProvider(Func<string> vpnHostnameProvider)
{
this.vpnHostnameProvider = vpnHostnameProvider;
this.hostnameProvider = vpnHostnameProvider;
return this;
}

Expand Down

0 comments on commit 68f6b6f

Please sign in to comment.