-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
source/communication_interfaces/test/tests/test_tcp_communication.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <gtest/gtest.h> | ||
#include <thread> | ||
|
||
#include "communication_interfaces/sockets/TCPClient.hpp" | ||
#include "communication_interfaces/sockets/TCPServer.hpp" | ||
|
||
using namespace communication_interfaces; | ||
|
||
class TestTCPSockets : public ::testing::Test { | ||
public: | ||
TestTCPSockets() { | ||
server_ = std::make_shared<sockets::TCPServer>(sockets::TCPServerConfiguration{6000, 50, true}); | ||
client_ = std::make_shared<sockets::TCPClient>(sockets::TCPClientConfiguration{"127.0.0.1", 6000, 50}); | ||
} | ||
|
||
std::thread start_server() { | ||
return std::thread([this] { this->serve(); }); | ||
} | ||
|
||
void serve() const { | ||
server_->open(); | ||
server_->connect(); | ||
ASSERT_TRUE(server_->is_connected()); | ||
std::string recv_message; | ||
EXPECT_TRUE(server_->receive_bytes(recv_message)); | ||
EXPECT_STREQ(recv_message.c_str(), client_message_.c_str()); | ||
EXPECT_TRUE(server_->send_bytes(server_message_)); | ||
} | ||
|
||
std::thread start_client() { | ||
return std::thread([this] { this->client(); }); | ||
} | ||
|
||
void client() const { | ||
client_->open(); | ||
client_->connect(); | ||
ASSERT_TRUE(client_->is_connected()); | ||
EXPECT_TRUE(client_->send_bytes(client_message_)); | ||
std::string recv_message; | ||
EXPECT_TRUE(client_->receive_bytes(recv_message)); | ||
EXPECT_STREQ(recv_message.c_str(), server_message_.c_str()); | ||
} | ||
|
||
std::string client_message_ = "Hello server"; | ||
std::string server_message_ = "Hello client"; | ||
|
||
std::shared_ptr<sockets::TCPClient> client_; | ||
std::shared_ptr<sockets::TCPServer> server_; | ||
}; | ||
|
||
TEST_F(TestTCPSockets, TestCommunication) { | ||
std::thread server = start_server(); | ||
usleep(100000); | ||
std::thread client = start_client(); | ||
|
||
client.join(); | ||
server.join(); | ||
} |