public void Test_CanGetKeysByPattern_HasOnlyFallbackClientRule(string pattern) { List <string> expectedResult = new List <string> { "x", "y", "z" }; Mock <ICacheClientExtended> fallbackClientMocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); fallbackClientMocker.Setup(c => c.GetKeysByPattern(pattern)) .Returns(expectedResult); IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClientMocker.Object); IEnumerable <string> actualResult = routedCacheClient.GetKeysByPattern(pattern); Assert.NotNull(actualResult); CollectionAssert.AreEqual(expectedResult, actualResult); fallbackClientMocker.Verify(c => c.GetKeysByPattern(pattern), Times.Once); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_FlushAllCall_SameCacheClientForMultipleRules() { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); sessionClientMocker.Setup(c => c.FlushAll()); fallbackClientMocker.Setup(c => c.FlushAll()); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, new KeyStartsWithStringCacheClientRule(sessionClient, StringComparison.InvariantCultureIgnoreCase, "xyz:"), new KeyStartsWithStringCacheClientRule(sessionClient, StringComparison.InvariantCultureIgnoreCase, "session:")); routedCacheClient.FlushAll(); sessionClientMocker.Verify(c => c.FlushAll(), Times.Once); fallbackClientMocker.Verify(c => c.FlushAll(), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_FlushAllCall_UniqueCacheClients() { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); sessionClientMocker.Setup(c => c.FlushAll()); fallbackClientMocker.Setup(c => c.FlushAll()); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); routedCacheClient.FlushAll(); sessionClientMocker.Verify(c => c.FlushAll(), Times.Once); fallbackClientMocker.Verify(c => c.FlushAll(), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_SingleRemoveCalls() { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); string randomKey = RandomKey; string sessionKey = SessionKey; sessionClientMocker.Setup(c => c.Remove(sessionKey)) .Returns(true); fallbackClientMocker.Setup(c => c.Remove(randomKey)) .Returns(true); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); Assert.IsTrue(routedCacheClient.Remove(sessionKey)); Assert.IsTrue(fallbackClient.Remove(randomKey)); sessionClientMocker.Verify(c => c.Remove(sessionKey), Times.Once); fallbackClientMocker.Verify(c => c.Remove(randomKey), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_GetTimeToLive_NullTtl() { Mock <ICacheClientExtended> sessionClientMocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); Mock <ICacheClientExtended> fallbackClientMocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); string randomKey = RandomKey; string sessionKey = SessionKey; sessionClientMocker.Setup(c => c.GetTimeToLive(sessionKey)) .Returns(( TimeSpan? )null); fallbackClientMocker.Setup(c => c.GetTimeToLive(randomKey)) .Returns(( TimeSpan? )null); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); Assert.AreEqual(null, routedCacheClient.GetTimeToLive(sessionKey)); Assert.AreEqual(null, routedCacheClient.GetTimeToLive(randomKey)); sessionClientMocker.Verify(c => c.GetTimeToLive(sessionKey), Times.Once); fallbackClientMocker.Verify(c => c.GetTimeToLive(randomKey), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanGetKeysByPattern_HasManyClientRules_AllDifferent(string pattern) { List <string> expectedResult = new List <string> { "x", "y", "z", "a", "b", "c", "1", "2", "3" }; Mock <ICacheClientExtended> fallbackClientMocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); Mock <ICacheClientExtended> client1Mocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); Mock <ICacheClientExtended> client2Mocker = new Mock <ICacheClientExtended>(MockBehavior.Strict); fallbackClientMocker.Setup(c => c.GetKeysByPattern(pattern)) .Returns(new List <string>() { "x", "y", "z" }); client1Mocker.Setup(c => c.GetKeysByPattern(pattern)) .Returns(new List <string>() { "a", "b", "c" }); client2Mocker.Setup(c => c.GetKeysByPattern(pattern)) .Returns(new List <string>() { "1", "2", "3" }); IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClientMocker.Object, new KeyStartsWithStringCacheClientRule(client1Mocker.Object, StringComparison.InvariantCultureIgnoreCase, Guid.NewGuid().ToString()), new KeyStartsWithStringCacheClientRule(client2Mocker.Object, StringComparison.InvariantCultureIgnoreCase, Guid.NewGuid().ToString())); IEnumerable <string> actualResult = routedCacheClient.GetKeysByPattern(pattern); Assert.NotNull(actualResult); CollectionAssert.AreEquivalent(expectedResult, actualResult); fallbackClientMocker.Verify(c => c.GetKeysByPattern(pattern), Times.Once); client1Mocker.Verify(c => c.GetKeysByPattern(pattern), Times.Once); client2Mocker.Verify(c => c.GetKeysByPattern(pattern), Times.Once); fallbackClientMocker.VerifyNoOtherCalls(); client1Mocker.VerifyNoOtherCalls(); client2Mocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_SingleAddCalls(int sessionClientVal, string fallbackClientVal) { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); string randomKey = RandomKey; string sessionKey = SessionKey; DateTime dateTimeRef = DateTime.Now; TimeSpan timeSpanRef = TimeSpan.FromHours(1); sessionClientMocker.Setup(c => c.Add <int>(sessionKey, sessionClientVal)) .Returns(true); sessionClientMocker.Setup(c => c.Add <int>(sessionKey, sessionClientVal, dateTimeRef)) .Returns(true); sessionClientMocker.Setup(c => c.Add <int>(sessionKey, sessionClientVal, timeSpanRef)) .Returns(true); fallbackClientMocker.Setup(c => c.Add <string>(randomKey, fallbackClientVal)) .Returns(true); fallbackClientMocker.Setup(c => c.Add <string>(randomKey, fallbackClientVal, dateTimeRef)) .Returns(true); fallbackClientMocker.Setup(c => c.Add <string>(randomKey, fallbackClientVal, timeSpanRef)) .Returns(true); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); routedCacheClient.Add <int>(sessionKey, sessionClientVal); routedCacheClient.Add <int>(sessionKey, sessionClientVal, dateTimeRef); routedCacheClient.Add <int>(sessionKey, sessionClientVal, timeSpanRef); routedCacheClient.Add <string>(randomKey, fallbackClientVal); routedCacheClient.Add <string>(randomKey, fallbackClientVal, timeSpanRef); routedCacheClient.Add <string>(randomKey, fallbackClientVal, dateTimeRef); sessionClientMocker.Verify(c => c.Add <int>(sessionKey, sessionClientVal), Times.Once); sessionClientMocker.Verify(c => c.Add <int>(sessionKey, sessionClientVal, dateTimeRef), Times.Once); sessionClientMocker.Verify(c => c.Add <int>(sessionKey, sessionClientVal, timeSpanRef), Times.Once); fallbackClientMocker.Verify(c => c.Add <string>(randomKey, fallbackClientVal), Times.Once); fallbackClientMocker.Verify(c => c.Add <string>(randomKey, fallbackClientVal, dateTimeRef), Times.Once); fallbackClientMocker.Verify(c => c.Add <string>(randomKey, fallbackClientVal, timeSpanRef), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanClearRules_HasOnlyFallbackClientRule() { Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClientMocker.Object); Assert.AreEqual(1, routedCacheClient.RulesCount); routedCacheClient.ClearRules(); Assert.AreEqual(1, routedCacheClient.RulesCount); }
/// <summary> /// Registers the given cache client as being used for ServiceStack sessions /// with the given target routed cache client. /// Essentially, what this does is register a ServiceStackSessionKeyCacheClientRule /// with the given routed cache client, backed by the given cache client. /// </summary> /// <param name="routedClient">The target routed cache client</param> /// <param name="cacheClient">The cache client that will be used for session storage.</param> /// <returns></returns> public static IRoutedCacheClient PushServiceStackSessionCacheClient(this IRoutedCacheClient routedClient, ICacheClient cacheClient, bool autoDispose = true) { if (routedClient == null) { throw new ArgumentNullException(nameof(routedClient)); } IRoutedCacheClientRule serviceStackSessionRule = new ServiceStackSessionKeyCacheClientRule(cacheClient); serviceStackSessionRule.AutoDispose = autoDispose; routedClient.PushClientWithRule(serviceStackSessionRule); return(routedClient); }
public static IEnumerable <IRoutedCacheClientRule> GetRegisteredCacheClientRules(this ICacheClient cacheClient) { if (cacheClient == null) { throw new ArgumentNullException(nameof(cacheClient)); } IRoutedCacheClient routedCacheClient = cacheClient as IRoutedCacheClient; if (routedCacheClient != null) { return(routedCacheClient.GetRegisteredClientRules()); } else { throw new Exception($"{nameof( GetRegisteredCacheClientRules )} is not implemented by {cacheClient.GetType().FullName}"); }; }
public void Test_CanClearRules_HasManyClientRules() { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, new KeyStartsWithStringCacheClientRule(sessionClient, StringComparison.InvariantCultureIgnoreCase, "xyz:"), new KeyStartsWithStringCacheClientRule(sessionClient, StringComparison.InvariantCultureIgnoreCase, "session:")); Assert.AreEqual(3, routedCacheClient.RulesCount); routedCacheClient.ClearRules(); Assert.AreEqual(1, routedCacheClient.RulesCount); }
public void Test_CanRoute_DecrementCall(int sessionClientVal, int fallbackClientVal) { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); string randomKey = RandomKey; string sessionKey = SessionKey; sessionClientMocker.Setup(c => c.Decrement(sessionKey, 1)) .Returns(sessionClientVal - 1); fallbackClientMocker.Setup(c => c.Decrement(randomKey, 10)) .Returns(fallbackClientVal - 10); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); Assert.AreEqual(sessionClientVal - 1, routedCacheClient.Decrement(sessionKey, 1)); Assert.AreEqual(fallbackClientVal - 10, routedCacheClient.Decrement(randomKey, 10)); sessionClientMocker.Verify(c => c.Decrement(sessionKey, 1), Times.Once); fallbackClientMocker.Verify(c => c.Decrement(randomKey, 10), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
public void Test_CanRoute_SingleGetCalls(int sessionClientVal, string fallbackClientVal) { Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); string randomKey = RandomKey; string sessionKey = SessionKey; sessionClientMocker.Setup(c => c.Get <int>(sessionKey)) .Returns(sessionClientVal); fallbackClientMocker.Setup(c => c.Get <string>(randomKey)) .Returns(fallbackClientVal); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); Assert.AreEqual(sessionClientVal, routedCacheClient .Get <int>(sessionKey)); Assert.AreEqual(fallbackClientVal, routedCacheClient .Get <string>(randomKey)); sessionClientMocker.Verify(c => c.Get <int>(sessionKey), Times.Once); fallbackClientMocker.Verify(c => c.Get <string>(randomKey), Times.Once); sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }
private void Assert_AllMethodsThrowObjectDisposedException(IRoutedCacheClient routedCacheClient) { Faker faker = new Faker(); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(RandomKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(SessionKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(RandomKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(SessionKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(RandomKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Add(SessionKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Decrement(RandomKey, faker.Random.UInt())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Decrement(SessionKey, faker.Random.UInt())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .FlushAll()); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .FlushAll()); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Get <string>(RandomKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Get <string>(SessionKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetAll <string>(faker.Make(faker.Random.Int(0, 100), () => RandomKey))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetAll <string>(faker.Make(faker.Random.Int(0, 100), () => SessionKeyPrefix))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetKeysByPattern(SessionKeyPrefix)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetKeysByPattern(RandomKeyPrefix)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetTimeToLive(SessionKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetTimeToLive(RandomKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Increment(RandomKey, faker.Random.UInt())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Increment(SessionKey, faker.Random.UInt())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Remove(SessionKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Remove(RandomKey)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .RemoveAll(faker.Make(faker.Random.Int(0, 100), () => RandomKey))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .RemoveAll(faker.Make(faker.Random.Int(0, 100), () => SessionKeyPrefix))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(RandomKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(SessionKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(RandomKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(SessionKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(RandomKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Replace(SessionKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(RandomKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(SessionKey, faker.Random.Int())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(RandomKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(SessionKey, faker.Random.Decimal(), faker.Date.Timespan())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(RandomKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .Set(SessionKey, faker.Random.String(), faker.Date.Future())); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .SetAll(new Dictionary <string, string>(faker.Make( faker.Random.Int(0, 100), () => new KeyValuePair <string, string>(RandomKey, faker.Random.String()) )))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .SetAll(new Dictionary <string, int>(faker.Make( faker.Random.Int(0, 100), () => new KeyValuePair <string, int>(SessionKey, faker.Random.Int()) )))); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .PushClientWithRule(new Mock <IRoutedCacheClientRule>(MockBehavior.Loose).Object)); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .ClearRules()); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetRegisteredClients()); Assert.Throws <ObjectDisposedException>(() => routedCacheClient .GetRegisteredClientRules()); }
public void Test_CanRoute_SetAll(int numSessionClientVals, int numFallbackClientVals) { Faker faker = new Faker(); Mock <ICacheClient> sessionClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Mock <ICacheClient> fallbackClientMocker = new Mock <ICacheClient>(MockBehavior.Strict); Dictionary <string, decimal> sessionValues = new Dictionary <string, decimal>(); Dictionary <string, string> fallbackValues = new Dictionary <string, string>(); for (int i = 0; i < numSessionClientVals; i++) { sessionValues[SessionKey] = faker.Random.Decimal(); } for (int i = 0; i < numFallbackClientVals; i++) { fallbackValues[RandomKey] = faker.Random.String(); } sessionClientMocker.Setup(c => c.SetAll(sessionValues)); fallbackClientMocker.Setup(c => c.SetAll(fallbackValues)); ICacheClient sessionClient = sessionClientMocker.Object; ICacheClient fallbackClient = fallbackClientMocker.Object; IRoutedCacheClient routedCacheClient = CreateRoutedCacheClient(fallbackClient, sessionClient); routedCacheClient.SetAll(sessionValues); routedCacheClient.SetAll(fallbackValues); if (numSessionClientVals > 0) { sessionClientMocker.Verify(c => c.SetAll(sessionValues), Times.Once); } else { sessionClientMocker.Verify(c => c.SetAll(sessionValues), Times.Never); } if (numFallbackClientVals > 0) { fallbackClientMocker.Verify(c => c.SetAll(fallbackValues), Times.Once); } else { fallbackClientMocker.Verify(c => c.SetAll(fallbackValues), Times.Never); } sessionClientMocker.VerifyNoOtherCalls(); fallbackClientMocker.VerifyNoOtherCalls(); }