Update of /cvsroot/cppunit/cppunit2/test/shmem_test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4621/test/shmem_test
Added Files:
SConscript main.cpp
Log Message:
* added end to end test for shared memory transport.
--- NEW FILE: main.cpp ---
#include <opentest/sharedmemorytransport.h>
#ifndef OPENTEST_NO_SHAREDMEMORYTRANSPORT
# define WIN32_LEAN_AND_MEAN
# define NOGDI
# define NOUSER
# define NOKERNEL
# define NOSOUND
# define NOMINMAX
# define BLENDFUNCTION void // for mingw & gcc
# include <windows.h>
static const char *logPrefix = "";
static void log( const char *format, ... )
{
va_list args;
va_start( args, format );
printf( "%s", logPrefix );
vprintf( format, args );
printf( "\n" );
va_end( args );
}
class MessageTransmitionTester : private OpenTest::RemoteMessageServer
{
public:
MessageTransmitionTester( OpenTest::MessageTransport &transport, const char *id )
: received_( 0 )
, sent_( 0 )
, id_( id)
{
const int maxCount = 3;
while ( sent_ < maxCount || received_ < maxCount )
{
if ( sent_ < maxCount )
{
++sent_;
log("sent");
OpenTest::RemoteMessagePtr message( new OpenTest::RemoteMessage( OpenTest::runnerMessageStopTest ) );
transport.sendMessage( message );
}
if ( received_ < maxCount )
{
transport.dispatchReceivedMessages( *this );
}
}
}
public: // overridden from RemoteMessageServer
void dispatchMessage( const OpenTest::RemoteMessagePtr &message )
{
++received_;
log("received");
}
private:
void log( const char *event ) const
{
printf( "[%s] => message %s (e:%d, r:%d)\n", id_, event, sent_, received_ );
}
int received_;
int sent_;
const char *id_;
};
static int runAsSlave( int argc, const char *argv[] )
{
DebugBreak();
logPrefix = "{slave} ";
OpenTest::SharedMemoryConfig config;
config.log_ = log;
OpenTest::SharedMemoryTransport transport( argv[2], config );
MessageTransmitionTester dummy( transport, "slave" );
return 0;
}
int main( int argc, const char *argv[] )
{
printf( "Command argv[0]: %s\n", argv[0] );
if ( argc > 1 )
printf( "Command argv[1]: %s\n", argv[1] );
printf( "Command line:%s\n", GetCommandLine() );
for ( int index =1; index < argc; ++index )
if ( std::string("--slave") == argv[index] )
return runAsSlave( argc, argv );
logPrefix = "{master} ";
OpenTest::SharedMemoryConfig config;
config.log_ = log;
OpenTest::SharedMemoryTransport transport( config );
std::string childCommandLine( GetCommandLine() );
childCommandLine += " --slave ";
childCommandLine += transport.transportName();
// Create a child process and pass the anymous shared memory file handle
// in StdIn.
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE; // inherit handle
saAttr.lpSecurityDescriptor = NULL;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
(LPTSTR)childCommandLine.c_str(), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed." );
return 1;
}
MessageTransmitionTester dummy( transport, "master" );
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
#else
int main( int argc, const char *argv[] )
{
return 0; // shared memory transport not supported
}
#endif
--- NEW FILE: SConscript ---
Import( 'env_testing buildCppUnitExample' )
buildCppUnitExample( env_testing, Split( """
main.cpp
""" ),
'w32shmem_test' )
|