Skip to content

Commit

Permalink
Test configuration consistency
Browse files Browse the repository at this point in the history
Remove some of the duplication in RedisClient and ClientManager
construction.  Change locations with hard-coded "localhost" or
"127.0.0.1" server addresses to use TestConfig.SingleHost.

With these changes, we're down to 10 failures(not connection-related) when
running the tests without a local instance of Redis
  • Loading branch information
dcartoon committed May 26, 2013
1 parent 0bee8be commit 0cac358
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using NUnit.Framework;
using ServiceStack.Redis.Messaging;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests.Benchmarks
{
Expand All @@ -26,7 +27,7 @@ private static RedisMqHost CreateMqHost()

private static RedisMqHost CreateMqHost(int noOfRetries)
{
var redisFactory = new BasicRedisClientManager();
var redisFactory = RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance();
try
{
redisFactory.Exec(redis => redis.FlushAll());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NUnit.Framework;
using ServiceStack.Redis.Messaging;
using ServiceStack.Text;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests.Benchmarks
{
Expand All @@ -22,7 +23,7 @@ public class IncrBlocking

private static RedisMqHostPool CreateMqHostPool(int threadCount = 1)
{
var redisFactory = new BasicRedisClientManager();
var redisFactory = RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance();
try
{
redisFactory.Exec(redis => redis.FlushAll());
Expand Down
2 changes: 1 addition & 1 deletion tests/ServiceStack.Redis.Tests/Examples/TodoApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Todo
public void Crud_TODO_App()
{
//Thread-safe client factory
var redisManager = new PooledRedisClientManager("localhost:6379");
var redisManager = new PooledRedisClientManager(TestConfig.SingleHostConnectionString);

redisManager.ExecAs<Todo>(redisTodos => {
var todo = new Todo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ServiceStack.Common.Tests.Models;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests.Generic
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ServiceStack.Redis.Tests/Issues/DomainEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DomainEventsTests
public void Can_Retrieve_DomainEvents()
{
var userId = Guid.NewGuid();
var client = new RedisClient("localhost");
var client = new RedisClient(TestConfig.SingleHost);
client.FlushAll();

client.As<DomainEvent>().Lists["urn:domainevents-" + userId].Add(new UserPromotedEvent { UserId = userId });
Expand All @@ -51,7 +51,7 @@ public void Can_Retrieve_DomainEvents()
[Test]
public void Can_from_Retrieve_DomainEvents_list()
{
var client = new RedisClient("localhost");
var client = new RedisClient(TestConfig.SingleHost);
var users = client.As<AggregateEvents>();

var userId = Guid.NewGuid();
Expand Down
3 changes: 2 additions & 1 deletion tests/ServiceStack.Redis.Tests/ManagedListGenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Redis.Generic;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests
{
Expand All @@ -15,7 +16,7 @@ public class ManagedListGenericTests
public void TestSetUp()
{
if (redisManager != null) redisManager.Dispose();
redisManager = new BasicRedisClientManager(TestConfig.SingleHost);
redisManager = RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance();
redisManager.Exec(r => r.FlushAll());
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ServiceStack.Redis.Tests/QueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void OnBeforeEachTest()
[Test]
public void TestSequentialWorkQueueUpdate()
{
using (var queue = new RedisSequentialWorkQueue<string>(10, 10, "127.0.0.1", 6379,1))
using (var queue = new RedisSequentialWorkQueue<string>(10, 10, TestConfig.SingleHost, TestConfig.RedisPort,1))
{

for (int i = 0; i < numMessages; ++i)
Expand All @@ -56,7 +56,7 @@ public void TestSequentialWorkQueueUpdate()
[Test]
public void TestSequentialWorkQueue()
{
using (var queue = new RedisSequentialWorkQueue<string>(10,10,"127.0.0.1",6379,1))
using (var queue = new RedisSequentialWorkQueue<string>(10, 10, TestConfig.SingleHost, TestConfig.RedisPort, 1))
{

for (int i = 0; i < numMessages; ++i)
Expand Down Expand Up @@ -117,7 +117,7 @@ public void TestSequentialWorkQueue()
[Test]
public void TestChronologicalWorkQueue()
{
using (var queue = new RedisChronologicalWorkQueue<string>(10, 10, "127.0.0.1", 6379))
using (var queue = new RedisChronologicalWorkQueue<string>(10, 10, TestConfig.SingleHost, TestConfig.RedisPort))
{
const int numMessages = 6;
var messages = new List<string>();
Expand Down Expand Up @@ -156,7 +156,7 @@ public void TestChronologicalWorkQueue()
[Test]
public void TestSimpleWorkQueue()
{
using (var queue = new RedisSimpleWorkQueue<string>(10, 10, "127.0.0.1", 6379))
using (var queue = new RedisSimpleWorkQueue<string>(10, 10, TestConfig.SingleHost, TestConfig.RedisPort))
{
int numMessages = 6;
var messages = new string[numMessages];
Expand Down
99 changes: 56 additions & 43 deletions tests/ServiceStack.Redis.Tests/RedisClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class RedisClientTests
{
const string Value = "Value";


public override void OnBeforeEachTest()
{
base.OnBeforeEachTest();
Expand Down Expand Up @@ -447,12 +448,13 @@ public void Can_store_multiple_keys()
var keys = 5.Times(x => "key" + x);
var vals = 5.Times(x => "val" + x);

var redis = RedisClient.New();

redis.SetAll(keys, vals);
using (var redis = RedisClient.New())
{
redis.SetAll(keys, vals);

var all = redis.GetValues(keys);
Assert.AreEqual(vals, all);
var all = redis.GetValues(keys);
Assert.AreEqual(vals, all);
}
}

[Test]
Expand All @@ -463,12 +465,13 @@ public void Can_store_Dictionary()
var map = new Dictionary<string, string>();
keys.ForEach(x => map[x] = "val" + x);

var redis = RedisClient.New();

redis.SetAll(map);
using (var redis = RedisClient.New())
{
redis.SetAll(map);

var all = redis.GetValuesMap(keys);
Assert.AreEqual(map, all);
var all = redis.GetValuesMap(keys);
Assert.AreEqual(map, all);
}
}

[Test]
Expand All @@ -478,11 +481,13 @@ public void Can_store_Dictionary_as_objects()
map["key_a"] = "123";
map["key_b"] = null;

var redis = RedisClient.New();
redis.SetAll(map);
using (var redis = RedisClient.New())
{
redis.SetAll(map);

Assert.That(redis.Get<string>("key_a"), Is.EqualTo("123"));
Assert.That(redis.Get("key_b"), Is.EqualTo(""));
Assert.That(redis.Get<string>("key_a"), Is.EqualTo("123"));
Assert.That(redis.Get("key_b"), Is.EqualTo(""));
}
}


Expand All @@ -493,55 +498,63 @@ public void Can_store_Dictionary_as_bytes()
map["key_a"] = "123".ToUtf8Bytes();
map["key_b"] = null;

var redis = RedisClient.New();
redis.SetAll(map);
using (var redis = RedisClient.New())
{
redis.SetAll(map);

Assert.That(redis.Get<string>("key_a"), Is.EqualTo("123"));
Assert.That(redis.Get("key_b"), Is.EqualTo(""));
Assert.That(redis.Get<string>("key_a"), Is.EqualTo("123"));
Assert.That(redis.Get("key_b"), Is.EqualTo(""));
}
}

[Test]
public void Should_reset_slowlog()
{
var redis = RedisClient.New();
redis.SlowlogReset();
using (var redis = RedisClient.New())
{
redis.SlowlogReset();
}
}

[Test]
public void Can_get_showlog()
{
var redis = RedisClient.New();
var log = redis.GetSlowlog(10);

foreach (var t in log)
using (var redis = RedisClient.New())
{
Console.WriteLine(t.Id);
Console.WriteLine(t.Duration);
Console.WriteLine(t.Timestamp);
Console.WriteLine(string.Join(":", t.Arguments));
var log = redis.GetSlowlog(10);

foreach (var t in log)
{
Console.WriteLine(t.Id);
Console.WriteLine(t.Duration);
Console.WriteLine(t.Timestamp);
Console.WriteLine(string.Join(":", t.Arguments));
}
}
}


[Test]
public void Can_change_db_at_runtime()
{
var redis = new RedisClient(TestConfig.SingleHost, TestConfig.RedisPort, db : 1);
var val = Environment.TickCount;
var key = "test" + val;
try
{
redis.Set(key, val);
redis.ChangeDb(2);
Assert.That(redis.Get<int>(key), Is.EqualTo(0));
redis.ChangeDb(1);
Assert.That(redis.Get<int>(key), Is.EqualTo(val));
redis.Dispose();
}
finally
using (var redis = new RedisClient(TestConfig.SingleHost, TestConfig.RedisPort, db: 1))
{
redis.ChangeDb(1);
redis.Del(key);
var val = Environment.TickCount;
var key = "test" + val;
try
{
redis.Set(key, val);
redis.ChangeDb(2);
Assert.That(redis.Get<int>(key), Is.EqualTo(0));
redis.ChangeDb(1);
Assert.That(redis.Get<int>(key), Is.EqualTo(val));
redis.Dispose();
}
finally
{
redis.ChangeDb(1);
redis.Del(key);
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/ServiceStack.Redis.Tests/RedisClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ protected void Log(string fmt, params object[] args)
Debug.WriteLine("{0}", string.Format(fmt, args).Trim());
}

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
RedisClient.NewFactoryFn = () => new RedisClient(TestConfig.SingleHost);
}

[SetUp]
public virtual void OnBeforeEachTest()
{
Redis = new RedisClient(TestConfig.SingleHost);
Redis = RedisClient.New();
}

[TearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Redis.Generic;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests
{
Expand All @@ -15,7 +16,7 @@ public class RedisClientsManagerExtensionsTests
public void OnBeforeEachTest()
{
if (redisManager != null) redisManager.Dispose();
redisManager = new BasicRedisClientManager(TestConfig.SingleHost);
redisManager = RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance();
redisManager.Exec(r => r.FlushAll());
}

Expand Down
3 changes: 2 additions & 1 deletion tests/ServiceStack.Redis.Tests/RedisMqHostSupportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ServiceStack.Messaging;
using ServiceStack.Messaging.Tests.Services;
using ServiceStack.Redis.Messaging;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests
{
Expand All @@ -13,7 +14,7 @@ public void Does_serialize_to_correct_MQ_name()
{
var message = new Message<Greet>(new Greet {Name = "Test"}) {};

var mqClient = new RedisMessageQueueClient(new BasicRedisClientManager());
var mqClient = new RedisMessageQueueClient(RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance());

mqClient.Publish(message);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ServiceStack.Redis.Tests/RedisMqHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using ServiceStack.Messaging;
using ServiceStack.Redis.Messaging;
using ServiceStack.Text;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests
{
Expand Down Expand Up @@ -40,7 +41,7 @@ private RedisMqHost CreateMqHost()

private static RedisMqHost CreateMqHost(int noOfRetries)
{
var redisFactory = new BasicRedisClientManager();
var redisFactory = RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance();
try
{
redisFactory.Exec(redis => redis.FlushAll());
Expand Down Expand Up @@ -72,7 +73,7 @@ private static void Publish_4_Rot13_messages(IMessageQueueClient mqClient)
[Test]
public void Utils_publish_Reverse_messages()
{
mqHost = new RedisMqHost(new BasicRedisClientManager(), 2, null);
mqHost = new RedisMqHost(RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance(), 2, null);
var mqClient = mqHost.CreateMessageQueueClient();
Publish_4_messages(mqClient);
Thread.Sleep(500);
Expand All @@ -82,7 +83,7 @@ public void Utils_publish_Reverse_messages()
[Test]
public void Utils_publish_Rot13_messages()
{
mqHost = new RedisMqHost(new BasicRedisClientManager(), 2, null);
mqHost = new RedisMqHost(RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance(), 2, null);
var mqClient = mqHost.CreateMessageQueueClient();
Publish_4_Rot13_messages(mqClient);
Thread.Sleep(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NUnit.Framework;
using ServiceStack.Redis.Messaging;
using ServiceStack.Text;
using ServiceStack.Redis.Tests.Support;

namespace ServiceStack.Redis.Tests
{
Expand Down Expand Up @@ -39,10 +40,10 @@ class Sleep1000

RedisMqServer CreateServer()
{
using (var redis = new RedisClient())
using (var redis = new RedisClient(TestConfig.SingleHost))
redis.FlushAll();

var mqServer = new RedisMqServer(new BasicRedisClientManager());
var mqServer = new RedisMqServer(RedisTestClientManagerFactory.GetBasicRedisClientManagerInstance());
mqServer.RegisterHandler<Sleep0>(m => new Sleep0 { Id = counter.Sleep0++ });

mqServer.RegisterHandler<Sleep10>(m => {
Expand Down

0 comments on commit 0cac358

Please sign in to comment.