Пример #1
0
        public void Clear_removes_all_entries()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            cache.Clear();

            Assert.False(cache.TryGetValue(1, out _));
            Assert.False(cache.TryGetValue(2, out _));
            Assert.False(cache.TryGetValue(3, out _));
        }
Пример #2
0
        public void TryRemove_removes_existing_entries()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryRemove(1, out var removed));
            Assert.AreEqual("one", removed);
            Assert.False(cache.TryGetValue(1, out _));

            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));
        }
Пример #3
0
        public void TryRemove_returns_false_for_unknown_entry()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");

            Assert.False(cache.TryRemove(2, out var removed));
            Assert.Null(removed);
        }
Пример #4
0
        public void Add_expires_last_used_entry_when_at_capacity()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));

            cache.Add(4, "four");

            Assert.False(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(4, out _));
        }
Пример #5
0
        public void TryGetValue_renews_lifespan_of_entry()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(1, out _));

            cache.Add(4, "four");

            Assert.True(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.False(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(4, out _));
        }
Пример #6
0
        public void when_TryGetValue_called_with_nonexistent_key__should_return_false()
        {
            var mruCache = new MruCache<ValueWrapper<int>, object>(12);

            object added = new object();
            var key = new ValueWrapper<int>(0);

            mruCache.Add(key, added);

            object res;
            Assert.IsFalse(mruCache.TryGetValue(new ValueWrapper<int>(1), out res));
            Assert.IsNull(res);
        }
Пример #7
0
        public void when_TryGetValue_called_with_existing_key__should_return_what_it_was_provided()
        {
            var mruCache = new MruCache<ValueWrapper<int>, object>(12);

            object added = new object();
            var key = new ValueWrapper<int>(0);

            mruCache.Add(key, added);

            object res;
            Assert.IsTrue(mruCache.TryGetValue(new ValueWrapper<int>(0), out res));
            Assert.AreEqual(added, res);
        }
Пример #8
0
        private void AddCallAndUpdateEaCache(string destinationUri, Call call)
        {
            _activeCalls.Add(call.Id, call);
            CallStateChangedEventArgs ea;

            if (!_eaCache.TryGetValue(new ValueWrapper <int>(call.Id), out ea))
            {
                ea = new CallStateChangedEventArgs {
                    Id = call.Id
                };
                _eaCache.Add(new ValueWrapper <int>(call.Id), ea);
            }
            ea.DestinationUri = destinationUri;
        }
Пример #9
0
        public void when_TryGetValue_called_with_nonexistent_key__should_return_false()
        {
            var mruCache = new MruCache <ValueWrapper <int>, object>(12);

            object added = new object();
            var    key   = new ValueWrapper <int>(0);

            mruCache.Add(key, added);

            object res;

            Assert.IsFalse(mruCache.TryGetValue(new ValueWrapper <int>(1), out res));
            Assert.IsNull(res);
        }
Пример #10
0
        public void when_TryGetValue_called_with_existing_key__should_return_what_it_was_provided()
        {
            var mruCache = new MruCache <ValueWrapper <int>, object>(12);

            object added = new object();
            var    key   = new ValueWrapper <int>(0);

            mruCache.Add(key, added);

            object res;

            Assert.IsTrue(mruCache.TryGetValue(new ValueWrapper <int>(0), out res));
            Assert.AreEqual(added, res);
        }
Пример #11
0
        public static Func <object[], TResult> GetFunc <TResult>
        (
            CodeActivityMetadata metadata
            , ConstructorInfo constructorInfo
            , MruCache <ConstructorInfo, Func <object[], TResult> > cache
            , ReaderWriterLockSlim locker
        )
        {
            Func <object[], TResult> func = null;

            if (constructorInfo != null)
            {
                locker.EnterWriteLock();
                try
                {
                    cache.TryGetValue(constructorInfo, out func);
                }
                finally
                {
                    locker.ExitWriteLock();
                }
            }
            if (func == null)
            {
                func = GetFunc <TResult>(metadata, constructorInfo);
                if (constructorInfo != null)
                {
                    locker.EnterWriteLock();
                    try
                    {
                        //MruCache has on ContainsKey(), so we use TryGetValue()
                        Func <object[], TResult> result = null;
                        if (!cache.TryGetValue(constructorInfo, out result))
                        {
                            cache.Add(constructorInfo, func);
                        }
                        else
                        {
                            func = result;
                        }
                    }
                    finally
                    {
                        locker.ExitWriteLock();
                    }
                }
            }
            return(func);
        }
        internal static Func <object, object[], object> GetFunc(CodeActivityMetadata metadata, MethodInfo methodInfo,
                                                                MruCache <MethodInfo, Func <object, object[], object> > cache, ReaderWriterLockSlim locker, bool valueTypeReference = false)
        {
            Func <object, object[], object> func = null;

            locker.EnterWriteLock();
            try
            {
                cache.TryGetValue(methodInfo, out func);
            }
            finally
            {
                locker.ExitWriteLock();
            }
            if (func == null)
            {
                func = GetFunc(metadata, methodInfo, valueTypeReference);
                locker.EnterWriteLock();
                try
                {
                    //MruCache has on ContainsKey(), so we use TryGetValue()
                    Func <object, object[], object> result = null;
                    if (!cache.TryGetValue(methodInfo, out result))
                    {
                        cache.Add(methodInfo, func);
                    }
                    else
                    {
                        func = result;
                    }
                }
                finally
                {
                    locker.ExitWriteLock();
                }
            }
            return(func);
        }