コード例 #1
0
        public void Test_CanRoute_SingleSetCall(decimal 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.Set <decimal>(sessionKey, sessionClientVal))
            .Returns(true);
            sessionClientMocker.Setup(c => c.Set <decimal>(sessionKey, sessionClientVal, dateTimeRef))
            .Returns(true);
            sessionClientMocker.Setup(c => c.Set <decimal>(sessionKey, sessionClientVal, timeSpanRef))
            .Returns(true);

            fallbackClientMocker.Setup(c => c.Set <string>(randomKey, fallbackClientVal))
            .Returns(true);
            fallbackClientMocker.Setup(c => c.Set <string>(randomKey, fallbackClientVal, dateTimeRef))
            .Returns(true);
            fallbackClientMocker.Setup(c => c.Set <string>(randomKey, fallbackClientVal, timeSpanRef))
            .Returns(true);

            ICacheClient sessionClient  = sessionClientMocker.Object;
            ICacheClient fallbackClient = fallbackClientMocker.Object;

            IRoutedCacheClient routedCacheClient =
                CreateRoutedCacheClient(fallbackClient, sessionClient);

            Assert.IsTrue(routedCacheClient.Set(sessionKey, sessionClientVal));
            Assert.IsTrue(routedCacheClient.Set(sessionKey, sessionClientVal, dateTimeRef));
            Assert.IsTrue(routedCacheClient.Set(sessionKey, sessionClientVal, timeSpanRef));

            Assert.IsTrue(routedCacheClient.Set(randomKey, fallbackClientVal));
            Assert.IsTrue(routedCacheClient.Set(randomKey, fallbackClientVal, dateTimeRef));
            Assert.IsTrue(routedCacheClient.Set(randomKey, fallbackClientVal, timeSpanRef));

            sessionClientMocker.Verify(c => c.Set <decimal>(sessionKey, sessionClientVal),
                                       Times.Once);
            sessionClientMocker.Verify(c => c.Set <decimal>(sessionKey, sessionClientVal, dateTimeRef),
                                       Times.Once);
            sessionClientMocker.Verify(c => c.Set <decimal>(sessionKey, sessionClientVal, timeSpanRef),
                                       Times.Once);

            fallbackClientMocker.Verify(c => c.Set <string>(randomKey, fallbackClientVal),
                                        Times.Once);
            fallbackClientMocker.Verify(c => c.Set <string>(randomKey, fallbackClientVal, dateTimeRef),
                                        Times.Once);
            fallbackClientMocker.Verify(c => c.Set <string>(randomKey, fallbackClientVal, timeSpanRef),
                                        Times.Once);

            sessionClientMocker.VerifyNoOtherCalls();
            fallbackClientMocker.VerifyNoOtherCalls();
        }
コード例 #2
0
        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());
        }