Exemplo n.º 1
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.º 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 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.º 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 GetWhenNotExistsTest()
        {
            var _cache = new CollectionCache<object, object>();
            var _key = new object();

            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 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.º 8
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.º 9
0
        public void GetStressTest()
        {
            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 _keyValuePair = new object();
                _cache.AddOrGetExisting(_key, _keyValuePair);
                _dictionary.Add(_key, _keyValuePair);
            }

            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();
                    _cache.Get(_key);
                }

                var _elapsedTime = (DateTimeOffset.UtcNow - _dateTimeOffSet).TotalMilliseconds;
                Assert.LessOrEqual(_elapsedTime, 150);
            }
        }
Exemplo n.º 10
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.º 11
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);
        }