Commit 0bf16924 authored by Marc Gravell's avatar Marc Gravell

Default port: 6379 normally, 6380 over ssl

parent d0880697
using NUnit.Framework;
using System.Linq;
using System.Net;
namespace StackExchange.Redis.Tests
{
[TestFixture]
public class DefaultPorts
{
[Test]
[TestCase("foo", 6379)]
[TestCase("foo:6379", 6379)]
[TestCase("foo:6380", 6380)]
[TestCase("foo,ssl=false", 6379)]
[TestCase("foo:6379,ssl=false", 6379)]
[TestCase("foo:6380,ssl=false", 6380)]
[TestCase("foo,ssl=true", 6380)]
[TestCase("foo:6379,ssl=true", 6379)]
[TestCase("foo:6380,ssl=true", 6380)]
[TestCase("foo:6381,ssl=true", 6381)]
public void ConfigStringRoundTripWithDefaultPorts(string config, int expectedPort)
{
var options = ConfigurationOptions.Parse(config);
string backAgain = config.ToString();
Assert.AreEqual(config, backAgain);
options.SetDefaultPorts(); // normally it is the multiplexer that calls this, not us
Assert.AreEqual(expectedPort, ((DnsEndPoint)options.EndPoints.Single()).Port);
}
[Test]
[TestCase("foo", 0, false, 6379)]
[TestCase("foo", 6379, false, 6379)]
[TestCase("foo", 6380, false, 6380)]
[TestCase("foo", 0, true, 6380)]
[TestCase("foo", 6379, true, 6379)]
[TestCase("foo", 6380, true, 6380)]
[TestCase("foo", 6381, true, 6381)]
public void ConfigManualWithDefaultPorts(string host, int port, bool useSsl, int expectedPort)
{
var options = new ConfigurationOptions();
if(port == 0)
{
options.EndPoints.Add(host);
} else
{
options.EndPoints.Add(host, port);
}
if (useSsl) options.UseSsl = true;
options.SetDefaultPorts(); // normally it is the multiplexer that calls this, not us
Assert.AreEqual(expectedPort, ((DnsEndPoint)options.EndPoints.Single()).Port);
}
}
}
using System; using NUnit.Framework;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace StackExchange.Redis.Tests namespace StackExchange.Redis.Tests
{ {
......
...@@ -12,10 +12,10 @@ namespace StackExchange.Redis.Tests ...@@ -12,10 +12,10 @@ namespace StackExchange.Redis.Tests
public class SSL : TestBase public class SSL : TestBase
{ {
[Test] [Test]
[TestCase(6379, false, false)] [TestCase(false, false)]
[TestCase(6380, true, false)] [TestCase(true, false)]
[TestCase(6380, true, true)] [TestCase(true, true)]
public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost) public void ConnectToSSLServer(bool useSsl, bool specifyHost)
{ {
string host = null; string host = null;
...@@ -32,7 +32,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost) ...@@ -32,7 +32,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost)
{ "cluster", null } { "cluster", null }
} }
), ),
EndPoints = { { host, port} }, EndPoints = { { host } },
AllowAdmin = true, AllowAdmin = true,
SyncTimeout = Debugger.IsAttached ? int.MaxValue : 5000 SyncTimeout = Debugger.IsAttached ? int.MaxValue : 5000
}; };
...@@ -66,7 +66,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost) ...@@ -66,7 +66,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost)
muxer.InternalError += OnInternalError; muxer.InternalError += OnInternalError;
var db = muxer.GetDatabase(); var db = muxer.GetDatabase();
db.Ping(); db.Ping();
using (var file = File.Create("ssl" + port + ".zip")) using (var file = File.Create("ssl-" + useSsl + "-" + specifyHost + ".zip"))
{ {
muxer.ExportConfiguration(file); muxer.ExportConfiguration(file);
} }
......
...@@ -67,6 +67,7 @@ ...@@ -67,6 +67,7 @@
<Compile Include="Commands.cs" /> <Compile Include="Commands.cs" />
<Compile Include="ConnectionShutdown.cs" /> <Compile Include="ConnectionShutdown.cs" />
<Compile Include="Databases.cs" /> <Compile Include="Databases.cs" />
<Compile Include="DefaultPorts.cs" />
<Compile Include="Expiry.cs" /> <Compile Include="Expiry.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="FloatingPoint.cs" /> <Compile Include="FloatingPoint.cs" />
......
...@@ -231,6 +231,14 @@ public ConfigurationOptions Clone() ...@@ -231,6 +231,14 @@ public ConfigurationOptions Clone()
} }
/// <summary>
/// Resolve the default port for any endpoints that did not have a port explicitly specified
/// </summary>
public void SetDefaultPorts()
{
endpoints.SetDefaultPorts(UseSsl ? 6380 : 6379);
}
/// <summary> /// <summary>
/// Returns the effective configuration string for this configuration /// Returns the effective configuration string for this configuration
/// </summary> /// </summary>
......
...@@ -695,6 +695,7 @@ static ConnectionMultiplexer CreateMultiplexer(object configuration) ...@@ -695,6 +695,7 @@ static ConnectionMultiplexer CreateMultiplexer(object configuration)
throw new ArgumentException("configuration"); throw new ArgumentException("configuration");
} }
if (config.EndPoints.Count == 0) throw new ArgumentException("No endpoints specified", "configuration"); if (config.EndPoints.Count == 0) throw new ArgumentException("No endpoints specified", "configuration");
config.SetDefaultPorts();
return new ConnectionMultiplexer(config); return new ConnectionMultiplexer(config);
} }
/// <summary> /// <summary>
......
...@@ -71,6 +71,31 @@ protected override void SetItem(int index, EndPoint item) ...@@ -71,6 +71,31 @@ protected override void SetItem(int index, EndPoint item)
if (existingIndex >= 0 && existingIndex != index) throw new ArgumentException("EndPoints must be unique", "item"); if (existingIndex >= 0 && existingIndex != index) throw new ArgumentException("EndPoints must be unique", "item");
base.SetItem(index, item); base.SetItem(index, item);
} }
}
internal void SetDefaultPorts(int defaultPort)
{
for (int i = 0; i < Count; i++)
{
var endpoint = this[i];
var dns = endpoint as DnsEndPoint;
if (dns != null)
{
if (dns.Port == 0)
{
this[i] = new DnsEndPoint(dns.Host, defaultPort, dns.AddressFamily);
continue;
}
}
var ip = endpoint as IPEndPoint;
if (ip != null)
{
if (ip.Port == 0)
{
this[i] = new IPEndPoint(ip.Address, defaultPort);
continue;
}
}
}
}
}
} }
...@@ -66,18 +66,20 @@ internal static string ToString(object value) ...@@ -66,18 +66,20 @@ internal static string ToString(object value)
} }
internal static string ToString(EndPoint endpoint) internal static string ToString(EndPoint endpoint)
{ {
if (endpoint == null) return "";
var dns = endpoint as DnsEndPoint; var dns = endpoint as DnsEndPoint;
if (dns == null) if (dns != null)
{ {
return endpoint.ToString(); if (dns.Port == 0) return dns.Host;
}
else
{ // DnsEndPoint includes the family-type in
// ToString(), but we don't want that
return dns.Host + ":" + Format.ToString(dns.Port); return dns.Host + ":" + Format.ToString(dns.Port);
} }
var ip = endpoint as IPEndPoint;
if (ip != null)
{
if (ip.Port == 0) return ip.Address.ToString();
return ip.Address.ToString() + ":" + Format.ToString(ip.Port);
}
return endpoint == null ? "" : endpoint.ToString();
} }
internal static string ToStringHostOnly(EndPoint endpoint) internal static string ToStringHostOnly(EndPoint endpoint)
{ {
...@@ -152,7 +154,7 @@ internal static EndPoint TryParseEndPoint(string endpoint) ...@@ -152,7 +154,7 @@ internal static EndPoint TryParseEndPoint(string endpoint)
if (i < 0) if (i < 0)
{ {
host = endpoint; host = endpoint;
port = 6379; port = 0;
} }
else else
{ {
......
...@@ -55,7 +55,7 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint) ...@@ -55,7 +55,7 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint)
slaveReadOnly = true; slaveReadOnly = true;
isSlave = false; isSlave = false;
databases = 0; databases = 0;
writeEverySeconds = config.KeepAlive; writeEverySeconds = config.KeepAlive > 0 ? config.KeepAlive : 60;
interactive = CreateBridge(ConnectionType.Interactive); interactive = CreateBridge(ConnectionType.Interactive);
serverType = ServerType.Standalone; serverType = ServerType.Standalone;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment