summaryrefslogtreecommitdiff
path: root/src/msvc6/testrunner/ActiveTest.cpp
blob: 67da35d18bc80f84c1e97fee198f040589b02b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "stdafx.h"
#include "ActiveTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



// Construct the active test
ActiveTest::ActiveTest( CPPUNIT_NS::Test *test )
    : TestDecorator( test )
    , m_runCompleted() 
{ 
  m_currentTestResult = NULL; 
  m_threadHandle = INVALID_HANDLE_VALUE; 
}


// Pend until the test has completed
ActiveTest::~ActiveTest()
{ 
  CSingleLock( &m_runCompleted, TRUE );
  m_test = NULL;
}


// Set the test result that we are to run
void 
ActiveTest::setTestResult( CPPUNIT_NS::TestResult *result )
{ 
  m_currentTestResult = result; 
}


// Run our test result
void 
ActiveTest::run()
{ 
  TestDecorator::run( m_currentTestResult );
}


// Spawn a thread to a test
void 
ActiveTest::run( CPPUNIT_NS::TestResult *result )
{
  CWinThread *thread;
  
  setTestResult( result );
  m_runCompleted.ResetEvent();

  thread = ::AfxBeginThread( threadFunction, 
                             this, 
                             THREAD_PRIORITY_NORMAL, 
                             0, 
                             CREATE_SUSPENDED);
  
  ::DuplicateHandle( GetCurrentProcess(), 
                     thread->m_hThread,
                     GetCurrentProcess(), 
                     &m_threadHandle, 
                     0, 
                     FALSE, 
                     DUPLICATE_SAME_ACCESS );

  thread->ResumeThread ();
}


// Simple execution thread.  Assuming that an ActiveTest instance
// only creates one of these at a time.
UINT 
ActiveTest::threadFunction( LPVOID thisInstance )
{
  ActiveTest *test = (ActiveTest *)thisInstance;

  test->run ();

  ::CloseHandle( test->m_threadHandle );
  test->m_threadHandle = INVALID_HANDLE_VALUE;

  test->m_runCompleted.SetEvent();

  return 0;
}