简体   繁体   English

如何测试可能的 memory 泄漏在测试 object (DUnitX, Spring4D 1.2.2) 破坏后引起的引用

[英]How to test possible memory leaks caused by references after the destruction of the tested object (DUnitX, Spring4D 1.2.2)

TMyClass contains two references. TMyClass包含两个引用。 A reference to an IList<integer> and a reference to IMyInterface .IList<integer>的引用和对IMyInterface的引用。 The mocking of IList<integer> is not necessary. IList<integer>的 mocking 不是必需的。 The framework probably well tested, well predictable behavior, so I could see it as a Data object.该框架可能经过良好测试,行为可预测,因此我可以将其视为数据 object。 But IMyInterface is an untested service, so I should mock it in a unit test.但是IMyInterface是一个未经测试的服务,所以我应该在单元测试中模拟它。 I want to check for memory leaks so I want to test the modifications of the RefCount-s of the references after the subject destroyed.我想检查 memory 泄漏,所以我想在主题销毁后测试引用的 RefCount-s 的修改。 The 'RefCount' of the IList<integer> changes in the right way. IList<integer>的“RefCount”以正确的方式变化。 But I can't say the same for the mocked IMyInterface (in my solution).但是对于模拟的IMyInterface (在我的解决方案中),我不能说同样的话。 How could I test that the references does not cause memory leaks?我如何测试参考不会导致 memory 泄漏? Or a test like this is an integration test and should I test it always with real instances?或者像这样的测试是一个集成测试,我应该总是用真实的实例来测试它吗?

unit Unit1;


interface

uses
    DUnitX.TestFramework
  , Spring.Collections
  , Spring.Mocking
  ;

type
IMyInterface = interface ( IInvokable )
  ['{76B13784-6CCF-4A87-882C-E624F003B082}']
  procedure foo;
end;

TMyClass = class
  private
    fList : IList<integer>;
    fMyInterface : IMyInterface;

  public
    constructor Create( list_ : IList<integer>; myInterface_ : IMyInterface );

end;

[TestFixture]
TMyClassTest = class
  protected
    function getInterfaceRefCount( const interface_ : IInterface ) : integer;

  public
    [Test]
    procedure listRefCountTest;
    [Test]
    procedure myInterfaceRefCountTest;

end;

implementation

uses
  System.SysUtils
  ;

constructor TMyClass.Create( list_ : IList<integer>; myInterface_ : IMyInterface );
begin
  inherited Create;
  fList := list_;
  fMyInterface := myInterface_;
end;

function TMyClassTest.getInterfaceRefCount( const interface_ : IInterface ) : integer;
begin
  result := ( interface_ as TInterfacedObject ).RefCount;
end;

procedure TMyClassTest.listRefCountTest;
var
  list : IList<integer>;
  myInterfaceMock : Mock<IMyInterface>;
  myClass : TMyClass;
  listRefCount : integer;
begin
  list := TCollections.CreateList<integer>;
  myClass := TMyClass.Create( list, myInterfaceMock );
  try
    listRefCount := getInterfaceRefCount( list );
  finally
    FreeAndNIL( myClass );
  end;
  Assert.AreEqual( listRefCount-1, getInterfaceRefCount( list ) );
end;

procedure TMyClassTest.myInterfaceRefCountTest;
var
  list : IList<integer>;
  myInterfaceMock : Mock<IMyInterface>;
  myClass : TMyClass;
  myInterfaceRefCount : integer;
begin
  list := TCollections.CreateList<integer>;
  myClass := TMyClass.Create( list, myInterfaceMock );
  try
    myInterfaceRefCount := getInterfaceRefCount( myInterfaceMock.Instance );
  finally
    FreeAndNIL( myClass );
  end;
  Assert.AreEqual( myInterfaceRefCount-1, getInterfaceRefCount( myInterfaceMock.Instance ) );
end;

initialization
  TDUnitX.RegisterTestFixture(TMyClassTest);

end.

Memoryleak checking does not need to be done explicitly - I suggest using https://github.com/shadow-cs/delphi-leakcheck for that - it can seemlessly integrated with either DUnit or DUnitX and automatically provides you with all the information you need when a leak occurs (opposed to only telling you "there was a leak of x bytes" which out of the box DUnit does by simply comparing allocated bytes before and after running the test)内存泄漏检查不需要明确进行 - 我建议使用https://github.com/shadow-cs/delphi-leakcheck - 它可以与 DUnit 或 DUnitX 无缝集成,并自动为您提供所需的所有信息当发生泄漏时(与仅告诉您“存在 x 字节泄漏”相反,开箱即用的 DUnit 通过简单地比较运行测试之前和之后分配的字节来实现)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM