Пример #1
0
        /// <summary>
        /// Removes all keys and values from the <paramref name="cache"/>.
        /// </summary>
        /// <param name="cache">The cache to clear.</param>
        /// <param name="scheduler">Scheduler to perform the clear action on.</param>
        /// <returns>
        /// An observable stream that signals when finished clear with an <see cref="Unit" />.
        /// </returns>
        public static IObservable <Unit> Clear <TKey, TValue>(this IObservableCache <TKey, TValue> cache, IScheduler scheduler = null)
        {
            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            return(cache.Clear(Observable.Return(Unit.Default), scheduler));
        }
        public void ConverterObservableCacheTests()
        {
            IObservableCache  cache    = InstantiateCache();
            IConverter        cDown    = new ConvertDown();
            IConverter        cUp      = new ConvertUp();
            TestCacheListener listener = new TestCacheListener();

            IObservableCache convCache = ConverterCollections.GetObservableCache(cache, cUp, cDown, cUp, cDown);

            Assert.IsNotNull(convCache);
            //AddCacheListener(listener)
            convCache.AddCacheListener(listener);
            Assert.AreEqual(listener.inserted, 0);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 0);
            convCache[1] = 2;
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Remove(1);
            Assert.AreEqual(listener.deleted, 1);
            //RemoveCacheListener(listener)
            convCache.RemoveCacheListener(listener);
            Assert.AreEqual(listener.inserted, 1);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);

            listener.inserted = listener.updated = listener.deleted = 0;
            convCache.Clear();
            //AddCacheListener(listener, key, isLite);
            convCache.AddCacheListener(listener, "1", true);
            for (int i = 0; i < 3; i++)
            {
                convCache.Add(i, i);
            }
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 0);
            Assert.AreEqual(listener.deleted, 0);
            for (int i = 0; i < 3; i++)
            {
                convCache[i] = i + 1;
            }
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Clear();
            Assert.AreEqual(listener.inserted, 1);
            Assert.AreEqual(listener.updated, 1);
            Assert.AreEqual(listener.deleted, 1);
            //RemoveCacheListener(listener, key)
            convCache.RemoveCacheListener(listener, "1");
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 1);

            listener.inserted = listener.updated = listener.deleted = 0;
            convCache.Clear();
            IFilter filter = AlwaysFilter.Instance;

            //AddCacheListener(listener, filter, isLite)
            convCache.AddCacheListener(listener, filter, true);
            for (int i = 0; i < 3; i++)
            {
                convCache.Add(i, i);
            }
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 0);
            Assert.AreEqual(listener.deleted, 0);
            for (int i = 0; i < 3; i++)
            {
                convCache[i] = i + 1;
            }
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 3);
            Assert.AreEqual(listener.deleted, 0);
            convCache.Clear();
            Assert.AreEqual(listener.inserted, 3);
            Assert.AreEqual(listener.updated, 3);
            Assert.AreEqual(listener.deleted, 3);
            //RemoveCacheListener(listener, filter)
            convCache.RemoveCacheListener(listener, filter);
            convCache.Add(1, 1);
            Assert.AreEqual(listener.inserted, 3);

            Assert.IsInstanceOf(typeof(ConverterCollections.ConverterObservableCache), convCache);
            ConverterCollections.ConverterObservableCache coc =
                convCache as ConverterCollections.ConverterObservableCache;
            Assert.IsNotNull(coc);
            Assert.AreEqual(coc.ObservableCache, cache);
        }