From e12b649b517a2a2bb5bb1a594a36c78e18eefd07 Mon Sep 17 00:00:00 2001 From: Jens Georg Date: Tue, 22 Sep 2020 17:57:05 +0200 Subject: [PATCH] rebot tests: Use random server ports Make it possible to run the tests in parallel Fixes #109 --- .../include/RebotDummyServer.h | 1 + tests/RebotDummyServer/main.cpp | 1 + .../testRebotConnectionTimeouts.cpp | 8 +++--- tests/scripts/testRebotBackend.sh | 25 ++++++++++++++----- .../testRebotBackendCreation.cpp | 14 ++++++++--- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/tests/RebotDummyServer/include/RebotDummyServer.h b/tests/RebotDummyServer/include/RebotDummyServer.h index 6ccac4789..39bf36c81 100644 --- a/tests/RebotDummyServer/include/RebotDummyServer.h +++ b/tests/RebotDummyServer/include/RebotDummyServer.h @@ -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 session() { return _currentSession.lock(); } diff --git a/tests/RebotDummyServer/main.cpp b/tests/RebotDummyServer/main.cpp index 5e44a903a..502f3dbcb 100644 --- a/tests/RebotDummyServer/main.cpp +++ b/tests/RebotDummyServer/main.cpp @@ -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; diff --git a/tests/executables_src/testRebotConnectionTimeouts.cpp b/tests/executables_src/testRebotConnectionTimeouts.cpp index 9230a9ee9..aa591f123 100644 --- a/tests/executables_src/testRebotConnectionTimeouts.cpp +++ b/tests/executables_src/testRebotConnectionTimeouts.cpp @@ -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)); @@ -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); @@ -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); @@ -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); diff --git a/tests/scripts/testRebotBackend.sh b/tests/scripts/testRebotBackend.sh index 6a9bea115..b35f0be61 100755 --- a/tests/scripts/testRebotBackend.sh +++ b/tests/scripts/testRebotBackend.sh @@ -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 diff --git a/tests/unitTestsNotUnderCtest/testRebotBackendCreation.cpp b/tests/unitTestsNotUnderCtest/testRebotBackendCreation.cpp index abf6740bc..96fac86df 100644 --- a/tests/unitTestsNotUnderCtest/testRebotBackendCreation.cpp +++ b/tests/unitTestsNotUnderCtest/testRebotBackendCreation.cpp @@ -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) @@ -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("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(ChimeraTK::numeric_address::BAR / 0 / 0xC) == 48 << 3); // The user register // is on bar 0, // address 0xC. We