Exemplo n.º 1
0
        public void NullCacheThrowsException()
        {
            var mock = new MockDataRetriever();
            var ex   = Record.Exception(() => new ResultCache <string>(Key, mock.GetData, null));

            Assert.IsType <ArgumentNullException>(ex);
        }
Exemplo n.º 2
0
        public void EmptyKeyThrowsException()
        {
            var mock = new MockDataRetriever();
            var ex   = Record.Exception(() => new ResultCache <string>("", mock.GetData, GetCache()));

            Assert.IsType <ArgumentException>(ex);
        }
Exemplo n.º 3
0
        public async Task ResultFailReturnsDefaultObject()
        {
            var mock   = new MockDataRetriever();
            var cache  = new ResultCache <string>(Key, mock.GetFailData, GetCache());
            var result = await cache.Get();

            Assert.Null(result);
        }
Exemplo n.º 4
0
        public async Task DataIsRetrievedOnceOnFirstTimeUse()
        {
            var mock       = new MockDataRetriever();
            var cache      = new ResultCache <string>(Key, mock.GetData, GetCache());
            var cachedData = await cache.Get();

            Assert.Equal(1, mock.DataRetrievalCount);
            Assert.Equal("this is the data", cachedData);
        }
Exemplo n.º 5
0
        public void PoisonDisposesTheCache()
        {
            var mock  = new MockDataRetriever();
            var cache = new ResultCache <string>(Key, mock.GetData, GetCache());

            cache.Get();

            Assert.Equal(1, mock.DataRetrievalCount);
            cache.Poison();
            cache.Get();
            Assert.Equal(2, mock.DataRetrievalCount);
        }
Exemplo n.º 6
0
        public async Task WhenCacheIsEmptyItReRetrievesTheData()
        {
            var mock            = new MockDataRetriever();
            var underlyingCache = GetCache();
            var cache           = new ResultCache <string>(Key, mock.GetData, underlyingCache);

            underlyingCache.Remove(Key);

            var cachedData = await cache.Get();

            Assert.Equal(1, mock.DataRetrievalCount);
            Assert.Equal("this is the data", cachedData);
        }
Exemplo n.º 7
0
        public async Task CachedDataIsRetrievedOnSubsequentUse()
        {
            var mock    = new MockDataRetriever();
            var cache   = new ResultCache <string>(Key, mock.GetData, GetCache());
            var result1 = await cache.Get();

            var result2 = await cache.Get();

            var result3 = await cache.Get();

            var result4 = await cache.Get();

            var result5 = await cache.Get();

            Assert.Equal(1, mock.DataRetrievalCount);
            Assert.True(new[] { result1, result2, result3, result4, result5 }.All(o => o.Equals("this is the data")));
        }
Exemplo n.º 8
0
        public async Task SeperateInstancesWithSameKey()
        {
            var mock    = new MockDataRetriever();
            var cache   = new ResultCache <string>(Key, mock.GetData, GetCache());
            var cache2  = new ResultCache <string>(Key, mock.GetData, GetCache());
            var result1 = await cache.Get();

            var result2 = await cache.Get();

            var result3 = await cache.Get();

            var result4 = await cache2.Get();

            var result5 = await cache2.Get();

            var result6 = await cache2.Get();

            Assert.Equal(2, mock.DataRetrievalCount); // we expect 2 because the data will be re-retrieved when newing up the second instance
            Assert.True(new[] { result1, result2, result3, result4, result5, result6 }.All(o => o.Equals("this is the data")));
        }
Exemplo n.º 9
0
        public async Task WhenCacheIsEmptyItReRetrievesTheDataAndCachesIt()
        {
            var mock            = new MockDataRetriever();
            var underlyingCache = GetCache();
            var cache           = new ResultCache <string>(Key, mock.GetData, underlyingCache);

            underlyingCache.Remove(Key);

            var result1 = await cache.Get();

            var result2 = await cache.Get();

            var result3 = await cache.Get();

            var result4 = await cache.Get();

            var result5 = await cache.Get();

            Assert.Equal(1, mock.DataRetrievalCount);
            Assert.True(new[] { result1, result2, result3, result4, result5 }.All(o => o.Equals("this is the data")));
        }