Example #1
0
        public void Get_NonExistentKey_ReturnsNull()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var result = sot.Get("SomeNoneExistantKey");

            Assert.Null(result);
        }
Example #2
0
        public void SetAndGet_KeysAreCaseSensitive_ReturnsSetValue()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value = new byte[1];

            sot.Set("MyKey", value);

            var result = sot.Get("mykey");

            Assert.Null(result);
        }
Example #3
0
        public void SetAndGet_KeyAndValue_ReturnsSetValue()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value = new byte[1];

            sot.Set("MyKey", value);

            var result = sot.Get("MyKey");

            Assert.Equal(value, result);
        }
Example #4
0
        public void Set_WithSlidingExpiration_GetReturnsNullAfterSlidingExperationTimeSpan()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value = new byte[1];

            sot.Set("MyKey", value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(1)));

            var result = sot.Get("MyKey");

            Assert.Equal(result, value);

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

            result = sot.Get("MyKey");

            Assert.Null(result);
        }
Example #5
0
        public void Remove_RemovesValue()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value = new byte[1];

            sot.Set("MyKey", value);

            var result = sot.Get("MyKey");

            Assert.Equal(value, result);

            sot.Remove("MyKey");

            var result2 = sot.Get("MyKey");

            Assert.Null(result2);
        }
Example #6
0
        public async Task SetAsync_OverwritesExistingValue_ReturnsNewValue()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value  = new byte[1];
            var value2 = new byte[2];

            await sot.SetAsync("MyKey", value);

            var result = sot.GetAsync("MyKey");

            Assert.Equal(value, await result);

            await sot.SetAsync("MyKey", value2);

            var result2 = sot.GetAsync("MyKey");

            Assert.Equal(await result2, value2);
            Assert.NotEqual(await result, value2);
        }
Example #7
0
        public void Set_OverwritesExitingValue_ReturnsNewValue()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value  = new byte[1];
            var value2 = new byte[2];

            sot.Set("MyKey", value);

            var result = sot.Get("MyKey");

            Assert.Equal(value, result);

            sot.Set("MyKey", value2);

            var result2 = sot.Get("MyKey");

            Assert.Equal(result2, value2);
            Assert.NotEqual(result, value2);
        }
Example #8
0
        public void Set_WithSlidingExpiration_ReturnsValueWhenAccessedRefreshingExpiresAt()
        {
            var sot = new MongoDistributedCache(_options, new InMemoryMongoAccessor());

            var value = new byte[1];

            sot.Set("MyKey", value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(1)));

            for (int i = 0; i < 4; i++)
            {
                var result1 = sot.Get("MyKey");
                Assert.Equal(value, result1);
                Thread.Sleep(TimeSpan.FromSeconds(.5));
            }

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

            var result2 = sot.Get("MyKey");

            Assert.Null(result2);
        }