Tests for MQ examples

This commit is contained in:
Alexey Rybalchenko
2017-08-23 11:12:29 +02:00
parent f6ee1cdf57
commit bd8be1436e
57 changed files with 658 additions and 118 deletions

View File

@@ -6,12 +6,11 @@
# copied verbatim in the file "LICENSE" #
################################################################################
configure_file(${CMAKE_SOURCE_DIR}/examples/MQ/1-sampler-sink/ex1-sampler-sink.json
${CMAKE_BINARY_DIR}/bin/config/ex1-sampler-sink.json)
configure_file(${CMAKE_SOURCE_DIR}/examples/MQ/1-sampler-sink/startMQEx1.sh.in
${CMAKE_BINARY_DIR}/bin/examples/MQ/1-sampler-sink/startMQEx1.sh)
configure_file(${CMAKE_SOURCE_DIR}/examples/MQ/1-sampler-sink/ex1-sampler-sink.json ${CMAKE_BINARY_DIR}/bin/config/ex1-sampler-sink.json)
configure_file(${CMAKE_SOURCE_DIR}/examples/MQ/1-sampler-sink/startMQEx1.sh.in ${CMAKE_BINARY_DIR}/bin/examples/MQ/1-sampler-sink/startMQEx1.sh)
configure_file(${CMAKE_SOURCE_DIR}/examples/MQ/1-sampler-sink/testMQEx1.sh.in ${CMAKE_BINARY_DIR}/bin/examples/MQ/1-sampler-sink/testMQEx1.sh)
Set(INCLUDE_DIRECTORIES
set(INCLUDE_DIRECTORIES
${CMAKE_SOURCE_DIR}/fairmq
${CMAKE_SOURCE_DIR}/fairmq/devices
${CMAKE_SOURCE_DIR}/fairmq/tools
@@ -20,40 +19,40 @@ Set(INCLUDE_DIRECTORIES
${CMAKE_CURRENT_BINARY_DIR}
)
Set(SYSTEM_INCLUDE_DIRECTORIES
set(SYSTEM_INCLUDE_DIRECTORIES
${Boost_INCLUDE_DIR}
${ZeroMQ_INCLUDE_DIR}
)
Include_Directories(${INCLUDE_DIRECTORIES})
Include_Directories(SYSTEM ${SYSTEM_INCLUDE_DIRECTORIES})
include_directories(${INCLUDE_DIRECTORIES})
include_directories(SYSTEM ${SYSTEM_INCLUDE_DIRECTORIES})
Set(LINK_DIRECTORIES
set(LINK_DIRECTORIES
${Boost_LIBRARY_DIRS}
)
Link_Directories(${LINK_DIRECTORIES})
link_directories(${LINK_DIRECTORIES})
Set(SRCS
set(SRCS
"FairMQExample1Sampler.cxx"
"FairMQExample1Sink.cxx"
)
Set(DEPENDENCIES
set(DEPENDENCIES
${DEPENDENCIES}
FairMQ
)
Set(LIBRARY_NAME FairMQExample1)
set(LIBRARY_NAME FairMQExample1)
GENERATE_LIBRARY()
Set(Exe_Names
set(Exe_Names
ex1-sampler
ex1-sink
)
Set(Exe_Source
set(Exe_Source
runExample1Sampler.cxx
runExample1Sink.cxx
)
@@ -64,16 +63,21 @@ math(EXPR _length ${_length}-1)
set(BIN_DESTINATION share/fairbase/examples/MQ/1-sampler-sink/bin)
set(EXECUTABLE_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/examples/MQ/1-sampler-sink")
ForEach(_file RANGE 0 ${_length})
foreach(_file RANGE 0 ${_length})
list(GET Exe_Names ${_file} _name)
list(GET Exe_Source ${_file} _src)
set(EXE_NAME ${_name})
set(SRCS ${_src})
set(DEPENDENCIES FairMQExample1)
GENERATE_EXECUTABLE()
EndForEach(_file RANGE 0 ${_length})
endforeach(_file RANGE 0 ${_length})
Install(
add_test(NAME MQ.ex1-sampler-sink COMMAND ${CMAKE_BINARY_DIR}/bin/examples/MQ/1-sampler-sink/testMQEx1.sh)
set_tests_properties(MQ.ex1-sampler-sink PROPERTIES TIMEOUT "30")
set_tests_properties(MQ.ex1-sampler-sink PROPERTIES RUN_SERIAL true)
set_tests_properties(MQ.ex1-sampler-sink PROPERTIES PASS_REGULAR_EXPRESSION " Received: \"Hello\"")
install(
FILES ex1-sampler-sink.json
DESTINATION share/fairbase/examples/MQ/1-sampler-sink/config/
)

View File

@@ -23,19 +23,20 @@ using namespace std;
FairMQExample1Sampler::FairMQExample1Sampler()
: fText()
, fMaxIterations(0)
, fNumIterations(0)
{
}
void FairMQExample1Sampler::InitTask()
{
// Get the fText value from the command line option (via fConfig)
// Get the fText and fMaxIterations values from the command line options (via fConfig)
fText = fConfig->GetValue<string>("text");
fMaxIterations = fConfig->GetValue<uint64_t>("max-iterations");
}
bool FairMQExample1Sampler::ConditionalRun()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
// create a copy of the data with new(), that will be deleted after the transfer is complete
string* text = new string(fText);
@@ -56,6 +57,13 @@ bool FairMQExample1Sampler::ConditionalRun()
{
return false;
}
else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
{
LOG(INFO) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
this_thread::sleep_for(chrono::seconds(1));
return true;
}

View File

@@ -27,6 +27,8 @@ class FairMQExample1Sampler : public FairMQDevice
protected:
std::string fText;
uint64_t fMaxIterations;
uint64_t fNumIterations;
virtual void InitTask();
virtual bool ConditionalRun();

View File

@@ -14,21 +14,36 @@
#include "FairMQExample1Sink.h"
#include "FairMQLogger.h"
#include "FairMQProgOptions.h" // device->fConfig
using namespace std;
FairMQExample1Sink::FairMQExample1Sink()
: fMaxIterations(0)
, fNumIterations(0)
{
// register a handler for data arriving on "data" channel
OnData("data", &FairMQExample1Sink::HandleData);
}
void FairMQExample1Sink::InitTask()
{
// Get the fMaxIterations value from the command line options (via fConfig)
fMaxIterations = fConfig->GetValue<uint64_t>("max-iterations");
}
// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
bool FairMQExample1Sink::HandleData(FairMQMessagePtr& msg, int /*index*/)
{
LOG(INFO) << "Received: \"" << string(static_cast<char*>(msg->GetData()), msg->GetSize()) << "\"";
// return true if want to be called again (otherwise go to IDLE state)
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
{
LOG(INFO) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
// return true if want to be called again (otherwise return false go to IDLE state)
return true;
}

View File

@@ -24,7 +24,12 @@ class FairMQExample1Sink : public FairMQDevice
virtual ~FairMQExample1Sink();
protected:
virtual void InitTask();
bool HandleData(FairMQMessagePtr&, int);
private:
uint64_t fMaxIterations;
uint64_t fNumIterations;
};
#endif /* FAIRMQEXAMPLE1SINK_H_ */

View File

@@ -14,7 +14,8 @@ namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("text", bpo::value<std::string>()->default_value("Hello"), "Text to send out");
("text", bpo::value<std::string>()->default_value("Hello"), "Text to send out")
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const FairMQProgOptions& /*config*/)

View File

@@ -11,8 +11,10 @@
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& /*options*/)
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const FairMQProgOptions& /*config*/)

View File

@@ -0,0 +1,26 @@
#!/bin/bash
ex1config="@CMAKE_BINARY_DIR@/bin/config/ex1-sampler-sink.json"
# setup a trap to kill everything if the test fails/timeouts
trap 'kill -TERM $SAMPLER_PID; kill -TERM $SINK_PID; wait $SAMPLER_PID; wait $SINK_PID;' TERM
SAMPLER="ex1-sampler"
SAMPLER+=" --id sampler1"
SAMPLER+=" --control static --log-color false"
SAMPLER+=" --max-iterations 1"
SAMPLER+=" --mq-config $ex1config"
@CMAKE_BINARY_DIR@/bin/examples/MQ/1-sampler-sink/$SAMPLER &
SAMPLER_PID=$!
SINK="ex1-sink"
SINK+=" --id sink1"
SINK+=" --control static --log-color false"
SINK+=" --max-iterations 1"
SINK+=" --mq-config $ex1config"
@CMAKE_BINARY_DIR@/bin/examples/MQ/1-sampler-sink/$SINK &
SINK_PID=$!
# wait for sampler and sink to finish
wait $SAMPLER_PID
wait $SINK_PID