Skip to content

Commit

Permalink
Allow default single string constructor to contain host:port format
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 14, 2013
1 parent 4f9d2c5 commit fcf546f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ServiceStack.Redis/RedisNativeClient.cs
Expand Up @@ -111,7 +111,7 @@ internal IRedisPipelineShared Pipeline
}

public RedisNativeClient(string host)
: this(host, DefaultPort) {}
: this(host.SplitOnLast(':')[0], host.Contains(':') ? int.Parse(host.SplitOnLast(':')[1]) : DefaultPort) { }

public RedisNativeClient(string host, int port)
: this(host, port, null) {}
Expand Down
6 changes: 5 additions & 1 deletion src/TestMqHost/Program2.cs
Expand Up @@ -13,13 +13,17 @@ namespace TestMqHost
{
class Program2
{

static void Main(string[] args)
{
var clientManager = new PooledRedisClientManager(new[] { "localhost" })
{
PoolTimeout = 1000,
};
clientManager.GetClient().FlushAll();
using (var client = clientManager.GetClient())
{
client.FlushAll();
}

var mqHost = new RedisMqServer(clientManager);

Expand Down
73 changes: 73 additions & 0 deletions tests/ServiceStack.Redis.Tests/RedisFailoverTests.cs
Expand Up @@ -140,5 +140,78 @@ public void Can_MqServer_recover_from_server_terminated_client_connections()
});

}

[Test]
public void Can_failover_at_runtime()
{
var failoverHost = "ny-devredis01:6380";
var localClient = new RedisClient("localhost");
string key = "test:failover";

localClient.Remove(key);
var failoverClient = new RedisClient(failoverHost);
failoverClient.Remove(key);

var clientManager = new PooledRedisClientManager(new[] { "localhost" });

RunInLoop(clientManager, callback:() =>
{
lock (clientManager)
Monitor.Pulse(clientManager);
});

Thread.Sleep(100);

clientManager.FailoverTo(failoverHost);

lock (clientManager)
Monitor.Wait(clientManager);

var localIncr = localClient.Get<int>(key);
var failoverIncr = failoverClient.Get<int>(key);
Assert.That(localIncr, Is.GreaterThan(0));
Assert.That(failoverIncr, Is.GreaterThan(0));
Assert.That(localIncr + failoverIncr, Is.EqualTo(100));
}


public static bool RunInLoop(PooledRedisClientManager clientManager, int iterations = 100, int sleepMs = 10, Action callback=null)
{
int count = 0;
int errors = 0;

10.Times(i =>
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (iterations-- > 0)
{
using (var client = clientManager.GetClient())
{
try
{
var result = client.Increment("test:failover", 1);
if (++count % (iterations / 10) == 0)
lock (clientManager)
Console.WriteLine("count: {0}, errors: {1}", count, errors);
}
catch (Exception ex)
{
errors++;
}
Thread.Sleep(sleepMs);
}
}
if (callback != null)
{
callback();
callback = null;
}
});
});

return true;
}
}
}

0 comments on commit fcf546f

Please sign in to comment.