[System.Runtime.Caching] Make some MemoryCache tests more reliable
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 4 Mar 2015 21:40:02 +0000 (13:40 -0800)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 4 Mar 2015 21:46:04 +0000 (13:46 -0800)
After MemoryCache was imported from referencesources, some tests started
failing occasionally. Cross-testing on MS.NET showed that you need to
access the cached item to make MemoryCache.GetCount() reliably return the
correct number after an item was expired from the cache.

Also removed __MonoTimerPeriod and __MonoEmulateOneCPU configuration variables,
they were used in Mono code before and don't make sense with the new code.

mcs/class/System.Runtime.Caching/Test/System.Runtime.Caching/MemoryCacheTest.cs

index 34ac03c5050d4be0c182a20f32bc10605b06da9e..8d9023b9ca5f7a0acf5890b5ddd623e76220006e 100644 (file)
@@ -833,17 +833,9 @@ namespace MonoTests.System.Runtime.Caching
                {
                        bool expired = false;
                        CacheEntryRemovedReason reason = CacheEntryRemovedReason.CacheSpecificEviction;
-                       NameValueCollection config;
-                       int sleepPeriod;
-#if !DOTNET
-                       config = new NameValueCollection ();
-                       config.Add ("__MonoTimerPeriod", "1");
-                       sleepPeriod = 1100;
-#else
-                       config = null;
-                       sleepPeriod = 20100; // 20s is the .NET period - discovered by experimentation
-#endif
-                       var mc = new PokerMemoryCache ("MyCache", config);
+                       int sleepPeriod = 1100;
+
+                       var mc = new PokerMemoryCache ("MyCache");
                        var cip = new CacheItemPolicy ();
 
                        cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
@@ -871,6 +863,7 @@ namespace MonoTests.System.Runtime.Caching
                        mc.Set ("key", "value", cip);
                        Thread.Sleep (sleepPeriod);
 
+                       Assert.IsNull (mc.Get ("key"), "#A3-0");
                        Assert.IsTrue (expired, "#A3-1");
                        Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#A3-2");
 
@@ -903,6 +896,10 @@ namespace MonoTests.System.Runtime.Caching
                        mc.Set ("key4", "value4", cip);
                        
                        Thread.Sleep (sleepPeriod);
+                       Assert.IsNull (mc.Get ("key1"), "#A4-1");
+                       Assert.IsNull (mc.Get ("key2"), "#A4-2");
+                       Assert.IsNull (mc.Get ("key3"), "#A4-3");
+                       Assert.IsNotNull (mc.Get ("key4"), "#A4-4");
                        Assert.AreEqual (3, expiredCount, "#A4");
                }
 
@@ -1235,11 +1232,7 @@ namespace MonoTests.System.Runtime.Caching
                        var config = new NameValueCollection ();
                        config["cacheMemoryLimitMegabytes"] = 0.ToString ();
                        config["physicalMemoryLimitPercentage"] = 100.ToString ();
-                       config["__MonoEmulateOneCPU"] = true.ToString ();
-
-                       // it appears that pollingInterval does nothing, so we set the Mono timer as well
                        config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();
-                       config["__MonoTimerPeriod"] = 1.ToString ();
                        
                        using (var mc = new MemoryCache ("TestCacheShrink",  config)) { 
                                Assert.AreEqual (0, mc.GetCount (), "#CS1");
@@ -1263,6 +1256,9 @@ namespace MonoTests.System.Runtime.Caching
                                // wait for the cache thread to expire the short duration items, this will also shrink the size of the cache
                                global::System.Threading.Thread.Sleep (5 * 1000);
                                
+                               for (int i = 0; i < HEAP_RESIZE_SHORT_ENTRIES; i++) {
+                                       Assert.IsNull (mc.Get ("short-" + i), "#CS4-" + i);
+                               }
                                Assert.AreEqual (HEAP_RESIZE_LONG_ENTRIES, mc.GetCount (), "#CS4");     
                                
                                // add some new items into the cache, this will grow the cache again
@@ -1280,11 +1276,7 @@ namespace MonoTests.System.Runtime.Caching
                        var config = new NameValueCollection ();
                        config["cacheMemoryLimitMegabytes"] = 0.ToString ();
                        config["physicalMemoryLimitPercentage"] = 100.ToString ();
-                       config["__MonoEmulateOneCPU"] = true.ToString ();
-
-                       // it appears that pollingInterval does nothing, so we set the Mono timer as well
                        config["pollingInterval"] = new TimeSpan (0, 0, 10).ToString ();
-                       config["__MonoTimerPeriod"] = 10.ToString ();
                        
                        using (var mc = new MemoryCache ("TestExpiredGetValues",  config)) {
                                Assert.AreEqual (0, mc.GetCount (), "#EGV1");
@@ -1320,11 +1312,7 @@ namespace MonoTests.System.Runtime.Caching
                        var config = new NameValueCollection ();
                        config["cacheMemoryLimitMegabytes"] = 0.ToString ();
                        config["physicalMemoryLimitPercentage"] = 100.ToString ();
-                       config["__MonoEmulateOneCPU"] = true.ToString ();
-
-                       // it appears that pollingInterval does nothing, so we set the Mono timer as well
                        config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();
-                       config["__MonoTimerPeriod"] = 1.ToString ();
 
                        using (var mc = new MemoryCache ("TestCacheExpiryOrdering",  config)) {
                                Assert.AreEqual (0, mc.GetCount (), "#CEO1");
@@ -1349,6 +1337,9 @@ namespace MonoTests.System.Runtime.Caching
 
                                global::System.Threading.Thread.Sleep (4 * 1000);
 
+                               for (int i = 0; i < 100; i++) {
+                                       Assert.IsNull (mc.Get ("short-" + i), "#CEO4-" + i);
+                               }
                                Assert.AreEqual (100, mc.GetCount (), "#CEO4");
                        }
                }
@@ -1359,11 +1350,7 @@ namespace MonoTests.System.Runtime.Caching
                        var config = new NameValueCollection ();
                        config["cacheMemoryLimitMegabytes"] = 0.ToString ();
                        config["physicalMemoryLimitPercentage"] = 100.ToString ();
-                       config["__MonoEmulateOneCPU"] = true.ToString ();
-
-                       // it appears that pollingInterval does nothing, so we set the Mono timer as well
                        config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();
-                       config["__MonoTimerPeriod"] = 1.ToString ();
 
                        using (var mc = new MemoryCache ("TestCacheSliding",  config)) {
                                Assert.AreEqual (0, mc.GetCount (), "#CSL1");
@@ -1390,10 +1377,16 @@ namespace MonoTests.System.Runtime.Caching
                                        Assert.AreNotEqual (null, item, "#CSL3-" + i);
                                }
 
+                               Assert.IsNull (mc.Get ("expire1"), "#CSL4-1");
+                               Assert.IsNull (mc.Get ("expire2"), "#CSL4-2");
+                               Assert.IsNull (mc.Get ("expire3"), "#CSL4-3");
+                               Assert.IsNull (mc.Get ("expire4"), "#CSL4-4");
+                               Assert.IsNull (mc.Get ("expire5"), "#CSL4-5");
                                Assert.AreEqual (1, mc.GetCount (), "#CSL4");
 
                                global::System.Threading.Thread.Sleep (4 * 1000);
 
+                               Assert.IsNull (mc.Get ("slidingtest"), "#CSL5a");
                                Assert.AreEqual (0, mc.GetCount (), "#CSL5");
                        }
                }