2017-02-28 109 views
0

我們有幾個類似的單元測試都扔間歇OutOfMemoryExceptions,打破我們的CI管道:單元測試間歇OOM異常

public void Evaluate_Node1GreaterThanNode2_ReturnsTrue_Decimal() 
{ 
    //Arrange 
    var generator = _fixture.Create<Generator<decimal>>(); 

    var value1 = _fixture.Create<decimal>(); 
    var value2 = generator.Where(x => x < value1).First(); 
    Node.Node1.Evaluate(Arg.Any<IPeriod>(), Arg.Any<IColleaguePeriodDataManager>()).Returns(value1); 
    Node.Node2.Evaluate(Arg.Any<IPeriod>(), Arg.Any<IColleaguePeriodDataManager>()).Returns(value2); 

    //Act 
    var result = Node.Evaluate(); 

    //Assert 
    Assert.IsTrue(result); 
    Assert.IsTrue(Node.EvaluatedResult); 
} 

正如你可以看到我們使用AutoFixture的生成器來創建數字,大於/小於初始值:

generator.Where(x => x < value1).First(); 

很明顯,這種方法是行不通的。有人有工作嗎?

這裏的錯誤和堆棧跟蹤:

System.OutOfMemoryException : Array dimensions exceeded supported range. 
at System.Collections.Generic.HashSet`1.SetCapacity(Int32 newSize, Boolean forceNewHashCodes) 
    at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value) 
    at Ploeh.AutoFixture.RandomNumericSequenceGenerator.GetNextRandom() 
    at Ploeh.AutoFixture.RandomNumericSequenceGenerator.CreateRandom(Type request) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) 
    at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context, T seed) 
    at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context) 
    at Ploeh.AutoFixture.Generator`1.<GetEnumerator>d__2.MoveNext() 
    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() 
    at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) 

回答

2

它看起來像測試場景需要value2value1更大。一個簡單的方法來做到這一點是這樣的:

var x = fixture.Create<decimal>(); 
var y = fixture.Create<decimal>(); 
var value1 = Math.Min(x, y); 
var value2 = Math.Max(x, y) + 0.0001m; // Add small fraction to make it strictly greater 

您可以重構這一個輔助方法,如果你不想看的臨時變量xy