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 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 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); }