summaryrefslogtreecommitdiff
path: root/tests/src/GC/API/GCHandleCollector/Usage.cs
blob: f346966a6b685f786e0e3586b39401349c788efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/* TEST: Usage
 * DESCRIPTION: Three usage scenarios that monitor the number of live handles and GC Collections
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

// the class that holds the HandleCollectors
public class HandleCollectorTest
{
    private static HandleCollector s_hc = new HandleCollector("hc", 100);

    public HandleCollectorTest()
    {
        s_hc.Add();
    }

    public static int Count
    {
        get { return s_hc.Count; }
    }

    ~HandleCollectorTest()
    {
        s_hc.Remove();
    }

    public static void Reset()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        s_hc = new HandleCollector("hc", 100);
    }
}


public class Usage
{
    private int _numTests = 0;
    private int _numInstances = 100;
    private const int deltaPercent = 10;

    // 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 Collection does not occur when handle count stays below maximum
    private bool Case2()
    {
        _numTests++;
        int handleCount = 0;

        for (int i = 0; i < _numInstances; i++)
        {
            new HandleCollectorTest();
            GC.WaitForPendingFinalizers();
            handleCount = HandleCollectorTest.Count;
            //Note that the GC should occur when handle count is 101 but it will happen at anytime after a creation and we stick to the previous
            //count to avoid error
        }

        Console.WriteLine("{0}, {1}", handleCount, _numInstances);

        if (handleCount == _numInstances)
        {
            Console.WriteLine("Case 2 Passed!");
            return true;
        }

        Console.WriteLine("Case 2 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("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;
    }


    // Checks that the threshold increases are within 0.2 error margine of deltaPercent
    private bool CheckPercentageIncrease(int current, int previous)
    {
        bool retValue = true;
        if (previous != 0)
        {
            double value = ((double)(current - previous)) / (double)previous;
            double expected = (double)deltaPercent / 100;
            double errorMargin = Math.Abs((double)(value - expected) / (double)expected);
            retValue = (errorMargin < 0.2);
        }

        return retValue;
    }


    public bool RunTest()
    {
        int numPassed = 0;

        if (Case1())
        {
            numPassed++;
        }

        HandleCollectorTest.Reset();

        if (Case2())
        {
            numPassed++;
        }

        HandleCollectorTest.Reset();

        if (Case3())
        {
            numPassed++;
        }

        return (numPassed == _numTests);
    }


    public static int Main()
    {
        if (GC.CollectionCount(0) > 20)
        {
            Console.WriteLine("GC Stress is enabled");
            Console.WriteLine("Abort Test");
            return 100;
        }

        Usage u = new Usage();

        if (u.RunTest())
        {
            Console.WriteLine();
            Console.WriteLine("Test Passed!");
            return 100;
        }

        Console.WriteLine();
        Console.WriteLine("Test Failed!");
        return 1;
    }
}