Last active
August 29, 2015 14:12
-
-
Save brunomlopes/f38abac610d0105a1a7b to your computer and use it in GitHub Desktop.
RedisCharacterizationTest for diferent databases using ServiceStack.Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ServiceStack.Redis; | |
using Xunit; | |
namespace SocialStack.Tests.Integration.Infrastructure | |
{ | |
public class RedisCharacterizationTest : IDisposable | |
{ | |
private IRedisClientsManager _db1ClientManager; | |
private IRedisClientsManager _db2ClientManager; | |
[Fact] | |
public void BasicRedisClientManager_WhenUsingADatabaseOnARedisConnectionString_CorrectDatabaseIsUsed() | |
{ | |
TestForDatabaseOnConnectionString(connectionString => new BasicRedisClientManager(connectionString)); | |
} | |
[Fact] | |
public void PooledRedisClientManager_WhenUsingADatabaseOnARedisConnectionString_CorrectDatabaseIsUsed() | |
{ | |
TestForDatabaseOnConnectionString(connectionString => new PooledRedisClientManager(connectionString)); | |
} | |
private void TestForDatabaseOnConnectionString(Func<string, IRedisClientsManager> factory) | |
{ | |
_db1ClientManager = factory("localhost?db=1"); | |
_db2ClientManager = factory("localhost?db=2"); | |
using (var cacheClient = _db1ClientManager.GetCacheClient()) | |
{ | |
cacheClient.Set("key", "value"); | |
} | |
using (var cacheClient = _db2ClientManager.GetCacheClient()) | |
{ | |
Assert.Null(cacheClient.Get<string>("key")); | |
} | |
} | |
[Fact] | |
public void WhenUsingAnInitialDatabase_CorrectDatabaseIsUsed() | |
{ | |
_db1ClientManager = new BasicRedisClientManager(1, "localhost"); | |
_db2ClientManager = new BasicRedisClientManager(2, "localhost"); | |
using (var cacheClient = _db1ClientManager.GetCacheClient()) | |
{ | |
cacheClient.Set("key", "value"); | |
} | |
using (var cacheClient = _db2ClientManager.GetCacheClient()) | |
{ | |
Assert.Null(cacheClient.Get<string>("key")); | |
} | |
} | |
public void Dispose() | |
{ | |
foreach (var clientManager in new [] {_db1ClientManager,_db2ClientManager}) | |
{ | |
if (clientManager != null) | |
{ | |
using (var cacheClient = clientManager.GetCacheClient()) | |
{ | |
cacheClient.Remove("key"); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment