当FreeOnTerminate = True时,为TThread后代编写Delphi DUnit测试的最佳方式是什么? TThread后代返回一个需要测试的引用,但是我无法弄清楚如何等待线程在测试中完成…
unit uThreadTests; interface uses Classes,TestFramework; type TMyThread = class(TThread) strict private FId: Integer; protected procedure Execute; override; public constructor Create(AId: Integer); property Id: Integer read FId; end; TestTMyThread = class(TTestCase) strict private FMyId: Integer; procedure OnThreadTerminate(Sender: TObject); protected procedure SetUp; override; procedure TearDown; override; published procedure TestMyThread; end; implementation { TMyThread } constructor TMyThread.Create(AId: Integer); begin FreeOnTerminate := True; FId := AId; inherited Create(False); end; procedure TMyThread.Execute; begin inherited; FId := FId + 1; end; { TestTMyThread } procedure TestTMyThread.TestMyThread; //var // LThread: TMyThread; begin // LThread := TMyThread.Create(1); // LThread.OnTerminate := OnThreadTerminate; // LThread.WaitFor; // CheckEquals(2,FMyId); // LThread.Free; ///// The above commented out code is only useful of FreeOnTerminate = False; with TMyThread.Create(1) do begin OnTerminate := OnThreadTerminate; WaitFor; /// Not sure how else to wait for the thread to finish? end; CheckEquals(2,FMyId); end; procedure TestTMyThread.OnThreadTerminate(Sender: TObject); begin FMyId := (Sender as TMyThread).Id; end; /// When FreeOnTerminate = True - THIS LINE CAUSES ERROR: Thread Error the handle is invalid procedure TestTMyThread.SetUp; begin inherited; end; procedure TestTMyThread.TearDown; begin inherited; end; initialization RegisterTests([TestTMyThread.Suite]); end.
任何想法都会受到欢迎.
德尔福2010.
解决方法
将线程子类化以使其更可测. TThread和TObject提供了足够的钩子,您可以添加感应变量来观察它到达您想要的状态的某些点.
我看到你可能希望测试的这个特定类的三个方面:
>它根据发送给构造函数的值计算其Id属性的值.
>它计算新线程中的新Id属性,而不是调用构造函数的线程.
>完成后自动释放.
所有这些东西都可以从一个子类测试,但是很难测试,而不需要更改线程的接口. (所有其他答案到目前为止需要更改线程的界面,例如通过添加更多的构造函数参数或通过更改自身的开始方式,这可以使线程在实际程序中使用更加困难或至少更麻烦)
type PTestData = ^TTestData; TTestData = record Event: TEvent; OriginalId: Integer; FinalId: Integer; end; TTestableMyThread = class(TMyThread) private FData: PTestData; public constructor Create(AId: Integer; AData: PTestData); destructor Destroy; override; procedure AfterConstruction; override; end; constructor TTestableMyThread.Create(AId: Integer; const AData: PTestData); begin inherited Create(AId); FData := AData; end; destructor TestableMyThread.Destroy; begin inherited; FData.FinalId := Id; // Tell the test that the thread has been freed FData.Event.SetEvent; end; procedure TTestableMyThread.AfterConstruction; begin FData.OriginalId := Id; inherited; // Call this last because this is where the thread starts running end;
使用该子类,可以编写一个检查以前识别的三个质量的测试:
procedure TestTMyThread.TestMyThread; var Data: TTestData; WaitResult: TWaitResult; begin Data.OriginalId := -1; Data.FinalId := -1; Data.Event := TSimpleEvent.Create; try TTestableMyThread.Create(1,@Data); // We don't free the thread,and the event is only set in the destructor,// so if the event is signaled,it means the thread freed itself: That // aspect of the test implicitly passes. We don't want to wait forever,// though,so we fail the test if we have to wait too long. Either the // Execute method is taking too long to do its computations,or the thread // isn't freeing itself. // Adjust the timeout based on expected performance of Execute. WaitResult := Data.Event.WaitFor(5000); case WaitResult of wrSignaled: ; // This is the expected result wrTimeOut: Fail('Timed out waiting for thread'); wrAbandoned: Fail('Event was abandoned'); wrError: RaiseLastOSError(Data.Event.LastError); else Fail('Unanticipated error waiting for thread'); end; CheckNotEquals(2,Data.OriginalId,'Didn''t wait till Execute to calculate Id'); CheckEquals(2,Data.FinalId,'Calculated wrong Id value'); finally Data.Event.Free; end; end;