diff --git a/src/net-questdb-client-tests/MultiUrlHttpTests.cs b/src/net-questdb-client-tests/MultiUrlHttpTests.cs index 84dd00d..e5c1b04 100644 --- a/src/net-questdb-client-tests/MultiUrlHttpTests.cs +++ b/src/net-questdb-client-tests/MultiUrlHttpTests.cs @@ -45,22 +45,23 @@ public void ParseMultipleAddresses_FromConfigString() { // Test parsing multiple addresses from config string var options = - new SenderOptions("http::addr=localhost:9000;addr=localhost:9001;addr=localhost:9002;auto_flush=off;"); + new SenderOptions("http::addr=localhost:9001;aDdR=locALhOSt:9002;addr=localhost:9003;auto_flush=off;"); + Assert.That(options.addr, Is.EqualTo("localhost:9001")); Assert.That(options.AddressCount, Is.EqualTo(3)); - Assert.That(options.addresses[0], Is.EqualTo("localhost:9000")); - Assert.That(options.addresses[1], Is.EqualTo("localhost:9001")); - Assert.That(options.addresses[2], Is.EqualTo("localhost:9002")); + Assert.That(options.addresses[0], Is.EqualTo("localhost:9001")); + Assert.That(options.addresses[1], Is.EqualTo("locALhOSt:9002")); + Assert.That(options.addresses[2], Is.EqualTo("localhost:9003")); } [Test] public void ParseMultipleAddresses_DefaultsToSingleAddress() { // Test that single address is handled correctly - var options = new SenderOptions("http::addr=localhost:9000;auto_flush=off;"); + var options = new SenderOptions("http::addr=localhost:9999;auto_flush=off;"); Assert.That(options.AddressCount, Is.EqualTo(1)); - Assert.That(options.addresses[0], Is.EqualTo("localhost:9000")); + Assert.That(options.addresses[0], Is.EqualTo("localhost:9999")); } [Test] @@ -69,7 +70,8 @@ public void ParseMultipleAddresses_NoAddrSpecified() // Test that default address is used when none specified var options = new SenderOptions("http::auto_flush=off;"); - Assert.That(options.AddressCount, Is.GreaterThan(0)); + Assert.That(options.addr, Is.EqualTo("localhost:9000")); + Assert.That(options.AddressCount, Is.EqualTo(1)); Assert.That(options.addresses[0], Is.EqualTo("localhost:9000")); } diff --git a/src/net-questdb-client-tests/SenderOptionsTests.cs b/src/net-questdb-client-tests/SenderOptionsTests.cs index 92c68bb..44de162 100644 --- a/src/net-questdb-client-tests/SenderOptionsTests.cs +++ b/src/net-questdb-client-tests/SenderOptionsTests.cs @@ -44,8 +44,8 @@ public void NoPortProvided() public void BasicParse() { Assert.That( - new SenderOptions("http::addr=localhost:9000;").addr, - Is.EqualTo("localhost:9000")); + new SenderOptions("http::addr=localhost:9999;").addr, + Is.EqualTo("localhost:9999")); } [Test] @@ -59,10 +59,37 @@ public void CapitalCaseInValues() [Test] public void DuplicateKey() { - // duplicate keys are 'last writer wins' + // duplicate (non-addr) keys are 'last writer wins' Assert.That( - new SenderOptions("http::addr=localhost:9000;addr=localhost:9009;").addr, - Is.EqualTo("localhost:9009")); + new SenderOptions("http::auth_timeout=15000;auth_timeout=8000;").auth_timeout, + Is.EqualTo(TimeSpan.FromMilliseconds(8000))); + } + + [Test] + public void DefaultConstructorInit() + { + var options = new SenderOptions + { + addr = "192.168.0.218:9000", + }; + + Assert.That(options.addr, Is.EqualTo("192.168.0.218:9000")); + Assert.That(options.AddressCount, Is.EqualTo(1)); + Assert.That(options.addresses[0], Is.EqualTo("192.168.0.218:9000")); + } + + [Test] + public void SetGetAddr() + { + var options = new SenderOptions(); + + Assert.That(options.addr, Is.EqualTo("localhost:9000")); + + options.addr = "192.168.0.218:9000"; + + Assert.That(options.addr, Is.EqualTo("192.168.0.218:9000")); + Assert.That(options.AddressCount, Is.EqualTo(1)); + Assert.That(options.addresses[0], Is.EqualTo("192.168.0.218:9000")); } [Test] diff --git a/src/net-questdb-client/Utils/SenderOptions.cs b/src/net-questdb-client/Utils/SenderOptions.cs index af11799..50a9ee1 100644 --- a/src/net-questdb-client/Utils/SenderOptions.cs +++ b/src/net-questdb-client/Utils/SenderOptions.cs @@ -56,8 +56,8 @@ public record SenderOptions "pool_timeout", "tls_verify", "tls_roots", "tls_roots_password", "own_socket", "gzip", }; - private string _addr = "localhost:9000"; - private List _addresses = new(); + private const string DefaultAddress = "localhost:9000"; + private readonly List _addresses = new(); private TimeSpan _authTimeout = TimeSpan.FromMilliseconds(15000); private AutoFlushType _autoFlush = AutoFlushType.on; private int _autoFlushBytes = int.MaxValue; @@ -91,7 +91,7 @@ public record SenderOptions /// public SenderOptions() { - ParseAddresses(); + addr = DefaultAddress; } /// @@ -103,8 +103,6 @@ public SenderOptions(string confStr) ReadConfigStringIntoBuilder(confStr); ParseEnumWithDefault(nameof(protocol), "http", out _protocol); ParseEnumWithDefault(nameof(protocol_version), "auto", out _protocol_version); - ParseStringWithDefault(nameof(addr), "localhost:9000", out _addr!); - ParseAddresses(); ParseEnumWithDefault(nameof(auto_flush), "on", out _autoFlush); ParseIntThatMayBeOff(nameof(auto_flush_rows), IsHttp() ? "75000" : "600", out _autoFlushRows); ParseIntThatMayBeOff(nameof(auto_flush_bytes), int.MaxValue.ToString(), out _autoFlushBytes); @@ -161,8 +159,12 @@ public ProtocolVersion protocol_version /// public string addr { - get => _addr; - set => _addr = value; + get => _addresses[0]; + set + { + _addresses.Clear(); + _addresses.Add(value); + } } /// @@ -595,21 +597,25 @@ private void ReadConfigStringIntoBuilder(string confStr) // Parse addresses manually before using DbConnectionStringBuilder // because DbConnectionStringBuilder only keeps the last value for duplicate keys _addresses.Clear(); - foreach (var param in paramString.Split(';')) + foreach (var param in paramString.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)) { - if (string.IsNullOrWhiteSpace(param)) continue; + if (!param.StartsWith("addr", StringComparison.OrdinalIgnoreCase)) + { + continue; + } - var kvp = param.Split('='); - if (kvp.Length == 2 && kvp[0].Trim() == "addr") + var kvp = param.Split('=', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + if (kvp.Length == 2 && kvp[0].Equals("addr", StringComparison.OrdinalIgnoreCase)) { - var addrValue = kvp[1].Trim(); - if (!string.IsNullOrEmpty(addrValue)) - { - _addresses.Add(addrValue); - } + _addresses.Add(kvp[1]); } } + if (_addresses.Count == 0) + { + _addresses.Add(DefaultAddress); + } + _connectionStringBuilder = new DbConnectionStringBuilder { ConnectionString = paramString, @@ -704,15 +710,6 @@ private void VerifyCorrectKeysInConfigString() } } - private void ParseAddresses() - { - // If no addresses were parsed from config string, use the primary addr - if (_addresses.Count == 0) - { - _addresses.Add(_addr); - } - } - /// /// Construct a new from the current options. ///