Exemplo n.º 1
0
        public void RemoveTest(bool useCustomCreateCacheCallback)
        {
            long result;
            var  cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            // Populate the cache
            PopulateCache(cache);

            // Remove an entry from tenant 1
            SetTenant(Tenant1Id);
            cache.Remove(_tenant1Values[0].Item1);

            // Remove two entries from tenant 2
            SetTenant(Tenant2Id);
            cache.Remove(_tenant2Values[0].Item1);
            cache.Remove(_tenant2Values[1].Item1);

            // Verify entries have been removed
            SetTenant(Tenant1Id);
            Assert.AreEqual(_tenant1Values.Count - 1, cache.Count);
            Assert.IsFalse(cache.TryGetValue(_tenant1Values[0].Item1, out result));

            // Verify entries have been removed
            SetTenant(Tenant2Id);
            Assert.AreEqual(_tenant2Values.Count - 2, cache.Count);
            Assert.IsFalse(cache.TryGetValue(_tenant2Values[0].Item1, out result));
            Assert.IsFalse(cache.TryGetValue(_tenant2Values[1].Item1, out result));
        }
Exemplo n.º 2
0
        public void IndexerTest(bool useCustomCreateCacheCallback)
        {
            var cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            // Populate the cache
            PopulateCacheViaIndexer(cache);

            // Verify that the entries are valid for tenant 1
            SetTenant(Tenant1Id);
            Assert.AreEqual(_tenant1Values.Count, cache.Count);

            foreach (var v in _tenant1Values)
            {
                Assert.AreEqual(v.Item2, cache[v.Item1]);
            }

            foreach (var v in _tenant2Values)
            {
                Assert.AreEqual(0, cache[v.Item1]);
            }

            // Verify that the entries are valid for tenant 2
            SetTenant(Tenant2Id);
            Assert.AreEqual(_tenant2Values.Count, cache.Count);

            foreach (var v in _tenant2Values)
            {
                Assert.AreEqual(v.Item2, cache[v.Item1]);
            }

            foreach (var v in _tenant1Values)
            {
                Assert.AreEqual(0, cache[v.Item1]);
            }
        }
Exemplo n.º 3
0
        public void ClearTest(bool useCustomCreateCacheCallback)
        {
            var innerCache = CreatePerTenantCache();

            var cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(() => innerCache) : null);

            // Populate the cache
            PopulateCache(cache);

            // Clear the cache for tenant 1
            SetTenant(Tenant1Id);
            cache.Clear();

            // Verify that the cache is empty for tenant 1
            Assert.AreEqual(0, cache.Count);

            // Assert that the inner cache was explicitly cleared (because Redis needs this)
            if (useCustomCreateCacheCallback)
            {
                Assert.AreEqual(0, innerCache.Count);
            }

            // Verify that the cache is not empty for tenant 2
            SetTenant(Tenant2Id);
            Assert.AreEqual(_tenant2Values.Count, cache.Count);
        }
Exemplo n.º 4
0
        public void AddTest(bool useCustomCreateCacheCallback)
        {
            long result;
            var  cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            // Populate the cache
            PopulateCache(cache);

            // Verify that the entries are valid for tenant 1
            SetTenant(Tenant1Id);
            Assert.AreEqual(_tenant1Values.Count, cache.Count);

            foreach (var v in _tenant1Values)
            {
                Assert.IsTrue(cache.TryGetValue(v.Item1, out result));
                Assert.AreEqual(v.Item2, result);
            }

            // Verify that the entries are valid for tenant 2
            SetTenant(Tenant2Id);
            Assert.AreEqual(_tenant2Values.Count, cache.Count);

            foreach (var v in _tenant2Values)
            {
                Assert.IsTrue(cache.TryGetValue(v.Item1, out result));
                Assert.AreEqual(v.Item2, result);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Populates the cache.
        /// </summary>
        /// <param name="cache">The cache.</param>
        private void PopulateCacheViaIndexer(PerTenantNonSharingCache <long, long> cache)
        {
            // Populate the cache for tenant 1
            SetTenant(Tenant1Id);
            foreach (var v in _tenant1Values)
            {
                cache[v.Item1] = v.Item2;
            }

            // Populate the cache for tenant 2
            SetTenant(Tenant2Id);
            foreach (var v in _tenant2Values)
            {
                cache[v.Item1] = v.Item2;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Populates the cache.
        /// </summary>
        /// <param name="cache">The cache.</param>
        private void PopulateCache(PerTenantNonSharingCache <long, long> cache)
        {
            // Populate the cache for tenant 1
            SetTenant(Tenant1Id);
            foreach (var v in _tenant1Values)
            {
                cache.Add(v.Item1, v.Item2);
            }

            // Populate the cache for tenant 2
            SetTenant(Tenant2Id);
            foreach (var v in _tenant2Values)
            {
                cache.Add(v.Item1, v.Item2);
            }
        }
Exemplo n.º 7
0
        public void ClearAllTest(bool useCustomCreateCacheCallback)
        {
            var cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            // Populate the cache
            PopulateCache(cache);

            cache.ClearAll();

            SetTenant(Tenant1Id);
            // Verify that the cache is empty for tenant 1
            Assert.AreEqual(0, cache.Count);

            // Verify that the cache is empty for tenant 2
            SetTenant(Tenant2Id);
            Assert.AreEqual(0, cache.Count);
        }
Exemplo n.º 8
0
        public void GetEnumeratorIEnumerableTest(bool useCustomCreateCacheCallback)
        {
            int count;
            var cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            // Populate the cache
            PopulateCache(cache);

            // Verify that the entries are valid for tenant 1
            SetTenant(Tenant1Id);
            Assert.AreEqual(_tenant1Values.Count, cache.Count);

            IEnumerator ten1Enumerator = ((IEnumerable)cache).GetEnumerator();

            count = 0;
            while (ten1Enumerator.MoveNext())
            {
                KeyValuePair <long, long> item = (KeyValuePair <long, long>)ten1Enumerator.Current;

                Assert.AreEqual(1, _tenant1Values.Count(t => t.Item1 == item.Key && t.Item2 == item.Value));
                count++;
            }
            Assert.AreEqual(_tenant1Values.Count, count);

            // Verify that the entries are valid for tenant 2
            SetTenant(Tenant2Id);
            Assert.AreEqual(_tenant2Values.Count, cache.Count);

            IEnumerator ten2Enumerator = ((IEnumerable)cache).GetEnumerator();

            count = 0;
            while (ten2Enumerator.MoveNext())
            {
                KeyValuePair <long, long> item = (KeyValuePair <long, long>)ten2Enumerator.Current;

                Assert.AreEqual(1, _tenant2Values.Count(t => t.Item1 == item.Key && t.Item2 == item.Value));
                count++;
            }
            Assert.AreEqual(_tenant2Values.Count, count);
        }
Exemplo n.º 9
0
        public void TestTryGetOrAdd(bool useCustomCreateCacheCallback)
        {
            var cache = new PerTenantNonSharingCache <long, long>("Test Cache", useCustomCreateCacheCallback ? new Func <ICache <long, long> >(CreatePerTenantCache) : null);

            long [] ids = new long [] { Tenant1Id, Tenant2Id };

            foreach (long tenantId in ids)
            {
                long expectedValue = tenantId * 13;

                SetTenant(tenantId);

                /////
                // Add a single element.
                /////
                long value;
                bool result = cache.TryGetOrAdd(1, out value, key =>
                {
                    Assert.That(key, Is.EqualTo(1));
                    return(expectedValue);
                });

                Assert.That(result, Is.False);
                Assert.That(value, Is.EqualTo(expectedValue));

                /////
                // Request the same again
                /////
                result = cache.TryGetOrAdd(1, out value, key =>
                {
                    throw new InvalidOperationException("The factory should not be called here");
                });

                Assert.That(result, Is.True);
                Assert.That(value, Is.EqualTo(expectedValue));
            }
        }