Esempio n. 1
0
        public async Task AwaitTaskOfCacheItem_TaskEmitsValueOfFactory()
        {
            object created   = new object();
            var    cacheItem =
                new LazyAwaitableCacheItem <object>(() => Task.FromResult(created));

            var result = await cacheItem.GetTask();

            Assert.AreSame(created, result);
        }
Esempio n. 2
0
        public void SynchronouslyEvaluateTaskOfCacheItem_TaskEmitsValueOfFactory()
        {
            object created   = new object();
            var    cacheItem =
                new LazyAwaitableCacheItem <object>(() => Task.FromResult(created));

            var result = cacheItem.GetTask().Result;

            Assert.AreSame(created, result);
        }
Esempio n. 3
0
        public async Task ResetLazyCahceItemFactory_FactoryIsInvokedAgain()
        {
            object createdFirst  = new object(),
                   createdSecond = new object();
            var mock             = new Mock <ICacheItemFactory <object> >();

            mock.SetupSequence(m => m.Create())
            .Returns(createdFirst)
            .Returns(createdSecond);
            Func <Task <object> > mockedFactory = () => Task.FromResult(mock.Object.Create());
            var cacheItem = new LazyAwaitableCacheItem <object>(() => mockedFactory());

            var resultYieldFirst = await cacheItem.GetTask();

            cacheItem.Reset();
            var resultYieldSecond = await cacheItem.GetTask();

            Assert.Multiple(() => {
                Assert.AreSame(createdFirst, resultYieldFirst);
                Assert.AreSame(createdSecond, resultYieldSecond);
            });
        }
        public async Task TryRemoveAdvanced_ItemInCache_ReturnsTrue()
        {
            Cache <string> cache = new Cache <string>(
                TimeSpan.FromSeconds(10));
            string key = "key";
            string valueToPutInCache = "ValueOfKey1";
            LazyAwaitableCacheItem <string> removedInstance = null;

            string encachedInitially = await cache.GetOrCreateItem(
                key,
                () => Task.FromResult(valueToPutInCache));

            bool tryRemoveResult = cache.TryRemove(key, out removedInstance);

            Assert.IsTrue(tryRemoveResult);
            Assert.IsNull(await cache.GetItem(key));
            Assert.AreEqual(encachedInitially, await removedInstance.GetTask());
        }
Esempio n. 5
0
        public async Task TryRemove_ItemInCache_ReturnsItemAsOutParameterAndEmitsTrue()
        {
            string key  = "mykey";
            object item = new object();
            var    instanceUnderTest =
                this.CreateInstanceUnderTest <object>()
                .AddItem(key, item);
            LazyAwaitableCacheItem <object> yieldOutParameter = null;

            bool   removeSucceeded  = instanceUnderTest.TryRemove(key, out yieldOutParameter);
            object awaitedCacheItem = await yieldOutParameter.GetTask();

            Assert.Multiple(() =>
            {
                Assert.IsTrue(removeSucceeded);
                Assert.AreSame(item, awaitedCacheItem);
            });
        }