31 #include <gtest/gtest.h> 32 #include <condition_variable> 47 server_->setMessageCallback(
51 client_.reset(
new Client(60001));
63 std::lock_guard<std::mutex> lk(message_mutex_);
64 received_message_ = std::string(buffer);
65 message_cv_.notify_one();
66 message_callback_ =
true;
71 std::lock_guard<std::mutex> lk(connect_mutex_);
72 client_fd_ = filedescriptor;
73 connect_cv_.notify_one();
74 connection_callback_ =
true;
79 std::unique_lock<std::mutex> lk(message_mutex_);
80 if (message_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
81 message_callback_ ==
true)
83 message_callback_ =
false;
91 std::unique_lock<std::mutex> lk(connect_mutex_);
92 if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
93 connection_callback_ ==
true)
95 connection_callback_ =
false;
111 std::string ip =
"127.0.0.1";
112 return TCPSocket::setup(ip, port_);
117 done_setting_up_client_ =
false;
118 client_setup_thread_ = std::thread(&Client::setupClient,
this, port_);
123 unsigned int max_count = 50;
124 unsigned int count = 0;
125 while (count < max_count)
127 if (done_setting_up_client_)
129 client_setup_thread_.join();
132 std::this_thread::sleep_for(std::chrono::milliseconds(100));
135 client_setup_thread_.detach();
140 virtual bool open(
int socket_fd,
struct sockaddr* address,
size_t address_len)
142 return ::connect(socket_fd, address, address_len) == 0;
152 std::string ip =
"127.0.0.1";
153 TCPSocket::setup(ip, port);
154 done_setting_up_client_ =
true;
171 bool connection_callback_ =
false;
172 bool message_callback_ =
false;
186 actual_state = client_->getState();
193 actual_state = client_->getState();
203 client_->setReconnectionTime(std::chrono::seconds(1));
204 client_->setupClientBeforeServer();
207 std::this_thread::sleep_for(std::chrono::seconds(1));
219 EXPECT_TRUE(client_->waitForClientSetupThread());
221 actual_state = client_->getState();
229 std::string expected_ip =
"";
230 std::string actual_ip = client_->getIP();
232 EXPECT_EQ(expected_ip, actual_ip);
235 expected_ip =
"127.0.0.1";
236 actual_ip = client_->getIP();
238 EXPECT_EQ(expected_ip, actual_ip);
243 std::string message =
"test message";
244 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(message.c_str());
245 size_t len = message.size();
248 EXPECT_FALSE(client_->write(data, len, written));
254 size_t read_chars = 0;
256 EXPECT_FALSE(client_->read((uint8_t*)&character, 1, read_chars));
263 std::string message =
"test message";
264 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(message.c_str());
265 size_t len = message.size();
267 client_->write(data, len, written);
269 EXPECT_TRUE(waitForMessageCallback());
270 EXPECT_EQ(message, received_message_);
278 EXPECT_TRUE(waitForConnectionCallback());
280 std::string send_message =
"test message";
281 size_t len = send_message.size();
282 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(send_message.c_str());
284 server_->write(client_fd_, data, len, written);
286 std::stringstream ss;
288 size_t read_chars = 0;
291 client_->read((uint8_t*)&characters, 1, read_chars);
296 std::string received_message = ss.str();
298 EXPECT_EQ(send_message, received_message);
304 int expected_fd = -1;
305 int actual_fd = client_->getSocketFD();
307 EXPECT_EQ(expected_fd, actual_fd);
310 actual_fd = client_->getSocketFD();
313 EXPECT_NE(expected_fd, actual_fd);
316 actual_fd = client_->getSocketFD();
318 EXPECT_EQ(expected_fd, actual_fd);
328 client_->setReceiveTimeout(tv);
331 size_t read_chars = 0;
334 EXPECT_FALSE(client_->read((uint8_t*)&character, 1, read_chars));
341 EXPECT_FALSE(client_->setup());
344 int main(
int argc,
char* argv[])
346 ::testing::InitGoogleTest(&argc, argv);
348 return RUN_ALL_TESTS();
std::condition_variable message_cv_
void connectionCallback(const int filedescriptor)
void messageCallback(const int filedescriptor, char *buffer)
Connection to socket got closed.
bool done_setting_up_client_
void setupClientBeforeServer()
Wrapper class for a TCP socket server.
SocketState
State the socket can be in.
void setupClient(int port)
std::mutex message_mutex_
bool waitForMessageCallback(int milliseconds=100)
Socket is initialized or setup failed.
int main(int argc, char *argv[])
std::thread client_setup_thread_
TEST_F(TCPSocketTest, socket_state)
std::condition_variable connect_cv_
std::unique_ptr< comm::TCPServer > server_
std::mutex connect_mutex_
std::unique_ptr< Client > client_
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
bool waitForClientSetupThread()
Class for TCP socket abstraction.
constexpr std::underlying_type< E >::type toUnderlying(const E e) noexcept
Converts an enum type to its underlying type.
Socket is connected and ready to use.
std::string received_message_
bool waitForConnectionCallback(int milliseconds=100)