public void SlidingExpirationRenewedByAccessUntilAbsoluteExpiration()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            cache.Set(key, value, new StreamCacheEntryOptions()
                      .SetSlidingExpiration(TimeSpan.FromSeconds(1))
                      .SetAbsoluteExpiration(TimeSpan.FromSeconds(3)));

            var result = cache.Get(key);

            Assert.Equal(value, result);

            for (var i = 0; i < 5; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));

                result = cache.Get(key);
                Assert.Equal(value, result);
            }

            Thread.Sleep(TimeSpan.FromSeconds(.6));

            result = cache.Get(key);
            Assert.Null(result);
        }
Пример #2
0
        public void SetNullValueThrows()
        {
            var          cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);
            MemoryStream value = null;
            var          key   = "myKey";

            Assert.Throws <ArgumentNullException>(() => cache.Set(key, value));
        }
Пример #3
0
        public void GetMissingKeyReturnsNull()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "non-existent-key";

            var result = cache.Get(key);

            Assert.Null(result);
        }
Пример #4
0
        public void SetAndGetReturnsObject()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));
            var key   = "myKey";

            cache.Set(key, value);

            var result = cache.Get(key);

            Assert.Equal(value, result);
        }
        public void ZeroRelativeExpirationThrows()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            Assert.Throws <ArgumentOutOfRangeException>(nameof(StreamCacheEntryOptions.AbsoluteExpirationRelativeToNow), () =>
            {
                cache.Set(key, value, new StreamCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.Zero));
            }); //"The relative expiration value must be positive.", TimeSpan.Zero);
        }
        public void NegativeSlidingExpirationThrows()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            Assert.Throws <ArgumentOutOfRangeException>(nameof(StreamCacheEntryOptions.SlidingExpiration), () =>
            {
                cache.Set(key, value, new StreamCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(-1)));
            }); // "The sliding expiration value must be positive.", TimeSpan.FromMinutes(-1));
        }
        public void AbsoluteSubSecondExpirationExpiresImmidately()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            cache.Set(key, value, new StreamCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(0.25)));

            var result = cache.Get(key);

            Assert.Null(result);
        }
        public void AbsoluteExpirationInThePastThrows()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            var expected = DateTimeOffset.Now - TimeSpan.FromMinutes(1);

            Assert.Throws <ArgumentOutOfRangeException>(nameof(StreamCacheEntryOptions.AbsoluteExpiration), () =>
            {
                cache.Set(key, value, new StreamCacheEntryOptions().SetAbsoluteExpiration(expected));
            }); //"The absolute expiration value must be in the future.", expected);
        }
Пример #9
0
        public void RemoveRemoves()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));
            var key   = "myKey";

            cache.Set(key, value);
            var result = cache.Get(key);

            Assert.Equal(value, result);

            cache.Remove(key);
            result = cache.Get(key);
            Assert.Null(result);
        }
        public void SlidingExpirationExpiresIfNotAccessed()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            cache.Set(key, value, new StreamCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(1)));

            var result = cache.Get(key);

            Assert.Equal(value, result);

            Thread.Sleep(TimeSpan.FromSeconds(3));

            result = cache.Get(key);
            Assert.Null(result);
        }
        public void RelativeExpirationExpires()
        {
            var cache = FileStreamTestConfig.CreateCacheInstance(GetType().Name);

            var key   = "myKey";
            var value = new StreamWithHeader(new MemoryStream(new byte[1]));

            cache.Set(key, value, new StreamCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1)));

            var result = cache.Get(key);

            Assert.Equal(value, result);

            for (var i = 0; i < 4 && result != null; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                result = cache.Get(key);
            }
            Assert.Null(result);
        }
Пример #12
0
        public void SetAlwaysOverwrites()
        {
            var cache  = FileStreamTestConfig.CreateCacheInstance(GetType().Name);
            var value1 = new StreamWithHeader(new MemoryStream(new byte[1] {
                1
            }));
            var key = "myKey";

            cache.Set(key, value1);
            var result = cache.Get(key);

            Assert.Equal(value1, result);

            var value2 = new StreamWithHeader(new MemoryStream(new byte[1] {
                2
            }));

            cache.Set(key, value2);
            result = cache.Get(key);
            Assert.Equal(value2, result);
        }