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 System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace StackExchange.Redis.Tests
{
......
......@@ -12,10 +12,10 @@ namespace StackExchange.Redis.Tests
public class SSL : TestBase
{
[Test]
[TestCase(6379, false, false)]
[TestCase(6380, true, false)]
[TestCase(6380, true, true)]
public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost)
[TestCase(false, false)]
[TestCase(true, false)]
[TestCase(true, true)]
public void ConnectToSSLServer(bool useSsl, bool specifyHost)
{
string host = null;
......@@ -32,7 +32,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost)
{ "cluster", null }
}
),
EndPoints = { { host, port} },
EndPoints = { { host } },
AllowAdmin = true,
SyncTimeout = Debugger.IsAttached ? int.MaxValue : 5000
};
......@@ -66,7 +66,7 @@ public void ConnectToSSLServer(int port, bool useSsl, bool specifyHost)
muxer.InternalError += OnInternalError;
var db = muxer.GetDatabase();
db.Ping();
using (var file = File.Create("ssl" + port + ".zip"))
using (var file = File.Create("ssl-" + useSsl + "-" + specifyHost + ".zip"))
{
muxer.ExportConfiguration(file);
}
......
......@@ -67,6 +67,7 @@
<Compile Include="Commands.cs" />
<Compile Include="ConnectionShutdown.cs" />
<Compile Include="Databases.cs" />
<Compile Include="DefaultPorts.cs" />
<Compile Include="Expiry.cs" />
<Compile Include="Config.cs" />
<Compile Include="FloatingPoint.cs" />
......
......@@ -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>
/// Returns the effective configuration string for this configuration
/// </summary>
......
......@@ -695,6 +695,7 @@ static ConnectionMultiplexer CreateMultiplexer(object configuration)
throw new ArgumentException("configuration");
}
if (config.EndPoints.Count == 0) throw new ArgumentException("No endpoints specified", "configuration");
config.SetDefaultPorts();
return new ConnectionMultiplexer(config);
}
/// <summary>
......
......@@ -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");
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;
}
}
}
}
}
}
......@@ -67,17 +67,19 @@ internal static string ToString(object value)
internal static string ToString(EndPoint endpoint)
{
if (endpoint == null) return "";
var dns = endpoint as DnsEndPoint;
if (dns == null)
if (dns != null)
{
return endpoint.ToString();
}
else
{ // DnsEndPoint includes the family-type in
// ToString(), but we don't want that
if (dns.Port == 0) return dns.Host;
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)
{
......@@ -152,7 +154,7 @@ internal static EndPoint TryParseEndPoint(string endpoint)
if (i < 0)
{
host = endpoint;
port = 6379;
port = 0;
}
else
{
......
......@@ -55,7 +55,7 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint)
slaveReadOnly = true;
isSlave = false;
databases = 0;
writeEverySeconds = config.KeepAlive;
writeEverySeconds = config.KeepAlive > 0 ? config.KeepAlive : 60;
interactive = CreateBridge(ConnectionType.Interactive);
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