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 _));
        }
        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 _));
        }
        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);
        }
        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 _));
        }
        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 _));
        }
        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);
        }
        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);
        }
Beispiel #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;
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
        }