Esempio n. 1
0
            public async Task Get_From_Cache_If_Cache_Miss_But_Queued_Late()
            {
                // Arrange
                var hit          = false;
                var providerMock = new Mock <ICacheProvider>();

                providerMock.Setup(m => m.TryGet <string>("key"))
                .Returns((string _) =>
                {
                    if (hit)
                    {
                        return(Task.FromResult(CachedValueResult <string> .Hit("cached_value")));
                    }

                    hit = true;
                    return(Task.FromResult(CachedValueResult <string> .Miss));
                });

                var lockMock = new Mock <ILock>();
                var handler  = new CacheHandler <ICacheProvider>(providerMock.Object, lockMock.Object);

                // Act
                var result = await handler.GetOrFetchAsync(
                    "key",
                    key => Task.FromResult(key + "_fetched"));

                // Assert
                Assert.Equal("cached_value", result);
                lockMock.Verify(l => l.LockAsync(It.IsAny <object>()), Times.Once);
                lockMock.Verify(l => l.LockAsync("key"), Times.Once);
                providerMock.Verify(m => m.Set(It.IsAny <string>(), It.IsAny <object>()), Times.Never);
                providerMock.Verify(m => m.TryGet <It.IsAnyType>(It.IsAny <string>()), Times.Exactly(2));
                providerMock.Verify(m => m.TryGet <string>("key"), Times.Exactly(2));
            }
Esempio n. 2
0
        public Task <CachedValueResult <T> > TryGet <T>(string key)
        {
            EnsureNotDisposed();

            if (_memoryCache.TryGetValue(key, out object dataFromCache) && dataFromCache is T castItem)
            {
                return(Task.FromResult(CachedValueResult <T> .Hit(castItem)));
            }

            return(Task.FromResult(CachedValueResult <T> .Miss));
        }
Esempio n. 3
0
            public void Create_Successful_Result_Instance_With_Matching_Value()
            {
                // Arrange
                const string value = "abc123";

                // Act
                var result = CachedValueResult <string> .Hit(value);

                // Assert
                Assert.True(result.Succeeded);
                Assert.Equal(value, result.Value);
            }
Esempio n. 4
0
            public async Task Gets_Cached_Item_When_Exist()
            {
                // Arrange
                var providerMock = new Mock <ICacheProvider>();

                providerMock.Setup(m => m.TryGet <string>("key"))
                .Returns(Task.FromResult(CachedValueResult <string> .Hit("cached_value")));
                var lockMock = new Mock <ILock>();
                var handler  = new CacheHandler <ICacheProvider>(providerMock.Object, lockMock.Object);

                // Act
                var result = await handler.GetOrFetchAsync(
                    "key",
                    key => Task.FromResult(key + "_fetched"));

                // Assert
                Assert.Equal("cached_value", result);
                lockMock.Verify(l => l.LockAsync(It.IsAny <object>()), Times.Never);
                providerMock.Verify(m => m.TryGet <It.IsAnyType>(It.IsAny <string>()), Times.Once);
                providerMock.Verify(m => m.TryGet <string>("key"), Times.Once);
                providerMock.Verify(m => m.Set(It.IsAny <string>(), It.IsAny <object>()), Times.Never);
            }