Exemplo n.º 1
0
        public void UpdateWhenRegionNameTest()
        {
            const string REGION = "Test";

            var _cache = new CollectionCache<object, object>();
            var _key = new object();
            var _value1 = new object();

            var _addOrGetExisting = _cache.AddOrGetExisting(_key, _value1, REGION);
            Assert.AreEqual(_value1, _addOrGetExisting);

            var _value2 = new KeyValuePair<object, object>(_key, new object());
            var _value = _cache.Update(_key, _value2, REGION);

            Assert.AreNotEqual(_addOrGetExisting, _value);
            Assert.AreEqual(_value2, _value);
        }
Exemplo n.º 2
0
        public void UpdateWhenCacheItemPolicyTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value1 = new object();

            var _addOrGetExisting = _cache.AddOrGetExisting(KEY, _value1);
            Assert.AreEqual(_value1, _addOrGetExisting);

            var _value2 = new object();
            var _value = _cache.Update(KEY, _value2, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMilliseconds(20) });

            Assert.AreNotEqual(_addOrGetExisting, _value);
            Assert.AreEqual(_value2, _value);

            Thread.Sleep(50);

            Assert.IsNull(_cache.Get(KEY).FirstOrDefault(_x => _x == _value));
        }
Exemplo n.º 3
0
        public void UpdateStressTest()
        {
            const int LOOPS1 = 3;
            const int LOOPS2 = 500;

            var _dictionary = new Dictionary<string, object>();
            var _cache = new CollectionCache<string, object>();

            for (var _j = 1; _j <= LOOPS2; _j++)
            {
                var _key = "Test" + _j;
                var _value = new object();
                _cache.AddOrGetExisting(_key, _value);
                _dictionary.Add(_key, _value);
            }

            for (var _i = 0; _i < LOOPS1; _i++)
            {
                var _dateTimeOffSet = DateTimeOffset.UtcNow;

                for (var _j = 1; _j <= LOOPS2; _j++)
                {
                    var _key = _dictionary.Where(_x => _x.Value == _dictionary["Test" + _j]).Select(_x => _x.Key).FirstOrDefault();
                    var _value = new object();
                    _cache.Update(_key, _value);
                }

                var _elapsedTime = (DateTimeOffset.UtcNow - _dateTimeOffSet).TotalMilliseconds;
                Assert.LessOrEqual(_elapsedTime, 150);
            }
        }
Exemplo n.º 4
0
        public void UpdateTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value1 =new object();

            var _addOrGetExisting = _cache.AddOrGetExisting(KEY, _value1);
            Assert.AreEqual(_value1, _addOrGetExisting);

            var _value2 = new KeyValuePair<string, object>(KEY, new object());
            var _value = _cache.Update(KEY, _value2);

            Assert.AreNotEqual(_addOrGetExisting, _value);
            Assert.AreEqual(_value2, _value);
        }
Exemplo n.º 5
0
        public void UpdateStressAsyncTest()
        {
            const int LOOPS1 = 3;
            const int LOOPS2 = 500;

            var _dictionary = new Dictionary<string, object>();
            var _cache = new CollectionCache<string, object>();

            for (var _j = 1; _j <= LOOPS2; _j++)
            {
                var _key = "Test" + _j;
                var _value = new object();
                _cache.AddOrGetExisting(_key, _value);
                _dictionary.Add(_key, _value);
            }

            for (var _i = 0; _i < LOOPS1; _i++)
            {
                var _dateTimeOffSet = DateTimeOffset.UtcNow;
                Parallel.ForEach(_dictionary, _x =>
                {
                    var _value = new object();
                    _cache.Update(_x.Key, _value);
                });

                var _elapsedTime = (DateTimeOffset.UtcNow - _dateTimeOffSet).TotalMilliseconds;
                Assert.LessOrEqual(_elapsedTime, 150);
            }
        }