public bool RunTest() { int numPassed = 0; if (Case1()) { numPassed++; } HandleCollectorTest.Reset(); if (Case2()) { numPassed++; } HandleCollectorTest.Reset(); if (Case3()) { numPassed++; } return(numPassed == _numTests); }
// ensures GC Collections frequency decrease by threshold private bool Case3() { _numTests++; int gcCount = GC.CollectionCount(2); int handleCount = HandleCollectorTest.Count; int prevHandleCount = HandleCollectorTest.Count; List <HandleCollectorTest> list = new List <HandleCollectorTest>(); for (int i = 0; i < deltaPercent; i++) { do { HandleCollectorTest h = new HandleCollectorTest(); if ((HandleCollectorTest.Count % 2) == 0) { list.Add(h); } GC.WaitForPendingFinalizers(); if (GC.CollectionCount(2) != gcCount) { gcCount = GC.CollectionCount(2); break; } else { handleCount = HandleCollectorTest.Count; } } while (true); // ensure threshold is increasing if (!CheckPercentageIncrease(handleCount, prevHandleCount)) { Console.WriteLine("Percentage not increasing, performing Collect/WFPF/Collect cycle"); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); if (handleCount == HandleCollectorTest.Count) { Console.WriteLine("No handles finalized in Collect/WFPF/Collect cycle"); return(false); } } prevHandleCount = handleCount; } Console.WriteLine("Case 3 Passed!"); return(true); }
// ensures GC Collections frequency decrease by threshold private bool Case3() { _numTests++; int gcCount = GC.CollectionCount(2); int handleCount = HandleCollectorTest.Count; int prevHandleCount = HandleCollectorTest.Count; List <HandleCollectorTest> list = new List <HandleCollectorTest>(); for (int i = 0; i < deltaPercent; i++) { do { HandleCollectorTest h = new HandleCollectorTest(); if ((HandleCollectorTest.Count % 2) == 0) { list.Add(h); } GC.WaitForPendingFinalizers(); if (GC.CollectionCount(2) != gcCount) { gcCount = GC.CollectionCount(2); break; } else { handleCount = HandleCollectorTest.Count; } } while (true); // ensure threshold is increasing if (!CheckPercentageIncrease(handleCount, prevHandleCount)) { // see github#4093 for the rationale for fail-fast in this test. Environment.FailFast(string.Empty); return(false); } prevHandleCount = handleCount; } Console.WriteLine("Case 3 Passed!"); return(true); }
// ensures GC Collections occur when handle count exceeds maximum private bool Case1() { _numTests++; // clear GC GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); int original = GC.CollectionCount(0); HandleCollectorTest h; // create objects and let them go out of scope for (int i = 0; i < _numInstances; i++) { h = new HandleCollectorTest(); } h = null; GC.WaitForPendingFinalizers(); // Collection should not have occurred if (GC.CollectionCount(0) != original) { Console.WriteLine("Early collection!"); Console.WriteLine("Case 1 Failed!"); return(false); } new HandleCollectorTest(); if ((GC.CollectionCount(0) - original) > 0) { Console.WriteLine("Case 1 Passed!"); return(true); } Console.WriteLine("Expected collection did not occur!"); Console.WriteLine("Case 1 Failed!"); return(false); }
// ensures GC Collections occur when handle count exceeds maximum private bool Case1() { _numTests++; // clear GC GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); HandleCollectorTest h; int original = GC.CollectionCount(0); // create objects and let them go out of scope for (int i = 0; i < _numInstances; i++) h = new HandleCollectorTest(); h = null; GC.WaitForPendingFinalizers(); // Collection should not have occurred if (GC.CollectionCount(0) != original) { Console.WriteLine("Early collection!"); Console.WriteLine("Case 1 Failed!"); return false; } new HandleCollectorTest(); if ((GC.CollectionCount(0) - original) > 0) { Console.WriteLine("Case 1 Passed!"); return true; } Console.WriteLine("Expected collection did not occur!"); Console.WriteLine("Case 1 Failed!"); return false; }
// ensures GC Collections frequency decrease by threshold private bool Case3() { _numTests++; int gcCount = GC.CollectionCount(2); int handleCount = HandleCollectorTest.Count; int prevHandleCount = HandleCollectorTest.Count; List<HandleCollectorTest> list = new List<HandleCollectorTest>(); for (int i = 0; i < deltaPercent; i++) { do { HandleCollectorTest h = new HandleCollectorTest(); if ((HandleCollectorTest.Count % 2) == 0) list.Add(h); GC.WaitForPendingFinalizers(); if (GC.CollectionCount(2) != gcCount) { gcCount = GC.CollectionCount(2); break; } else handleCount = HandleCollectorTest.Count; } while (true); // ensure threshold is increasing if (!CheckPercentageIncrease(handleCount, prevHandleCount)) { Console.WriteLine("Case 3 failed: threshold not increasing!"); return false; } prevHandleCount = handleCount; } Console.WriteLine("Case 3 Passed!"); return true; }