public void GetItem_OfCacheableClassWhenInitialisationTimesOut_ReturnsInitialValueOfNull()
        {
            // Arrange
            AutoRefreshingItemCache <CacheableClass> cache = new AutoRefreshingItemCache <CacheableClass>(
                new FakeLogger(),
                async(ct) => { await TimeDelay.WaitForAsync(500); return(new CacheableClass(100)); },
                null,
                TimeSpan.FromMinutes(2)
                );
            CacheableClass actualResult;

            cache.StartInitialisation();
            cache.TryCompleteInitialisation(TimeSpan.FromMilliseconds(50));

            // Act
            actualResult = cache.GetItem();

            // Assert
            Assert.IsNull(actualResult);
        }
        public void GetItemAsync_OfCacheableClassWhenInitialRetrievalTimesOut_ReturnsDefaultValueOfNull()
        {
            // Arrange
            InMemoryItemCache <CacheableClass> cache = new InMemoryItemCache <CacheableClass>(
                async() => { await TimeDelay.WaitForAsync(200); return(new CacheableClass(100)); },
                null,
                TimeSpan.FromMinutes(2),
                50, 50);
            CacheableClass cacheItem;

            // Act
            Task <CacheableClass> task1 = cache.GetItemAsync();
            Task <CacheableClass> task2 = AwaitDelayAndGetItemFromCacheableClassCacheAsync(50, cache);

            // Start the 2 tasks, with task2 likely to have to wait on task1
            Task.WaitAll(task1, task2);
            cacheItem = task2.Result;

            // Assert
            Assert.IsNull(cacheItem);
        }
        public void TryCompleteInitialisation_VerySlowCacheLoad_TimesOut()
        {
            // Arrange
            AutoRefreshingItemCache <int> cache = new AutoRefreshingItemCache <int>(
                new FakeLogger(),
                new NonCloningClonerFactory <int>(),
                async(ct) => { await TimeDelay.WaitForAsync(30000); return(125); },
                0,
                TimeSpan.FromMinutes(30)
                );
            bool actualResult;
            bool expectedResult = false;

            cache.StartInitialisation();

            // Act
            actualResult = cache.TryCompleteInitialisation(TimeSpan.FromMilliseconds(50));

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
        public void GetItem_OfIntWhenInitialialisationTimesOut_ReturnsInitialValueOfZero()
        {
            // Arrange
            AutoRefreshingItemCache <int> cache = new AutoRefreshingItemCache <int>(
                new FakeLogger(),
                new NonCloningClonerFactory <int>(),
                async(ct) => { await TimeDelay.WaitForAsync(500); return(125); },
                0,
                TimeSpan.FromMinutes(2)
                );
            int actualResult;
            int expectedResult = 0;

            cache.StartInitialisation();
            cache.TryCompleteInitialisation(TimeSpan.FromMilliseconds(50));

            // Act
            actualResult = cache.GetItem();

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
        public void GetItemAsync_OfIntWhenInitialRetrievalTimesOut_ReturnsDefaultValueOfZero()
        {
            // Arrange
            InMemoryItemCache <int> cache = new InMemoryItemCache <int>(
                new NonCloningClonerFactory <int>(),
                async() => { await TimeDelay.WaitForAsync(200); return(125); },
                null,
                TimeSpan.FromMinutes(2),
                50, 50);
            int actualResult;
            int expectedResult = 0;

            // Act
            Task <int> task1 = cache.GetItemAsync();
            Task <int> task2 = AwaitDelayAndGetItemFromIntCacheAsync(50, cache);

            // Start the 2 tasks, with task2 likely to have to wait on task1
            Task.WaitAll(task1, task2);
            actualResult = task2.Result;

            // Assert
            Assert.AreEqual(expectedResult, actualResult);
        }
        private async Task <CacheableClass> AwaitDelayAndGetItemFromCacheableClassCacheAsync(int delayPeriod, InMemoryItemCache <CacheableClass> cache)
        {
            await TimeDelay.WaitForAsync(delayPeriod);

            return(await cache.GetItemAsync());
        }