public async Task ExistingObject_IncrementByOneAndSetExpirationDateAsync()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var    cache      = new MemoryCache(new MemoryCacheOptions());
                var    repository = new MemoryThrottleRepository(cache);
                string id         = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count      = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                cache
                .Set(id, cacheItem, cacheItem.Expiration);

                // Act
                await repository.AddOrIncrementWithExpirationAsync(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);

                Assert.Equal(2L, item.Count);
                Assert.Equal(new DateTime(2030, 1, 1), item.Expiration);
            }
            public void RetrieveValidThrottleCountFromRepostitory()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var    cache      = new MemoryCache("testing_cache");
                var    repository = new MemoryThrottleRepository(cache);
                string id         = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count      = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                repository.AddOrIncrementWithExpiration(key, limiter);

                // Act
                var count = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(count, 1);
            }
            public void ExistingObject_IncrementByOneAndSetExpirationDate()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);
                string id = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                cache
                    .Set(id, cacheItem, cacheItem.Expiration);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);
                Assert.Equal(2L, item.Count);
                Assert.Equal(new DateTime(2030, 1, 1), item.Expiration);
            }
            public void IncrementReturnsOne_ExpireKey()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                Limiter limiter = new Limiter().Limit(1).Over(10);
                var db = Substitute.For<IDatabase>();
                var repository = new RedisThrottleRepository(db);
                string id = repository.CreateThrottleKey(key, limiter);

                db
                    .StringIncrement(id)
                    .Returns(1);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                db
                    .Received(1)
                    .StringIncrement(id);

                db
                    .Received(1)
                    .KeyExpire(id, limiter.Period);
            }
            public void IncrementReturnsOne_ExpireKey()
            {
                // Arrange
                var     key        = new SimpleThrottleKey("test", "key");
                Limiter limiter    = new Limiter().Limit(1).Over(10);
                var     db         = Substitute.For <IDatabase>();
                var     repository = new RedisThrottleRepository(db);
                string  id         = repository.CreateThrottleKey(key, limiter);

                db
                .StringIncrement(id)
                .Returns(1);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                db
                .Received(1)
                .StringIncrement(id);

                db
                .Received(1)
                .KeyExpire(id, limiter.Period);
            }
            public void SetLock()
            {
                // Arrange
                var     key         = new SimpleThrottleKey("test", "key");
                Limiter limiter     = new Limiter().Limit(1).Over(1).LockFor(1);
                var     db          = Substitute.For <IDatabase>();
                var     repository  = new RedisThrottleRepository(db);
                string  id          = repository.CreateLockKey(key, limiter);
                var     transaction = Substitute.For <ITransaction>();

                db
                .CreateTransaction()
                .Returns(transaction);

                // Act
                repository.SetLock(key, limiter);

                // Assert
                transaction
                .Received(1)
                .StringIncrementAsync(id);

                transaction
                .Received(1)
                .KeyExpireAsync(id, limiter.LockDuration);

                transaction
                .Received(1)
                .Execute();
            }
            public async Task RetrieveValidThrottleCountFromRepostitoryAsync()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var    cache      = new MemoryCache(new MemoryCacheOptions());
                var    repository = new MemoryThrottleRepository(cache);
                string id         = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count      = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                await repository.AddOrIncrementWithExpirationAsync(key, limiter);

                // Act
                var count = await repository.GetThrottleCountAsync(key, limiter);

                // Assert
                Assert.Equal(1, count);
            }
            public void RetrieveValidThrottleCountFromRepostitory()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);
                string id = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                repository.AddOrIncrementWithExpiration(key, limiter);

                // Act
                var count = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(count, 1);
            }
            public void ThrottleCountReturnsNullWhenUsingInvalidKey()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var cache      = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);

                // Act
                var count = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(count, null);
            }
            public async Task ThrottleCountReturnsNullWhenUsingInvalidKeyAsync()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var cache      = new MemoryCache(new MemoryCacheOptions());
                var repository = new MemoryThrottleRepository(cache);

                // Act
                var count = await repository.GetThrottleCountAsync(key, limiter);

                // Assert
                Assert.Null(count);
            }
            public void RemoveThrottle()
            {
                // Arrange
                var     key        = new SimpleThrottleKey("test", "key");
                Limiter limiter    = new Limiter().Limit(1).Over(1);
                var     db         = Substitute.For <IDatabase>();
                var     repository = new RedisThrottleRepository(db);
                string  id         = repository.CreateThrottleKey(key, limiter);

                // Act
                repository.RemoveThrottle(key, limiter);

                // Assert
                db
                .Received(1)
                .KeyDelete(id);
            }
Example #12
0
            public void KeyDoesNotExist_ReturnsNull()
            {
                // Arrange
                var     key        = new SimpleThrottleKey("test", "key");
                Limiter limiter    = new Limiter().Limit(1).Over(1);
                var     db         = Substitute.For <IDatabase>();
                var     repository = new RedisThrottleRepository(db);
                string  id         = repository.CreateThrottleKey(key, limiter);

                db
                .StringGet(id)
                .Returns((long?)null);

                // Act
                long?result = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Null(result);
            }
            public void KeyExists_ReturnsTrue(bool keyExists, bool expected)
            {
                // Arrange
                var     key        = new SimpleThrottleKey("test", "key");
                Limiter limiter    = new Limiter().Limit(1).Over(1).LockFor(1);
                var     db         = Substitute.For <IDatabase>();
                var     repository = new RedisThrottleRepository(db);
                string  id         = repository.CreateLockKey(key, limiter);

                db
                .KeyExists(id)
                .Returns(keyExists);

                // Act
                bool result = repository.LockExists(key, limiter);

                // Assert
                Assert.Equal(expected, result);
            }
            public void KeyExists_ReturnsParsedValue()
            {
                // Arrange
                var     key        = new SimpleThrottleKey("test", "key");
                Limiter limiter    = new Limiter().Limit(1).Over(1);
                var     db         = Substitute.For <IDatabase>();
                var     repository = new RedisThrottleRepository(db);
                string  id         = repository.CreateThrottleKey(key, limiter);

                db
                .StringGet(id)
                .Returns((RedisValue)"10");

                // Act
                long?result = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(10, result);
            }
            public void KeyExists_ReturnsParsedValue()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                Limiter limiter = new Limiter().Limit(1).Over(1);
                var db = Substitute.For<IDatabase>();
                var repository = new RedisThrottleRepository(db);
                string id = repository.CreateThrottleKey(key, limiter);

                db
                    .StringGet(id)
                    .Returns((RedisValue)"10");

                // Act
                long? result = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(10, result);
            }
            public void NewObject_SetsCountToOneWithExpiration()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);
                repository.CurrentDate = () => new DateTime(2030, 1, 1);

                string id = repository.CreateThrottleKey(key, limiter);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);
                Assert.Equal(1L, item.Count);
                // We're testing a future date by 100 seconds which is 40 seconds + 1 minute
                Assert.Equal(new DateTime(2030, 1, 1, 0, 1, 40), item.Expiration);
            }
            public async Task NewObject_SetsCountToOneWithExpirationAsync()
            {
                // Arrange
                var key     = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                              .Limit(1)
                              .Over(100);
                var cache      = new MemoryCache(new MemoryCacheOptions());
                var repository = new MemoryThrottleRepository(cache);

                repository.CurrentDate = () => new DateTime(2030, 1, 1);

                string id = await repository.CreateThrottleKeyAsync(key, limiter);

                // Act
                await repository.AddOrIncrementWithExpirationAsync(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);

                Assert.Equal(1L, item.Count);
                // We're testing a future date by 100 seconds which is 40 seconds + 1 minute
                Assert.Equal(new DateTime(2030, 1, 1, 0, 1, 40), item.Expiration);
            }
            public void SetLock()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                Limiter limiter = new Limiter().Limit(1).Over(1).LockFor(1);
                var db = Substitute.For<IDatabase>();
                var repository = new RedisThrottleRepository(db);
                string id = repository.CreateLockKey(key, limiter);
                var transaction = Substitute.For<ITransaction>();

                db
                    .CreateTransaction()
                    .Returns(transaction);

                // Act
                repository.SetLock(key, limiter);

                // Assert
                transaction
                    .Received(1)
                    .StringIncrementAsync(id);

                transaction
                    .Received(1)
                    .KeyExpireAsync(id, limiter.LockDuration);

                transaction
                    .Received(1)
                    .Execute();
            }
            public void KeyExists_ReturnsTrue(bool keyExists, bool expected)
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                Limiter limiter = new Limiter().Limit(1).Over(1).LockFor(1);
                var db = Substitute.For<IDatabase>();
                var repository = new RedisThrottleRepository(db);
                string id = repository.CreateLockKey(key, limiter);

                db
                    .KeyExists(id)
                    .Returns(keyExists);

                // Act
                bool result = repository.LockExists(key, limiter);

                // Assert
                Assert.Equal(expected, result);
            }
            public void ThrottleCountReturnsNullWhenUsingInvalidKey()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);

                // Act
                var count = repository.GetThrottleCount(key, limiter);

                // Assert
                Assert.Equal(count, null);
            }
            public void RemoveThrottle()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                Limiter limiter = new Limiter().Limit(1).Over(1);
                var db = Substitute.For<IDatabase>();
                var repository = new RedisThrottleRepository(db);
                string id = repository.CreateThrottleKey(key, limiter);

                // Act
                repository.RemoveThrottle(key, limiter);

                // Assert
                db
                    .Received(1)
                    .KeyDelete(id);
            }