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 TypeInfo type = typeof(HandleCollector).GetTypeInfo(); FieldInfo handleCount = type.GetDeclaredField("_handleCount") ?? // corefx type.GetDeclaredField("handleCount"); // desktop Assert.NotNull(handleCount); handleCount.SetValue(collector, int.MaxValue - ToAdd); } for (int i = 0; i < ToAdd; i++) { collector.Add(); } Assert.Throws <InvalidOperationException>(() => collector.Add()); }
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()); }
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()); }
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()); }
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); }
internal HandleLimitTester(HandleCollector collector) { _collector = collector; _collector.Add(); GC.KeepAlive(this); }