Exemplo n.º 1
0
        public void AddOrGetExistingStressTest()
        {
            const int LOOPS1 = 3;
            const int LOOPS2 = 500;

            for (var _i = 0; _i < LOOPS1; _i++)
            {
                var _cache = new CollectionCache<string, object>();
                var _dateTimeOffSet = DateTimeOffset.UtcNow;

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

                var _elapsedTime = (DateTimeOffset.UtcNow - _dateTimeOffSet).TotalMilliseconds;
                Assert.LessOrEqual(_elapsedTime, 30);
            }
        }
Exemplo n.º 2
0
        public void AddOrGetExistingAsynctressWhenFastTimeoutTest()
        {
            const int LOOPS1 = 3;
            const int LOOPS2 = 500;

            for (var _i = 0; _i < LOOPS1; _i++)
            {
                var _dictionary = new Dictionary<object, object>();
                var _cache = new CollectionCache<object, object>();

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

                var _dateTimeOffSet = DateTimeOffset.UtcNow;
                Parallel.ForEach(_dictionary, _x => _cache.AddOrGetExisting(_x.Key, _x.Value, DateTimeOffset.UtcNow.AddMilliseconds(50)));

                var _elapsedTime = (DateTimeOffset.UtcNow - _dateTimeOffSet).TotalMilliseconds;
                Assert.LessOrEqual(_elapsedTime, 50);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        public void RemoveTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

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

            _cache.Remove(KEY, _value);

            var _actual = _cache.Get(KEY);
            Assert.IsEmpty(_actual);
        }
Exemplo n.º 5
0
        public void RemoveWhenRegionNameTest()
        {
            const string REGION = "Test";

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

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

            _cache.Remove(_key, _value, REGION);

            var _actual = _cache.Get(_key);
            Assert.IsEmpty(_actual);
        }
Exemplo n.º 6
0
        public void GetWhenRegionNameDoesNotExistsTest()
        {
            const string REGION = "Test";
            const string REGION2 = "Test2";

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

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

            Assert.Throws<KeyNotFoundException>(() => _cache.Get(_key, REGION2));
        }
Exemplo n.º 7
0
        public void GetWhenRegionNameTest()
        {
            const string REGION = "Test";

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

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

            var _actual = _cache.Get(_key, REGION);
            Assert.AreEqual(_value, _actual.FirstOrDefault());
        }
Exemplo n.º 8
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.º 9
0
        public void AddOrGetExistingWhenCacheItemPolicyTest()
        {
            var _cache = new CollectionCache<object, object>();
            var _key = new object();
            var _value = new object();

            var _addOrGetExisting = _cache.AddOrGetExisting(_key, _value, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.UtcNow.AddMilliseconds(20) });
            Assert.AreEqual(_value, _addOrGetExisting);

            Thread.Sleep(80);

            var _actual = _cache.Get(_key);
            Assert.IsEmpty(_actual);
        }
Exemplo n.º 10
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.º 11
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.º 12
0
        public void GetCountWhenRegionAndNoElementsTest()
        {
            const string REGION = "TestC";

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

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

            var _count = _cache.GetCount(_key);
            Assert.AreEqual(0, _count);
        }
Exemplo n.º 13
0
        public void GetCountWhenRegionNameTest()
        {
            const string REGION = "Test";

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

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

            var _count = _cache.GetCount(_key, REGION);
            Assert.AreEqual(1, _count);
        }
Exemplo n.º 14
0
        public void GetCountTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

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

            var _count = _cache.GetCount(KEY);
            Assert.AreEqual(1, _count);
        }
Exemplo n.º 15
0
        public void ContainsWhenRegionNameTest()
        {
            const string REGION = "Test";

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

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

            var _contains = _cache.Contains(_key, _value, REGION);
            Assert.IsTrue(_contains);
        }
Exemplo n.º 16
0
        public void ContainsWhenFalseTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

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

            const string KEY2 = "Test2";
            var _contains = _cache.Contains(KEY2, _value);
            Assert.IsFalse(_contains);
        }
Exemplo n.º 17
0
        public void AddOrUpdateExistingWithObjectTest()
        {
            var _cache = new CollectionCache<object, object>();
            var _key = typeof(object);
            var _value = typeof(object);

            var _addOrGetExisting = _cache.AddOrGetExisting(_key, _value, DateTimeOffset.UtcNow.AddMilliseconds(500));
            Assert.AreEqual(_value, _addOrGetExisting);

            var _value2 = new KeyValuePair<object, object>(_key, new object());
            var _actual = _cache.AddOrUpdateExisting(_key, _value2);

            Assert.AreEqual(_actual, _value2);
        }
Exemplo n.º 18
0
        public void AddOrGetExistingWhenNotExistsTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";

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

            Assert.AreEqual(_value, _actual);
        }
Exemplo n.º 19
0
        public void GetTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

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

            var _actual = _cache.Get(KEY);
            Assert.AreEqual(_value, _actual.FirstOrDefault());
        }
Exemplo n.º 20
0
        public void GetValuesWhenValueNotParsedTest()
        {
            var _cache = new CollectionCache<string, object>();

            const string KEY1 = "Test1";
            var _value1 = new object();

            const string KEY2 = "Test2";
            var _value2 = new object();

            _cache.AddOrGetExisting(KEY1, _value1);
            _cache.AddOrGetExisting(KEY2, _value2);

            var _list = _cache.GetValues(new[] { KEY1 }).ToList();
            Assert.Contains(KEY1, _list.Select(_x => _x.Key).ToList());
            Assert.IsFalse(_list.Select(_x => _x.Key).Contains(KEY2));
        }
Exemplo n.º 21
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.º 22
0
        public void GetWhenEqualsOverrideTest()
        {
            var _cache = new CollectionCache<TestCacheObject, object>();
            var _key = new TestCacheObject { Id = "A" };
            var _value = new object();

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

            var _lookupKey = new TestCacheObject { Id = "A" };
            var _actual = _cache.Get(_lookupKey);
            Assert.AreEqual(_value, _actual.FirstOrDefault());
        }
Exemplo n.º 23
0
        public void AddOrGetExistingTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

            var _addOrGetExisting1 = _cache.AddOrGetExisting(KEY, _value);
            Assert.AreEqual(_value, _addOrGetExisting1);

            var _addOrGetExisting2 = _cache.AddOrGetExisting(KEY, _value);
            Assert.AreEqual(_value, _addOrGetExisting2);
        }
Exemplo n.º 24
0
        public void AddOrGetExistingWhenAbsoluteExpirationTest()
        {
            var _cache = new CollectionCache<string, object>();
            const string KEY = "Test";
            var _value = new object();

            var _addOrGetExisting = _cache.AddOrGetExisting(KEY, _value, DateTimeOffset.UtcNow.AddMilliseconds(20));
            Assert.AreEqual(_value, _addOrGetExisting);

            Thread.Sleep(80);

            var _actual = _cache.Get(KEY);
            Assert.IsEmpty(_actual);
        }