Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domire8 committed May 26, 2023
1 parent 4e7216b commit 31f401e
Showing 1 changed file with 58 additions and 0 deletions.
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();
}

0 comments on commit 31f401e

Please sign in to comment.