diff --git a/test/integration/twoProcsSrvCall.cc b/test/integration/twoProcsSrvCall.cc index c9e254b6..72bdde7a 100644 --- a/test/integration/twoProcsSrvCall.cc +++ b/test/integration/twoProcsSrvCall.cc @@ -30,18 +30,16 @@ #include "gtest/gtest.h" #include "test_config.hh" -using namespace gz; - -static bool responseExecuted; -static bool wrongResponseExecuted; - -static std::string partition; // NOLINT(*) -static std::string g_topic = "/foo"; // NOLINT(*) -static int data = 5; -static int counter = 0; - -static constexpr const char *kTwoProcsSrvCallReplierExe = +namespace { +bool responseExecuted {false}; +bool wrongResponseExecuted {false}; +int counter {0}; + +constexpr const int kData {5}; +constexpr const char* kTopic {"/foo"}; +constexpr const char *kTwoProcsSrvCallReplierExe = TWO_PROCS_SRV_CALL_REPLIER_EXE; +} // namespace ////////////////////////////////////////////////// /// \brief Initialize some global variables. @@ -54,9 +52,9 @@ void reset() ////////////////////////////////////////////////// /// \brief Service call response callback. -void response(const msgs::Int32 &_rep, const bool _result) +void response(const gz::msgs::Int32 &_rep, const bool _result) { - EXPECT_EQ(_rep.data(), data); + EXPECT_EQ(_rep.data(), kData); EXPECT_TRUE(_result); responseExecuted = true; @@ -65,25 +63,55 @@ void response(const msgs::Int32 &_rep, const bool _result) ////////////////////////////////////////////////// /// \brief Service call response callback. -void wrongResponse(const msgs::Vector3d &/*_rep*/, bool /*_result*/) +void wrongResponse(const gz::msgs::Vector3d &/*_rep*/, bool /*_result*/) { wrongResponseExecuted = true; } +////////////////////////////////////////////////// +class twoProcSrvCall: public testing::Test { + protected: + void SetUp() override { + gz::utils::env("GZ_PARTITION", this->prevPartition); + + // Get a random partition name. + this->partition = testing::getRandomNumber(); + + // Set the partition name for this process. + gz::utils::setenv("GZ_PARTITION", this->partition); + + this->pi = std::make_unique( + std::vector({ + kTwoProcsSrvCallReplierExe, this->partition})); + } + + void TearDown() override { + gz::utils::setenv("GZ_PARTITION", this->prevPartition); + + this->pi->Terminate(); + this->pi->Join(); + } + + private: + std::string prevPartition; + std::string partition; + std::unique_ptr pi; +}; + + ////////////////////////////////////////////////// /// \brief Two different nodes running in two different processes. One node /// advertises a service and the other requests a few service calls. -TEST(twoProcSrvCall, SrvTwoProcs) +TEST_F(twoProcSrvCall, SrvTwoProcs) { - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - reset(); - msgs::Int32 req; - req.set_data(data); + gz::msgs::Int32 req; + req.set_data(kData); + + gz::transport::Node node; + EXPECT_TRUE(node.Request(kTopic, req, response)); - transport::Node node; - EXPECT_TRUE(node.Request(g_topic, req, response)); int i = 0; while (i < 300 && !responseExecuted) @@ -99,7 +127,7 @@ TEST(twoProcSrvCall, SrvTwoProcs) // Make another request. reset(); - EXPECT_TRUE(node.Request(g_topic, req, response)); + EXPECT_TRUE(node.Request(kTopic, req, response)); i = 0; while (i < 300 && !responseExecuted) @@ -119,30 +147,28 @@ TEST(twoProcSrvCall, SrvTwoProcs) /// \brief This test spawns a service responser and a service requester. The /// requester uses a wrong type for the request argument. The test should verify /// that the service call does not succeed. -TEST(twoProcSrvCall, SrvRequestWrongReq) +TEST_F(twoProcSrvCall, SrvRequestWrongReq) { - msgs::Vector3d wrongReq; - msgs::Int32 rep; - bool result; + gz::msgs::Vector3d wrongReq; + gz::msgs::Int32 rep; + bool result {false}; unsigned int timeout = 1000; - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - wrongReq.set_x(1); wrongReq.set_y(2); wrongReq.set_z(3); reset(); - transport::Node node; + gz::transport::Node node; // Request an asynchronous service call with wrong type in the request. - EXPECT_TRUE(node.Request(g_topic, wrongReq, response)); + EXPECT_TRUE(node.Request(kTopic, wrongReq, response)); std::this_thread::sleep_for(std::chrono::milliseconds(300)); EXPECT_FALSE(responseExecuted); // Request a synchronous service call with wrong type in the request. - EXPECT_FALSE(node.Request(g_topic, wrongReq, timeout, rep, result)); + EXPECT_FALSE(node.Request(kTopic, wrongReq, timeout, rep, result)); reset(); } @@ -151,28 +177,26 @@ TEST(twoProcSrvCall, SrvRequestWrongReq) /// \brief This test spawns a service responser and a service requester. The /// requester uses a wrong type for the response argument. The test should /// verify that the service call does not succeed. -TEST(twoProcSrvCall, SrvRequestWrongRep) +TEST_F(twoProcSrvCall, SrvRequestWrongRep) { - msgs::Int32 req; - msgs::Vector3d wrongRep; - bool result; + gz::msgs::Int32 req; + gz::msgs::Vector3d wrongRep; + bool result {false}; unsigned int timeout = 1000; - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - - req.set_data(data); + req.set_data(kData); reset(); - transport::Node node; + gz::transport::Node node; // Request an asynchronous service call with wrong type in the response. - EXPECT_TRUE(node.Request(g_topic, req, wrongResponse)); + EXPECT_TRUE(node.Request(kTopic, req, wrongResponse)); std::this_thread::sleep_for(std::chrono::milliseconds(300)); EXPECT_FALSE(wrongResponseExecuted); // Request a synchronous service call with wrong type in the response. - EXPECT_FALSE(node.Request(g_topic, req, timeout, wrongRep, result)); + EXPECT_FALSE(node.Request(kTopic, req, timeout, wrongRep, result)); reset(); } @@ -182,35 +206,33 @@ TEST(twoProcSrvCall, SrvRequestWrongRep) /// service requesters use incorrect types in some of the requests. The test /// should verify that a response is received only when the appropriate types /// are used. -TEST(twoProcSrvCall, SrvTwoRequestsOneWrong) +TEST_F(twoProcSrvCall, SrvTwoRequestsOneWrong) { - msgs::Int32 req; - msgs::Int32 goodRep; - msgs::Vector3d badRep; - bool result; + gz::msgs::Int32 req; + gz::msgs::Int32 goodRep; + gz::msgs::Vector3d badRep; + bool result {false}; unsigned int timeout = 2000; - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - - req.set_data(data); + req.set_data(kData); reset(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); - transport::Node node; + gz::transport::Node node; // Request service calls with wrong types in the response. - EXPECT_FALSE(node.Request(g_topic, req, timeout, badRep, result)); - EXPECT_TRUE(node.Request(g_topic, req, wrongResponse)); + EXPECT_FALSE(node.Request(kTopic, req, timeout, badRep, result)); + EXPECT_TRUE(node.Request(kTopic, req, wrongResponse)); std::this_thread::sleep_for(std::chrono::milliseconds(300)); EXPECT_FALSE(wrongResponseExecuted); reset(); // Valid service requests. - EXPECT_TRUE(node.Request(g_topic, req, timeout, goodRep, result)); - EXPECT_TRUE(node.Request(g_topic, req, response)); + EXPECT_TRUE(node.Request(kTopic, req, timeout, goodRep, result)); + EXPECT_TRUE(node.Request(kTopic, req, response)); std::this_thread::sleep_for(std::chrono::milliseconds(300)); EXPECT_TRUE(responseExecuted); @@ -221,13 +243,11 @@ TEST(twoProcSrvCall, SrvTwoRequestsOneWrong) /// \brief This test spawns two nodes on different processes. One of the nodes /// advertises a service and the other uses ServiceList() for getting the list /// of available services. -TEST(twoProcSrvCall, ServiceList) +TEST_F(twoProcSrvCall, ServiceList) { - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - reset(); - transport::Node node; + gz::transport::Node node; // We need some time for discovering the other node. std::this_thread::sleep_for(std::chrono::milliseconds(2500)); @@ -237,7 +257,7 @@ TEST(twoProcSrvCall, ServiceList) node.ServiceList(services); auto end1 = std::chrono::steady_clock::now(); ASSERT_EQ(services.size(), 1u); - EXPECT_EQ(services.at(0), g_topic); + EXPECT_EQ(services.at(0), kTopic); services.clear(); // Time elapsed to get the first service list @@ -247,7 +267,7 @@ TEST(twoProcSrvCall, ServiceList) node.ServiceList(services); auto end2 = std::chrono::steady_clock::now(); EXPECT_EQ(services.size(), 1u); - EXPECT_EQ(services.at(0), g_topic); + EXPECT_EQ(services.at(0), kTopic); // The first ServiceList() call might block if the discovery is still // initializing (it may happen if we run this test alone). @@ -266,14 +286,12 @@ TEST(twoProcSrvCall, ServiceList) /// \brief This test spawns two nodes on different processes. One of the nodes /// advertises a service and the other uses ServiceInfo() for getting /// information about the service. -TEST(twoProcSrvCall, ServiceInfo) +TEST_F(twoProcSrvCall, ServiceInfo) { - auto pi = gz::utils::Subprocess({kTwoProcsSrvCallReplierExe, partition}); - reset(); - transport::Node node; - std::vector publishers; + gz::transport::Node node; + std::vector publishers; // We need some time for discovering the other node. std::this_thread::sleep_for(std::chrono::milliseconds(2500)); @@ -291,19 +309,3 @@ TEST(twoProcSrvCall, ServiceInfo) reset(); } - -////////////////////////////////////////////////// -int main(int argc, char **argv) -{ - // Get a random partition name. - partition = testing::getRandomNumber(); - - // Set the partition name for this process. - gz::utils::setenv("GZ_PARTITION", partition); - - // Enable verbose mode. - // gz::utils::setenv("GZ_VERBOSE", "1"); - - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -}