31 #include <gtest/gtest.h> 32 #include <condition_variable> 48 std::string host =
"127.0.0.1";
49 TCPSocket::setup(host, port);
52 void send(
const std::string& text)
54 size_t len = text.size();
55 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(text.c_str());
57 TCPSocket::write(data, len, written);
62 std::stringstream result;
64 size_t read_chars = 99;
65 while (read_chars > 0)
67 TCPSocket::read((uint8_t*)&character, 1, read_chars);
69 if (character ==
'\n')
78 virtual bool open(
int socket_fd,
struct sockaddr* address,
size_t address_len)
80 return ::connect(socket_fd, address, address_len) == 0;
87 std::lock_guard<std::mutex> lk(connect_mutex_);
88 client_fd_ = filedescriptor;
89 connect_cv_.notify_one();
90 connection_callback_ =
true;
95 std::lock_guard<std::mutex> lk(disconnect_mutex_);
97 disconnect_cv_.notify_one();
98 disconnection_callback_ =
true;
103 std::lock_guard<std::mutex> lk(message_mutex_);
104 message_ = std::string(buffer);
105 message_cv_.notify_one();
106 message_callback_ =
true;
111 std::unique_lock<std::mutex> lk(connect_mutex_);
112 if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
113 connection_callback_ ==
true)
115 connection_callback_ =
false;
126 std::unique_lock<std::mutex> lk(disconnect_mutex_);
127 if (disconnect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
128 disconnection_callback_ ==
true)
130 disconnection_callback_ =
false;
141 std::unique_lock<std::mutex> lk(message_mutex_);
142 if (message_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
143 message_callback_ ==
true)
145 message_callback_ =
false;
155 std::string message_ =
"";
168 bool connection_callback_ =
false;
169 bool disconnection_callback_ =
false;
170 bool message_callback_ =
false;
183 ASSERT_NO_THROW(Client client(port_));
184 Client client(port_);
187 EXPECT_NO_THROW(client.send(
"message\n"));
188 EXPECT_NO_THROW(waitForMessageCallback());
190 EXPECT_NO_THROW(client.close());
191 EXPECT_NO_THROW(waitForDisconnectionCallback());
197 server.
setMessageCallback(std::bind(&TCPServerTest_callback_functions_Test::messageCallback,
this,
198 std::placeholders::_1, std::placeholders::_2));
200 std::bind(&TCPServerTest_callback_functions_Test::connectionCallback,
this, std::placeholders::_1));
202 std::bind(&TCPServerTest_callback_functions_Test::disconnectionCallback,
this, std::placeholders::_1));
206 Client client(port_);
207 EXPECT_TRUE(waitForConnectionCallback());
209 client.send(
"message\n");
210 EXPECT_TRUE(waitForMessageCallback());
213 EXPECT_TRUE(waitForDisconnectionCallback());
219 server.
setMessageCallback(std::bind(&TCPServerTest_unlimited_clients_allowed_Test::messageCallback,
this,
220 std::placeholders::_1, std::placeholders::_2));
222 std::bind(&TCPServerTest_unlimited_clients_allowed_Test::connectionCallback,
this, std::placeholders::_1));
224 std::bind(&TCPServerTest_unlimited_clients_allowed_Test::disconnectionCallback,
this, std::placeholders::_1));
228 std::vector<Client*> clients;
230 for (
unsigned int i = 0; i < 100; ++i)
232 client =
new Client(port_);
233 ASSERT_TRUE(waitForConnectionCallback());
234 clients.push_back(client);
241 server.
setMessageCallback(std::bind(&TCPServerTest_max_clients_allowed_Test::messageCallback,
this,
242 std::placeholders::_1, std::placeholders::_2));
244 std::bind(&TCPServerTest_max_clients_allowed_Test::connectionCallback,
this, std::placeholders::_1));
246 std::bind(&TCPServerTest_max_clients_allowed_Test::disconnectionCallback,
this, std::placeholders::_1));
251 Client client1(port_);
252 EXPECT_TRUE(waitForConnectionCallback());
253 Client client2(port_);
254 EXPECT_FALSE(waitForConnectionCallback());
260 server.
setMessageCallback(std::bind(&TCPServerTest_message_transmission_Test::messageCallback,
this,
261 std::placeholders::_1, std::placeholders::_2));
263 std::bind(&TCPServerTest_message_transmission_Test::connectionCallback,
this, std::placeholders::_1));
265 std::bind(&TCPServerTest_message_transmission_Test::disconnectionCallback,
this, std::placeholders::_1));
268 Client client(port_);
269 EXPECT_TRUE(waitForConnectionCallback());
272 std::string message =
"test message\n";
273 client.send(message);
275 EXPECT_TRUE(waitForMessageCallback());
276 EXPECT_EQ(message, message_);
278 size_t len = message.size();
279 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(message.c_str());
282 ASSERT_TRUE(server.
write(client_fd_, data, len, written));
283 EXPECT_EQ(client.recv(), message);
289 server.
setMessageCallback(std::bind(&TCPServerTest_client_connections_Test::messageCallback,
this,
290 std::placeholders::_1, std::placeholders::_2));
292 std::bind(&TCPServerTest_client_connections_Test::connectionCallback,
this, std::placeholders::_1));
294 std::bind(&TCPServerTest_client_connections_Test::disconnectionCallback,
this, std::placeholders::_1));
297 std::string message =
"text message\n";
298 size_t len = message.size();
299 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(message.c_str());
303 Client client1(port_);
304 EXPECT_TRUE(waitForConnectionCallback());
305 int client1_fd = client_fd_;
307 Client client2(port_);
308 EXPECT_TRUE(waitForConnectionCallback());
309 int client2_fd = client_fd_;
311 Client client3(port_);
312 EXPECT_TRUE(waitForConnectionCallback());
313 int client3_fd = client_fd_;
317 EXPECT_TRUE(waitForDisconnectionCallback());
319 EXPECT_FALSE(server.
write(client1_fd, data, len, written));
320 EXPECT_TRUE(server.
write(client2_fd, data, len, written));
321 EXPECT_TRUE(server.
write(client3_fd, data, len, written));
324 EXPECT_TRUE(waitForDisconnectionCallback());
325 EXPECT_FALSE(server.
write(client1_fd, data, len, written));
326 EXPECT_FALSE(server.
write(client2_fd, data, len, written));
327 EXPECT_TRUE(server.
write(client3_fd, data, len, written));
330 EXPECT_TRUE(waitForDisconnectionCallback());
331 EXPECT_FALSE(server.
write(client1_fd, data, len, written));
332 EXPECT_FALSE(server.
write(client2_fd, data, len, written));
333 EXPECT_FALSE(server.
write(client3_fd, data, len, written));
336 int main(
int argc,
char* argv[])
338 ::testing::InitGoogleTest(&argc, argv);
340 return RUN_ALL_TESTS();
void setDisconnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients disconnecting from the server.
bool waitForConnectionCallback(int milliseconds=100)
void setMaxClientsAllowed(const uint32_t &max_clients_allowed)
Set the maximum number of clients allowed to connect to this server.
void send(const std::string &text)
void setConnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients connecting to the server.
std::condition_variable message_cv_
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
TEST_F(TCPServerTest, socket_creation)
Wrapper class for a TCP socket server.
void start()
Start event handling.
void connectionCallback(const int filedescriptor)
void messageCallback(const int filedescriptor, char *buffer)
bool waitForDisconnectionCallback(int milliseconds=100)
bool waitForMessageCallback(int milliseconds=100)
std::condition_variable connect_cv_
std::mutex message_mutex_
std::condition_variable disconnect_cv_
bool write(const int fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
std::mutex disconnect_mutex_
std::mutex connect_mutex_
Class for TCP socket abstraction.
void disconnectionCallback(const int filedescriptor)
void setMessageCallback(std::function< void(const int, char *)> func)
This callback will be triggered on messages received on the socket.
int main(int argc, char *argv[])