Exemplo n.º 1
0
        public void PolicyManager()
        {
            CachePolicyManager.Reset();

            var dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2") });

            Assert.IsNotNull(dsc.PolicyKey);
            Assert.AreEqual(0, dsc.Count);

            Assert.IsTrue(dsc.ContainsKey(1));
            Assert.IsTrue(dsc.ContainsKey(2));
            Assert.IsFalse(dsc.ContainsKey(3));
            Assert.AreEqual(2, dsc.Count);

            var policy = new DailyCachePolicy();

            CachePolicyManager.Set(dsc.PolicyKey, policy);

            var policy2 = dsc.GetPolicy();

            Assert.IsNotNull(policy2);
            Assert.AreSame(policy, policy2);

            var pa = CachePolicyManager.GetPolicies();

            Assert.AreEqual(1, pa.Length);

            CachePolicyManager.ForceFlush();
            Assert.AreEqual(0, dsc.Count);

            Assert.IsTrue(dsc.ContainsKey(1));
            Assert.IsTrue(dsc.ContainsKey(2));
            Assert.IsFalse(dsc.ContainsKey(3));
            Assert.AreEqual(2, dsc.Count);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Reset (set up) the <see cref="TestSetUp"/> to a known initial state; will result in the <see cref="RegisterSetUp(Func{int, object, bool})">registered</see> set up function being executed.
 /// </summary>
 /// <param name="setUpIfAlreadyDone">Indicates whether to perform the setup if already done; defaults to <c>true</c>.</param>
 /// <param name="data">Optional data to be passed to the resgitered set up function.</param>
 /// <remarks>This also invokes the <see cref="CachePolicyManager.ForceFlush"/> and <see cref="DependencyGroupAttribute.Refresh"/>.</remarks>
 public static void Reset(bool setUpIfAlreadyDone = true, object?data = null)
 {
     lock (_lock)
     {
         if (!_registeredSetUpInvoked || setUpIfAlreadyDone)
         {
             ShouldContinueRunningTests = true;
             CachePolicyManager.ForceFlush();
             DependencyGroupAttribute.Refresh();
             _registeredSetUpData    = data;
             _registeredSetUpInvoked = false;
         }
     }
 }
Exemplo n.º 3
0
        public void CreateAndGetWithForceFlush()
        {
            CachePolicyManager.IsInternalTracingEnabled = true;
            CachePolicyManager.Reset();

            // Initialize the cache.
            var mtc = new MultiTenantCache <KeyValueCache <int, string> >((g, p) =>
            {
                if (g.Equals(ToGuid(1)))
                {
                    return(new KeyValueCache <int, string>(p, (k) => k.ToString()));
                }
                else
                {
                    return(new KeyValueCache <int, string>(p, (k) => "X" + k.ToString()));
                }
            }, "MultiTenantCacheTest");

            var pa = CachePolicyManager.GetPolicies();

            Assert.AreEqual(1, pa.Length);

            // Check the internal nocachepolicy.
            var p0 = pa.Where(x => x.Key.StartsWith("MultiTenantCacheTest_")).SingleOrDefault();

            Assert.IsNotNull(p0);
            Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value);

            // Check that the cache is empty.
            Assert.IsFalse(mtc.Contains(ToGuid(1)));
            Assert.AreEqual(0, mtc.Count);
            mtc.Remove(ToGuid(1));

            // Check the first tenant.
            var kvc1 = mtc.GetCache(ToGuid(1));

            Assert.IsNotNull(kvc1);
            Assert.AreEqual("1", kvc1[1]);
            Assert.AreEqual(1, mtc.Count);

            // Check the default policy for type.
            pa = CachePolicyManager.GetPolicies();
            Assert.AreEqual(2, pa.Length);

            var p1 = pa.Where(x => x.Key == "MultiTenantCacheTest").SingleOrDefault();

            Assert.IsNotNull(p1);
            Assert.IsInstanceOf(typeof(NoExpiryCachePolicy), p1.Value);

            // Check the second tenant.
            var kvc2 = mtc.GetCache(ToGuid(2));

            Assert.IsNotNull(kvc2);
            Assert.AreEqual("X1", kvc2[1]);
            Assert.AreEqual(2, mtc.Count);

            // No new PolicyManager policies should be created.
            pa = CachePolicyManager.GetPolicies();
            Assert.AreEqual(2, pa.Length);

            // Flush the cache - nothing should happen as they never expire.
            CachePolicyManager.Flush();
            Assert.AreEqual(2, mtc.Count);

            // Remove a tenant.
            mtc.Remove(ToGuid(2));
            Assert.IsTrue(mtc.Contains(ToGuid(1)));
            Assert.IsFalse(mtc.Contains(ToGuid(2)));
            Assert.AreEqual(1, mtc.Count);
            Assert.AreEqual(0, kvc2.Count);

            // Force flush the cache - should be removed.
            CachePolicyManager.ForceFlush();
            Assert.IsFalse(mtc.Contains(ToGuid(1)));
            Assert.IsFalse(mtc.Contains(ToGuid(2)));
            Assert.AreEqual(0, mtc.Count);
            Assert.AreEqual(0, kvc1.Count);
        }
Exemplo n.º 4
0
        public void PolicyManager()
        {
            CachePolicyManager.Reset();

            var i   = 0;
            var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }, "KeyValueCacheTest");

            Assert.IsNotNull(mtc.PolicyKey);

            var policy = new DailyCachePolicy();

            CachePolicyManager.Set(mtc.PolicyKey, policy);

            var policy2 = mtc.GetPolicy();

            Assert.IsNotNull(policy2);
            Assert.AreSame(policy, policy2);

            var pa = CachePolicyManager.GetPolicies();

            Assert.AreEqual(2, pa.Length);

            // Check the internal nocachepolicy.
            var p0 = pa.Where(x => x.Key.StartsWith("KeyValueCacheTest_")).SingleOrDefault();

            Assert.IsNotNull(p0);
            Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value);

            // Check the default policy for type.
            var p1 = pa.Where(x => x.Key == "KeyValueCacheTest").SingleOrDefault();

            Assert.IsNotNull(p1);
            Assert.IsInstanceOf(typeof(DailyCachePolicy), p1.Value);

            // Get (add) new item to cache.
            var s = mtc[1];

            Assert.AreEqual("1", s);
            Assert.AreEqual(1, i);

            // No new globally managed policies should have been created.
            pa = CachePolicyManager.GetPolicies();
            Assert.AreEqual(2, pa.Length);

            // Check policy for item is DailyCachePolicy but has its own instance.
            policy2 = mtc.GetPolicyByKey(1);
            Assert.IsNotNull(policy2);
            Assert.IsInstanceOf(typeof(DailyCachePolicy), policy2);
            Assert.AreNotSame(policy, policy2);

            // There should be no policy where item not found.
            Assert.IsNull(mtc.GetPolicyByKey(2));

            // Flush cache where not expired.
            mtc.Flush();
            s = mtc[1];
            Assert.AreEqual("1", s);
            Assert.AreEqual(1, i);

            // Force flush; should reload cache after.
            CachePolicyManager.ForceFlush();
            s = mtc[1];
            Assert.AreEqual("1", s);
            Assert.AreEqual(2, i);
        }