Ejemplo n.º 1
0
        public async Task RemoveExpiredCacheEntriesAsync_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                // Prepare
                await using (var sqliteCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <SqliteCacheContext> >().CreateDbContext())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        sqliteCacheContext.Cache.Add(new CacheEntry <DateTime>
                        {
                            ExpiresAtTime = now.AddYears(i).UtcDateTime,
                            Id            = i.ToString(CultureInfo.InvariantCulture),
                            Value         = new byte[i]
                        });
                    }

                    Assert.AreEqual(10, await sqliteCacheContext.SaveChangesAsync());
                }

                now = now.AddYears(4).AddMonths(6);
                serviceProvider.SetTime(now);
                var sqliteCache = (SqliteCache)serviceProvider.GetRequiredService <IDistributedCache>();

                Assert.AreEqual(5, await sqliteCache.RemoveExpiredCacheEntriesAsync());

                await using (var sqliteCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <SqliteCacheContext> >().CreateDbContext())
                {
                    Assert.AreEqual(5, await sqliteCacheContext.Cache.CountAsync());
                    Assert.AreEqual(5, await sqliteCacheContext.Cache.CountAsync(cacheEntry => cacheEntry.ExpiresAtTime > now.UtcDateTime));
                }
            }
        }
Ejemplo n.º 2
0
        public async Task SetAsync_Test()
        {
            const string key = "Key";

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                serviceProvider.SetTime(await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000));

                var sqliteCache = (SqliteCache)serviceProvider.GetRequiredService <IDistributedCache>();
                await sqliteCache.SetAsync(key, Array.Empty <byte>());

                await using (var sqliteCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <SqliteCacheContext> >().CreateDbContext())
                {
                    Assert.AreEqual(1, await sqliteCacheContext.Cache.CountAsync());

                    var cacheEntry = await sqliteCacheContext.Cache.FindAsync(key);

                    Assert.IsNull(cacheEntry.AbsoluteExpiration);
                    Assert.AreEqual(await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000, minute: 20), cacheEntry.ExpiresAtTime);
                    Assert.AreEqual(key, cacheEntry.Id);
                    Assert.AreEqual(Convert.ToInt64(TimeSpan.FromMinutes(20).TotalSeconds), cacheEntry.SlidingExpirationInSeconds);
                    Assert.IsTrue(Array.Empty <byte>().SequenceEqual(cacheEntry.Value));
                }
            }
        }
Ejemplo n.º 3
0
        public async Task GetAsync_Test()
        {
            const string key = "Key";

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                var sqliteCache = (SqliteCache)serviceProvider.GetRequiredService <IDistributedCache>();
                var value       = await sqliteCache.GetAsync(key);

                Assert.IsNull(value);

                await sqliteCache.SetAsync(key, Array.Empty <byte>());

                value = await sqliteCache.GetAsync(key);

                Assert.IsNotNull(value);

                var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

                serviceProvider.SetTime(now);
                await sqliteCache.SetAsync(key, Array.Empty <byte>(), new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromSeconds(1) });

                serviceProvider.SetTime(now.AddSeconds(2));
                value = await sqliteCache.GetAsync(key);

                Assert.IsNull(value);
            }
        }
Ejemplo n.º 4
0
        public async Task RefreshAsync_Test()
        {
            const string key = "Key";

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

                serviceProvider.SetTime(now);
                var sqliteCache = (SqliteCache)serviceProvider.GetRequiredService <IDistributedCache>();

                await sqliteCache.SetAsync(key, Array.Empty <byte>(), new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromSeconds(1) });

                await using (var sqliteCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <SqliteCacheContext> >().CreateDbContext())
                {
                    var cacheEntry = await sqliteCacheContext.Cache.FindAsync(key);

                    Assert.AreEqual(now.UtcDateTime.AddSeconds(1), cacheEntry.ExpiresAtTime);
                }

                serviceProvider.SetTime(now.AddSeconds(1));
                await sqliteCache.RefreshAsync(key);

                await using (var sqliteCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <SqliteCacheContext> >().CreateDbContext())
                {
                    var cacheEntry = await sqliteCacheContext.Cache.FindAsync(key);

                    Assert.AreEqual(now.UtcDateTime.AddSeconds(2), cacheEntry.ExpiresAtTime);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task Refresh_Test()
        {
            const string key = "Key";

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

                serviceProvider.SetTime(now);
                var dateTimeOffsetContextCache = (DateTimeOffsetCacheMock)serviceProvider.GetRequiredService <IDistributedCache>();

                // ReSharper disable All

                dateTimeOffsetContextCache.Set(key, Array.Empty <byte>(), new DistributedCacheEntryOptions {
                    SlidingExpiration = TimeSpan.FromSeconds(1)
                });
                using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    var cacheEntry = dateTimeOffsetCacheContext.Cache.Find(key);
                    Assert.AreEqual(now.AddSeconds(1), cacheEntry.ExpiresAtTime);
                }

                serviceProvider.SetTime(now.AddSeconds(1));
                dateTimeOffsetContextCache.Refresh(key);
                using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    var cacheEntry = dateTimeOffsetCacheContext.Cache.Find(key);
                    Assert.AreEqual(now.AddSeconds(2), cacheEntry.ExpiresAtTime);
                }

                // ReSharper restore All
            }
        }
Ejemplo n.º 6
0
        public async Task Get_Test()
        {
            const string key = "Key";

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                // ReSharper disable MethodHasAsyncOverload

                var dateTimeOffsetContextCache = (DateTimeOffsetCacheMock)serviceProvider.GetRequiredService <IDistributedCache>();
                var value = dateTimeOffsetContextCache.Get(key);
                Assert.IsNull(value);

                dateTimeOffsetContextCache.Set(key, Array.Empty <byte>());
                value = dateTimeOffsetContextCache.Get(key);
                Assert.IsNotNull(value);

                var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

                serviceProvider.SetTime(now);
                dateTimeOffsetContextCache.Set(key, Array.Empty <byte>(), new DistributedCacheEntryOptions {
                    SlidingExpiration = TimeSpan.FromSeconds(1)
                });
                serviceProvider.SetTime(now.AddSeconds(2));
                value = dateTimeOffsetContextCache.Get(key);
                Assert.IsNull(value);

                // ReSharper restore MethodHasAsyncOverload
            }
        }
Ejemplo n.º 7
0
        public async Task Set_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            await using (var serviceProvider = await this.CreateServiceProviderAsync(now))
            {
                var sqlServerCacheOptions = serviceProvider.GetRequiredService <IOptions <SqlServerCacheOptions> >().Value;
                Assert.AreEqual(TimeSpan.FromMinutes(20), sqlServerCacheOptions.DefaultSlidingExpiration);
                Assert.IsNull(sqlServerCacheOptions.ExpiredItemsDeletionInterval);

                var sqlServerCache = (SqlServerCache)serviceProvider.GetRequiredService <IDistributedCache>();
                // ReSharper disable MethodHasAsyncOverload
                sqlServerCache.Set("1", Array.Empty <byte>(), new DistributedCacheEntryOptions());
                // ReSharper restore MethodHasAsyncOverload

                using (var scope = serviceProvider.CreateScope())
                {
                    var sqlServerCacheContext = scope.ServiceProvider.GetRequiredService <SqlServerCacheContext>();
                    Assert.AreEqual(1, await sqlServerCacheContext.Cache.CountAsync());
                    var cacheEntry = await sqlServerCacheContext.Cache.FindAsync("1");

                    Assert.IsNotNull(cacheEntry);
                    Assert.IsNull(cacheEntry.AbsoluteExpiration);
                    Assert.AreEqual(now.Add(sqlServerCacheOptions.DefaultSlidingExpiration), cacheEntry.ExpiresAtTime);
                    Assert.AreEqual("1", cacheEntry.Id);
                    Assert.AreEqual((uint)sqlServerCacheOptions.DefaultSlidingExpiration.TotalSeconds, cacheEntry.SlidingExpirationInSeconds);
                    Assert.IsTrue(Array.Empty <byte>().SequenceEqual(cacheEntry.Value));
                }
            }
        }
Ejemplo n.º 8
0
        public async Task GetRefreshedExpiration_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            var systemClock = new SystemClockMock
            {
                UtcNow = now
            };

            var contextCache = await this.CreateContextCacheAsync(systemClock : systemClock);

            var initialExpires = now.AddSeconds(10);

            var cacheEntry = new CacheEntry <DateTimeOffset>
            {
                ExpiresAtTime = initialExpires
            };

            Assert.AreEqual(initialExpires, contextCache.GetRefreshedExpiration(cacheEntry));

            cacheEntry = new CacheEntry <DateTimeOffset>
            {
                ExpiresAtTime = initialExpires,
                SlidingExpirationInSeconds = 60
            };
            Assert.AreEqual(now.AddSeconds(60), contextCache.GetRefreshedExpiration(cacheEntry));

            cacheEntry = new CacheEntry <DateTimeOffset>
            {
                AbsoluteExpiration = now.AddHours(5),
                ExpiresAtTime      = initialExpires
            };
            Assert.AreEqual(initialExpires, contextCache.GetRefreshedExpiration(cacheEntry));

            cacheEntry = new CacheEntry <DateTimeOffset>
            {
                AbsoluteExpiration         = now.AddHours(5),
                ExpiresAtTime              = initialExpires,
                SlidingExpirationInSeconds = 60
            };
            Assert.AreEqual(now.AddSeconds(60), contextCache.GetRefreshedExpiration(cacheEntry));

            cacheEntry = new CacheEntry <DateTimeOffset>
            {
                AbsoluteExpiration         = now.AddSeconds(40),
                ExpiresAtTime              = initialExpires,
                SlidingExpirationInSeconds = 60
            };
            Assert.AreEqual(now.AddSeconds(40), contextCache.GetRefreshedExpiration(cacheEntry));
        }
Ejemplo n.º 9
0
        public async Task CleanupIfNecessary_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                // Prepare
                await using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        dateTimeOffsetCacheContext.Cache.Add(new CacheEntry <DateTimeOffset>
                        {
                            ExpiresAtTime = now.AddYears(i),
                            Id            = i.ToString(CultureInfo.InvariantCulture),
                            Value         = new byte[i]
                        });
                    }

                    Assert.AreEqual(10, await dateTimeOffsetCacheContext.SaveChangesAsync());
                }

                now = now.AddYears(4).AddMonths(6);
                serviceProvider.SetTime(now);
                var dateTimeOffsetContextCache = (DateTimeOffsetCacheMock)serviceProvider.GetRequiredService <IDistributedCache>();
                Assert.AreEqual(DateTimeOffset.MaxValue, dateTimeOffsetContextCache.LastCleanup);
                dateTimeOffsetContextCache.LastCleanup = DateTimeOffset.MinValue;
                dateTimeOffsetContextCache.CleanupIfNecessary();
                Thread.Sleep(500);
                Assert.AreEqual(now, dateTimeOffsetContextCache.LastCleanup);

                await using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    Assert.AreEqual(5, await dateTimeOffsetCacheContext.Cache.CountAsync());
                    Assert.AreEqual(5, await dateTimeOffsetCacheContext.Cache.CountAsync(cacheEntry => cacheEntry.ExpiresAtTime > now));
                }
            }
        }
Ejemplo n.º 10
0
        public async Task RemoveExpiredCacheEntries_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            await using (var serviceProvider = await this.CreateServiceProviderAsync())
            {
                // Prepare
                await using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        dateTimeOffsetCacheContext.Cache.Add(new CacheEntry <DateTimeOffset>
                        {
                            ExpiresAtTime = now.AddYears(i),
                            Id            = i.ToString(CultureInfo.InvariantCulture),
                            Value         = new byte[i]
                        });
                    }

                    Assert.AreEqual(10, await dateTimeOffsetCacheContext.SaveChangesAsync());
                }

                now = now.AddYears(4).AddMonths(6);
                serviceProvider.SetTime(now);
                var dateTimeOffsetContextCache = (DateTimeOffsetCacheMock)serviceProvider.GetRequiredService <IDistributedCache>();

                // ReSharper disable MethodHasAsyncOverload
                Assert.AreEqual(5, dateTimeOffsetContextCache.RemoveExpiredCacheEntries());
                // ReSharper restore MethodHasAsyncOverload

                await using (var dateTimeOffsetCacheContext = serviceProvider.GetRequiredService <IDbContextFactory <DateTimeOffsetCacheContext> >().CreateDbContext())
                {
                    Assert.AreEqual(5, await dateTimeOffsetCacheContext.Cache.CountAsync());
                    Assert.AreEqual(5, await dateTimeOffsetCacheContext.Cache.CountAsync(cacheEntry => cacheEntry.ExpiresAtTime > now));
                }
            }
        }
Ejemplo n.º 11
0
        public async Task GetExpirationInformation_Test()
        {
            var now = await DateTimeOffsetHelper.CreateDateTimeOffsetAsync(2000);

            var systemClock = new SystemClockMock
            {
                UtcNow = now
            };

            var contextCache = await this.CreateContextCacheAsync(systemClock : systemClock);

            var expirationInformation = contextCache.GetExpirationInformation(new DistributedCacheEntryOptions());

            Assert.IsNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(now.AddMinutes(20), expirationInformation.Expires);
            Assert.IsNotNull(expirationInformation.SlidingExpirationInSeconds);
            Assert.AreEqual(20 * 60, expirationInformation.SlidingExpirationInSeconds.Value);

            var absoluteExpiration = now.AddDays(2);
            var options            = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = absoluteExpiration
            };

            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNotNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(absoluteExpiration, expirationInformation.AbsoluteExpiration.Value);
            Assert.AreEqual(absoluteExpiration, expirationInformation.Expires);
            Assert.IsNull(expirationInformation.SlidingExpirationInSeconds);

            absoluteExpiration = now.AddDays(5);
            var absoluteExpirationRelativeToNow = TimeSpan.FromDays(5);

            options = new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow
            };
            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNotNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(absoluteExpiration, expirationInformation.AbsoluteExpiration.Value);
            Assert.AreEqual(absoluteExpiration, expirationInformation.Expires);
            Assert.IsNull(expirationInformation.SlidingExpirationInSeconds);

            options = new DistributedCacheEntryOptions
            {
                SlidingExpiration = TimeSpan.FromMinutes(50)
            };
            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(now.AddMinutes(50), expirationInformation.Expires);
            Assert.IsNotNull(expirationInformation.SlidingExpirationInSeconds);
            Assert.AreEqual(50 * 60, expirationInformation.SlidingExpirationInSeconds.Value);

            options = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = now.AddDays(5),
                AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(15),
                SlidingExpiration = TimeSpan.FromMinutes(40)
            };
            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNotNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(now.AddDays(15), expirationInformation.AbsoluteExpiration.Value);
            Assert.AreEqual(now.AddMinutes(40), expirationInformation.Expires);
            Assert.IsNotNull(expirationInformation.SlidingExpirationInSeconds);
            Assert.AreEqual(40 * 60, expirationInformation.SlidingExpirationInSeconds.Value);

            options = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = now.AddDays(5),
                SlidingExpiration  = TimeSpan.FromMinutes(40)
            };
            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNotNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(now.AddDays(5), expirationInformation.AbsoluteExpiration.Value);
            Assert.AreEqual(now.AddMinutes(40), expirationInformation.Expires);
            Assert.IsNotNull(expirationInformation.SlidingExpirationInSeconds);
            Assert.AreEqual(40 * 60, expirationInformation.SlidingExpirationInSeconds.Value);

            options = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = now.AddDays(5),
                AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(15)
            };
            expirationInformation = contextCache.GetExpirationInformation(options);
            Assert.IsNotNull(expirationInformation.AbsoluteExpiration);
            Assert.AreEqual(now.AddDays(15), expirationInformation.AbsoluteExpiration.Value);
            Assert.AreEqual(now.AddDays(15), expirationInformation.Expires);
            Assert.IsNull(expirationInformation.SlidingExpirationInSeconds);
        }