Skip to content

Commit

Permalink
rebot tests: Use random server ports
Browse files Browse the repository at this point in the history
Make it possible to run the tests in parallel

Fixes #109
  • Loading branch information
phako committed Sep 22, 2020
1 parent cbdb8b4 commit e12b649
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions tests/RebotDummyServer/include/RebotDummyServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace ChimeraTK {
void start();
void stop();
bool is_running();
unsigned int port() const { return _connectionAcceptor.local_endpoint().port(); }

boost::asio::io_service& service() { return _io; }
std::shared_ptr<RebotDummySession> session() { return _currentSession.lock(); }
Expand Down
1 change: 1 addition & 0 deletions tests/RebotDummyServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int main(int, char** argv) {
});

std::cout << "Rebot dummy server started" << std::endl;
std::cout << "PORT " << testServer.port() << std::endl;
testServer.start();
std::cout << "Rebot dummy server stopped" << std::endl;

Expand Down
8 changes: 4 additions & 4 deletions tests/executables_src/testRebotConnectionTimeouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace ChimeraTK;
// Test fixture for setup and teardown
struct F {
F()
: rebotServer{5001 /*port*/, "./mtcadummy_rebot.map", 1 /*protocol version*/},
: rebotServer{0 /*use random port*/, "./mtcadummy_rebot.map", 1 /*protocol version*/},
serverThread([&]() { rebotServer.start(); }) {
while(not rebotServer.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Expand All @@ -35,7 +35,7 @@ BOOST_FIXTURE_TEST_CASE(testOpenConnection, F) {

uint32_t timeout_sec = 1;
auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
Device d("sdm://./rebot=localhost,5001,mtcadummy_rebot.map," + std::to_string(timeout_sec));
Device d("sdm://./rebot=localhost," + std::to_string(rebotServer.port()) + ",mtcadummy_rebot.map," + std::to_string(timeout_sec));

BOOST_CHECK(d.isFunctional() == false);

Expand All @@ -52,7 +52,7 @@ BOOST_FIXTURE_TEST_CASE(testOpenConnection, F) {
BOOST_FIXTURE_TEST_CASE(testReadTimeout, F) {
uint32_t timeout_sec = 1;
auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
Device d("sdm://./rebot=localhost,5001,mtcadummy_rebot.map," + std::to_string(timeout_sec));
Device d("sdm://./rebot=localhost," + std::to_string(rebotServer.port()) + ",mtcadummy_rebot.map," + std::to_string(timeout_sec));

BOOST_CHECK(d.isFunctional() == false);

Expand All @@ -74,7 +74,7 @@ BOOST_FIXTURE_TEST_CASE(testReadTimeout, F) {
BOOST_FIXTURE_TEST_CASE(testWriteTimeout, F) {
uint32_t timeout_sec = 1;
auto accetable_completion_time = std::chrono::seconds(timeout_sec * 5);
Device d("sdm://./rebot=localhost,5001,mtcadummy_rebot.map," + std::to_string(timeout_sec));
Device d("sdm://./rebot=localhost," + std::to_string(rebotServer.port()) + ",mtcadummy_rebot.map," + std::to_string(timeout_sec));

BOOST_CHECK(d.isFunctional() == false);

Expand Down
25 changes: 19 additions & 6 deletions tests/scripts/testRebotBackend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@
TEST_RESULT=0

for PROTOCO_VERSION in 0 1; do

#start the server with the protocol version to test against
./RebotDummyServer -m ./mtcadummy_rebot.map -V $PROTOCO_VERSION&
# -p 0 will make the server start on a random port. It will print the port it uses
# on stdout, eg. "PORT 32442"
output=`mktemp`
./RebotDummyServer -m ./mtcadummy_rebot.map -V $PROTOCO_VERSION -p 0 >$output &
SERVER_PID=$!
sleep .1

# We take the port from the server and modify the DMAP files accordingly.
# It is passed on to the "subtests" as well as the server port itself
PORT=`awk '/^PORT /{print $2}' $output`
DMAP=`mktemp -p . -t XXXXXXXXX.dmap`
echo "Using server port $PORT"
echo "Using dmap file $DMAP"

sed -e "s|,5001|,$PORT|g" dummies.dmap > "$DMAP"

# run the test now; mskrebot uses ip address of the server in the dmap file
./testRebotBackend mskrebot ./dummies.dmap
./testRebotBackend mskrebot "$DMAP" "$PORT"
if [ $? -ne 0 ] ; then # The above test failed;
echo "Testing backed using the IP addess with protocol version ${PROTOCO_VERSION} failed!"
TEST_RESULT=$(( ( $TEST_RESULT + 1 ) + ( ${PROTOCO_VERSION} *10 ) ))
fi

# mskrebot1 uses hostname of the server in the dmap file
./testRebotBackend mskrebot1 ./dummies.dmap
./testRebotBackend mskrebot1 "$DMAP" "$PORT"
if [ $? -ne 0 ] ; then # The above test failed;
echo "Testing backed using the hostname with protocol version ${PROTOCO_VERSION} failed!"
TEST_RESULT=$(( ( $TEST_RESULT + 2 ) + ( ${PROTOCO_VERSION} *10 ) ))
fi

./testRebotBackendCreation
./testRebotBackendCreation "$DMAP" "$PORT"
if [ $? -ne 0 ] ; then # The above test failed;
echo "Testing RebotBackend creation failed!"
TEST_RESULT=$(( $TEST_RESULT + 4 ))
fi
fi

rm -f "$DMAP" "$output"

kill $SERVER_PID
#a small sleep so the port is actually freed for the next server which is
Expand Down
14 changes: 11 additions & 3 deletions tests/unitTestsNotUnderCtest/testRebotBackendCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ BOOST_AUTO_TEST_SUITE(RebotDeviceTestSuite)
BOOST_AUTO_TEST_CASE(testFactoryForRebotDeviceCreation) {
// set dmap file path
auto dmapPathBackup = ChimeraTK::getDMapFilePath();
ChimeraTK::setDMapFilePath("./dummies.dmap");
std::string dmapPath{"./dummies.dmap"};
if(framework::master_test_suite().argc > 1) {
dmapPath = framework::master_test_suite().argv[1];
}
ChimeraTK::setDMapFilePath(dmapPath);
std::string port{"5001"};
if(framework::master_test_suite().argc > 2) {
port = framework::master_test_suite().argv[2];
}

// There are four situations where the map-file information is coming from
// 1. From the dmap file (old way, third column in dmap file)
Expand All @@ -49,13 +57,13 @@ BOOST_AUTO_TEST_CASE(testFactoryForRebotDeviceCreation) {
// creation because we have to bypass the dmap file parser which at the time
// of writing this requires a map file as third column
ChimeraTK::Device secondDevice;
secondDevice.open("sdm://./rebot=localhost,5001,mtcadummy_rebot.map");
secondDevice.open("sdm://./rebot=localhost," + port + ",mtcadummy_rebot.map");
BOOST_CHECK(secondDevice.read<double>("BOARD/WORD_USER") == 48);
secondDevice.close();

// 3. We don't have a map file, so we have to use numerical addressing
ChimeraTK::Device thirdDevice;
thirdDevice.open("sdm://./rebot=localhost,5001");
thirdDevice.open("sdm://./rebot=localhost," + port);
BOOST_CHECK(thirdDevice.read<int32_t>(ChimeraTK::numeric_address::BAR / 0 / 0xC) == 48 << 3); // The user register
// is on bar 0,
// address 0xC. We
Expand Down

0 comments on commit e12b649

Please sign in to comment.