Commit 6218102c authored by Marc Gravell's avatar Marc Gravell

SO22783038

parent 588c921f
......@@ -6,6 +6,7 @@
using System.Threading.Tasks;
using System.IO;
using StackExchange.Redis;
using System.Linq;
namespace Tests
{
......@@ -123,18 +124,57 @@ public void CanOpenSecuredConnection()
}
}
[Test, ExpectedException(typeof(SocketException))]
[Test, ExpectedException(typeof(RedisConnectionException))]
public void CanNotOpenNonsenseConnection_IP()
{
using (var conn = ConnectionMultiplexer.Connect(Config.LocalHost + ":6500"))
{
var log = new StringWriter();
try {
using (var conn = ConnectionMultiplexer.Connect(Config.LocalHost + ":6500")) { }
} finally {
Console.WriteLine(log);
}
}
[Test, ExpectedException(typeof(SocketException))]
[Test, ExpectedException(typeof(RedisConnectionException))]
public void CanNotOpenNonsenseConnection_DNS()
{
using (var conn = ConnectionMultiplexer.Connect("doesnot.exist.ds.aasd981230d.com:6500"))
var log = new StringWriter();
try {
using (var conn = ConnectionMultiplexer.Connect("doesnot.exist.ds.aasd981230d.com:6500", log)) { }
} finally {
Console.WriteLine(log);
}
}
[Test]
public void CreateDisconnectedNonsenseConnection_IP()
{
var log = new StringWriter();
try
{
using (var conn = ConnectionMultiplexer.Connect(Config.LocalHost + ":6500,abortConnect=false")) {
Assert.IsFalse(conn.GetServer(conn.GetEndPoints().Single()).IsConnected);
Assert.IsFalse(conn.GetDatabase().IsConnected(default(RedisKey)));
}
}
finally
{
Console.WriteLine(log);
}
}
[Test]
public void CreateDisconnectedNonsenseConnection_DNS()
{
var log = new StringWriter();
try
{
using (var conn = ConnectionMultiplexer.Connect("doesnot.exist.ds.aasd981230d.com:6500,abortConnect=false", log)) {
Assert.IsFalse(conn.GetServer(conn.GetEndPoints().Single()).IsConnected);
Assert.IsFalse(conn.GetDatabase().IsConnected(default(RedisKey)));
}
}
finally
{
Console.WriteLine(log);
}
}
......
......@@ -40,6 +40,10 @@ public enum ConnectionFailureType
/// <summary>
/// The database is loading and is not available for use
/// </summary>
Loading
Loading,
/// <summary>
/// It has not been possible to create an intial connection to the redis server(s)
/// </summary>
UnableToConnect
}
}
......@@ -647,7 +647,7 @@ public static async Task<ConnectionMultiplexer> ConnectAsync(string configuratio
bool configured = await muxer.ReconfigureAsync(true, false, log, null, "connect").ObserveErrors().ForAwait();
if (!configured)
{
throw new InvalidOperationException("Unable to configure servers");
throw ExceptionFactory.UnableToConnect();
}
killMe = null;
return muxer;
......@@ -670,7 +670,7 @@ public static async Task<ConnectionMultiplexer> ConnectAsync(ConfigurationOption
bool configured = await muxer.ReconfigureAsync(true, false, log, null, "connect").ObserveErrors().ForAwait();
if (!configured)
{
throw new InvalidOperationException("Unable to configure servers");
throw ExceptionFactory.UnableToConnect();
}
killMe = null;
return muxer;
......@@ -717,6 +717,7 @@ public static ConnectionMultiplexer Connect(string configuration, TextWriter log
throw new TimeoutException();
}
}
if(!task.Result) throw ExceptionFactory.UnableToConnect();
killMe = null;
return muxer;
}
......@@ -745,6 +746,7 @@ public static ConnectionMultiplexer Connect(ConfigurationOptions configuration,
throw new TimeoutException();
}
}
if(!task.Result) throw ExceptionFactory.UnableToConnect();
killMe = null;
return muxer;
}
......@@ -1259,6 +1261,10 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
LogLocked(log, stormLog);
}
if(first && configuration.AbortOnConnectFail && (standaloneCount == 0 && clusterCount == 0))
{
return false;
}
return true;
} catch (Exception ex)
......
......@@ -105,5 +105,11 @@ static string GetLabel(bool includeDetail, RedisCommand command, Message message
{
return message == null ? command.ToString() : (includeDetail ? message.CommandAndKey : message.Command.ToString());
}
internal static Exception UnableToConnect()
{
return new RedisConnectionException(ConnectionFailureType.UnableToConnect,
"It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail");
}
}
}
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