Exemple #1
0
        public async Task GetAsync_ReturnsBoolCachedValue()
        {
            // Arrange

            var        key         = Guid.NewGuid().ToString();
            const bool cachedValue = true;

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetAsync <bool>(key);

            // Assert
            Assert.AreEqual(cachedValue, result);
        }
Exemple #2
0
        public void GetIfCached_ReturnsCachedValue()
        {
            // Arrange

            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var wasCached = sut.GetIfCached <AClass>(key, out var result);

            // Assert
            Assert.True(wasCached);
            Assert.AreEqual(cachedValue.Value, result.Value);
        }
Exemple #3
0
        public async Task GetIfCachedAsync_ReturnsCachedValue()
        {
            // Arrange

            var key         = Guid.NewGuid().ToString();
            var cachedValue = new AClass();

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetIfCachedAsync <AClass>(key);

            // Assert
            Assert.True(result.wasInCache);
            Assert.AreEqual(cachedValue.Value, result.value.Value);
        }
Exemple #4
0
        public async Task GetOrCreateAsync_ReturnsBoolCachedValue()
        {
            // Arrange

            var        key         = Guid.NewGuid().ToString();
            const bool cachedValue = true;
            Func <string, Task <bool> > factory = _ => Task.FromResult(false);
            var options = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = await sut.GetOrCreateAsync(key, options, factory);

            // Assert
            Assert.AreEqual(cachedValue, result);
        }
Exemple #5
0
        public void GetOrCreate_ReturnsIntCachedValue()
        {
            // Arrange

            var                   key         = Guid.NewGuid().ToString();
            const int             cachedValue = 1234;
            Func <string, object> factory     = _ => 4321;
            var                   options     = new CacheEntryOptions(TimeSpan.FromMilliseconds(1));

            var cache = new FakeDistributedCache(new MemoryCache(new MemoryCacheOptions()));

            cache.SetString(key, JsonConvert.SerializeObject(cachedValue));

            var logger = Substitute.For <ILogger <DistributedCache> >();
            var sut    = new DistributedCache(cache, logger);


            // Act
            var result = sut.GetOrCreate(key, typeof(int), options, factory);

            // Assert
            Assert.AreEqual(cachedValue, result);
        }