Exemplo n.º 1
0
        public static void CountOverflow()
        {
            HandleCollector collector = new HandleCollector("CountOverflow", int.MaxValue);

            // We could iterate here 2B times calling Add, but that often takes over 100s
            // Instead, for testing purposes, we reach into the HandleCollector via reflection
            // to make it think it's already been called int.MaxValue - 10 times.  We then
            // only have to call Add 10 times rather than int.MaxValue times, and the test
            // completes very quickly.  If we ever need to run the test on a platform that
            // doesn't support such reflection, we can revert to the super-long running test
            // or find another workaround.

            const int ToAdd = 10; // int.MaxValue
            {
                // Jump HandleCollector instance forward until it almost overflows
                FieldInfo handleCount = typeof(HandleCollector).GetTypeInfo().GetDeclaredField("_handleCount");
                Assert.NotNull(handleCount);
                handleCount.SetValue(collector, int.MaxValue - ToAdd);
            }

            for (int i = 0; i < ToAdd; i++)
            {
                collector.Add();
            }

            Assert.Throws<InvalidOperationException>(() => collector.Add());
        }
Exemplo n.º 2
0
        public static void TestHandleCollector()
        {
            Tuple<int, int, int> intialGcState = new Tuple<int, int, int>(
                GC.CollectionCount(0),
                GC.CollectionCount(1),
                GC.CollectionCount(2));

            HandleCollector lowLimitCollector = new HandleCollector("LowLimit.Collector", LowLimitSize);
            for (int i = 0; i < LowLimitSize + 1; ++i)
            {
                HandleLimitTester hlt = new HandleLimitTester(lowLimitCollector);
            }

            Tuple<int, int, int> postLowLimitState = new Tuple<int, int, int>(
                GC.CollectionCount(0),
                GC.CollectionCount(1),
                GC.CollectionCount(2));

            Assert.True(intialGcState.Item1 + intialGcState.Item2 + intialGcState.Item3 < postLowLimitState.Item1 + postLowLimitState.Item2 + postLowLimitState.Item3, "Low limit handle did not trigger a GC");

            HandleCollector highLimitCollector = new HandleCollector("HighLimit.Collector", HighLimitSize);
            for (int i = 0; i < HighLimitSize + 10; ++i)
            {
                HandleLimitTester hlt = new HandleLimitTester(highLimitCollector);
            }

            Tuple<int, int, int> postHighLimitState = new Tuple<int, int, int>(
                GC.CollectionCount(0),
                GC.CollectionCount(1),
                GC.CollectionCount(2));

            Assert.True(postLowLimitState.Item1 + postLowLimitState.Item2 + postLowLimitState.Item3 < postHighLimitState.Item1 + postHighLimitState.Item2 + postHighLimitState.Item3, "High limit handle did not trigger a GC");
        }
Exemplo n.º 3
0
        public void Ctor_Name_InitialThreshold(string name, int initialThreshold)
        {
            var handleCollector = new HandleCollector(name, initialThreshold);

            Assert.Equal(0, handleCollector.Count);
            Assert.Equal(name ?? string.Empty, handleCollector.Name);
            Assert.Equal(initialThreshold, handleCollector.InitialThreshold);
            Assert.Equal(int.MaxValue, handleCollector.MaximumThreshold);
        }
Exemplo n.º 4
0
        public static void CountOverflow()
        {
            HandleCollector collector = new HandleCollector("CountOverflow", int.MaxValue);
            for (int i = 0; i < Int32.MaxValue; i++)
            {
                collector.Add();
            }

            Assert.Throws<InvalidOperationException>(() => collector.Add());
        }
Exemplo n.º 5
0
        public static void CountOverflow()
        {
            HandleCollector collector = new HandleCollector("CountOverflow", int.MaxValue);

            for (int i = 0; i < Int32.MaxValue; i++)
            {
                collector.Add();
            }

            Assert.Throws <InvalidOperationException>(() => collector.Add());
        }
Exemplo n.º 6
0
        public static void SimplePropertyValidation()
        {
            string name    = "ExampleName";
            int    initial = 10;
            int    max     = 20;

            HandleCollector collector = new HandleCollector(name, initial, max);

            Assert.Equal(0, collector.Count);
            Assert.Equal(name, collector.Name);
            Assert.Equal(initial, collector.InitialThreshold);
            Assert.Equal(max, collector.MaximumThreshold);
        }
Exemplo n.º 7
0
        public static void SimplePropertyValidation()
        {
            string name = "ExampleName";
            int initial = 10;
            int max = 20;

            HandleCollector collector = new HandleCollector(name, initial, max);

            Assert.Equal(0, collector.Count);
            Assert.Equal(name, collector.Name);
            Assert.Equal(initial, collector.InitialThreshold);
            Assert.Equal(max, collector.MaximumThreshold);
        }
Exemplo n.º 8
0
        public void AddRemove_AcrossMultipleGenerations_Success()
        {
            var collector = new HandleCollector("name", 0);

            collector.Add();
            Assert.Equal(1, collector.Count);

            collector.Add();
            Assert.Equal(2, collector.Count);

            collector.Add();
            Assert.Equal(3, collector.Count);

            collector.Remove();
            Assert.Equal(2, collector.Count);

            collector.Remove();
            Assert.Equal(1, collector.Count);

            collector.Remove();
            Assert.Equal(0, collector.Count);
        }
Exemplo n.º 9
0
        public static void TestHandleCollector()
        {
            (int gen0, int gen1, int gen2)initialGcState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
            int initSum = initialGcState.gen0 + initialGcState.gen1 + initialGcState.gen2;

            HandleCollector lowLimitCollector = new HandleCollector("LowLimit.Collector", LowLimitSize);

            for (int i = 0; i < LowLimitSize + 1; ++i)
            {
                HandleLimitTester hlt = new HandleLimitTester(lowLimitCollector);
                Assert.True(lowLimitCollector.Count <= i + 1);
            }

            // HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
            GC.WaitForPendingFinalizers();

            (int gen0, int gen1, int gen2)postLowLimitState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
            int postLowLimitSum = postLowLimitState.gen0 + postLowLimitState.gen1 + postLowLimitState.gen2;

            Assert.True(initSum < postLowLimitSum, $"Low limit handle did not trigger a GC: {initSum} < {postLowLimitSum}");

            HandleCollector highLimitCollector = new HandleCollector("HighLimit.Collector", HighLimitSize);

            for (int i = 0; i < HighLimitSize + 10; ++i)
            {
                HandleLimitTester hlt = new HandleLimitTester(highLimitCollector);
                Assert.True(highLimitCollector.Count <= i + 1);
            }

            // HandleLimitTester does the decrement on the HandleCollector during finalization, so we wait for pending finalizers.
            GC.WaitForPendingFinalizers();

            (int gen0, int gen1, int gen2)postHighLimitState = (GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
            int postHighLimitSum = postHighLimitState.gen0 + postHighLimitState.gen1 + postHighLimitState.gen2;

            Assert.True(postLowLimitSum < postHighLimitSum, $"High limit handle did not trigger a GC: {postLowLimitSum} < {postHighLimitSum}");
        }
Exemplo n.º 10
0
        public static void EmptyRemoval()
        {
            HandleCollector collector = new HandleCollector("EmptyRemoval", 10);

            Assert.Throws <InvalidOperationException>(() => collector.Remove());
        }
Exemplo n.º 11
0
        public static void NullNameCtor()
        {
            HandleCollector collector = new HandleCollector(null, 0, 0);

            Assert.Equal(string.Empty, collector.Name);
        }
Exemplo n.º 12
0
 internal HandleLimitTester(HandleCollector collector)
 {
     _collector = collector;
     _collector.Add();
     GC.KeepAlive(this);
 }
Exemplo n.º 13
0
 public static void EmptyRemoval()
 {
     HandleCollector collector = new HandleCollector("EmptyRemoval", 10);
     Assert.Throws<InvalidOperationException>(() => collector.Remove());
 }
Exemplo n.º 14
0
 public static void NullNameCtor()
 {
     HandleCollector collector = new HandleCollector(null, 0, 0);
     Assert.Equal(string.Empty, collector.Name);
 }
Exemplo n.º 15
0
 internal HandleLimitTester(HandleCollector collector)
 {
     _collector = collector;
     _collector.Add();
     GC.KeepAlive(this);
 }
Exemplo n.º 16
0
 internal HandleLimitTester(HandleCollector collector)
 {
     _collector = collector;
     // Adding an allocation to ensure memory pressure exists so the call to
     // GC.Collect() in the Add() method below will have something to do.
     _pressure = new int[1_000];