2424#include " Namespace.hh"
2525#include " common/WebNotify.hh"
2626#include " gtest/gtest.h"
27+ #include < algorithm>
28+ #include < execution>
2729#include < iostream>
2830#include < string>
2931#include < vector>
3638#include < sys/socket.h>
3739#include < netinet/in.h>
3840
39- class SimpleTCPServer {
41+ class SimpleTCPServer
42+ {
4043public:
41- explicit SimpleTCPServer (int port) : port_(port), ready_(false ), running_(false ) { Start (); }
42-
43- ~SimpleTCPServer () {
44+ explicit SimpleTCPServer (int port) : port_(port), ready_(false ),
45+ running_(false )
46+ {
47+ Start ();
48+ }
49+
50+ ~SimpleTCPServer ()
51+ {
4452 Shutdown ();
53+
4554 if (server_thread_.joinable ()) {
4655 server_thread_.join (); // Avoid std::terminate()
4756 }
4857 }
49-
50- void Start () {
51- if (running_) return ; // Don't start twice
52-
58+
59+ void Start ()
60+ {
61+ if (running_) {
62+ return ; // Don't start twice
63+ }
64+
5365 running_ = true ;
5466 server_thread_ = std::thread (&SimpleTCPServer::RunServer, this );
5567 }
5668
57- std::vector<char > GetMessage () {
69+ std::vector<char > GetMessage ()
70+ {
5871 std::unique_lock<std::mutex> lock (msg_mutex_);
5972 cv_.wait (lock, [&ready = ready_] { return ready.load (); });
6073 return message_;
6174 }
62-
75+
6376private:
64- void RunServer () {
77+ void RunServer ()
78+ {
6579 int server_fd, client_fd;
6680 struct sockaddr_in address {};
6781 socklen_t addrlen = sizeof (address);
6882 const int buffer_size = 4096 ;
6983 char buffer[buffer_size];
70-
7184 server_fd = socket (AF_INET, SOCK_STREAM, 0 );
85+
7286 if (server_fd < 0 ) {
7387 perror (" Socket failed" );
7488 return ;
7589 }
76-
90+
7791 server_fd_ = server_fd; // Save for shutdown
78-
7992 int opt = 1 ;
8093 setsockopt (server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
81-
8294 address.sin_family = AF_INET;
8395 address.sin_addr .s_addr = INADDR_ANY;
8496 address.sin_port = htons (port_);
85-
97+
8698 if (bind (server_fd, (struct sockaddr *)&address, sizeof (address)) < 0 ) {
8799 perror (" Bind failed" );
88100 close (server_fd);
89101 return ;
90102 }
91-
103+
92104 if (listen (server_fd, 1 ) < 0 ) {
93105 perror (" Listen failed" );
94106 close (server_fd);
95107 return ;
96108 }
97-
109+
98110 std::cout << " Server listening on port " << port_ << " \n " ;
99-
100111 client_fd = accept (server_fd, (struct sockaddr *)&address, &addrlen);
112+
101113 if (client_fd < 0 ) {
102- if (running_) perror (" Accept failed" );
114+ if (running_) {
115+ perror (" Accept failed" );
116+ }
117+
103118 close (server_fd);
104119 return ;
105120 }
106-
121+
107122 std::vector<char > local_msg;
108123 ssize_t bytes_read;
124+
109125 while ((bytes_read = read (client_fd, buffer, buffer_size)) > 0 ) {
110126 local_msg.insert (local_msg.end (), buffer, buffer + bytes_read);
111127 break ;
112128 }
113-
129+
114130 {
115131 std::lock_guard<std::mutex> lock (msg_mutex_);
116132 message_ = std::move (local_msg);
117133 ready_ = true ;
118134 }
119-
135+
120136 close (client_fd);
121137 close (server_fd);
122138 running_ = false ;
123139 cv_.notify_all ();
124140 }
125-
126- void Shutdown () {
141+
142+ void Shutdown ()
143+ {
127144 if (running_) {
128145 running_ = false ;
146+
129147 if (server_fd_ != -1 ) {
130- shutdown (server_fd_, SHUT_RDWR);
131- close (server_fd_);
132- server_fd_ = -1 ;
148+ shutdown (server_fd_, SHUT_RDWR);
149+ close (server_fd_);
150+ server_fd_ = -1 ;
133151 }
152+
134153 if (server_thread_.joinable ()) {
135- server_thread_.join ();
154+ server_thread_.join ();
136155 }
137156 }
138157 }
139-
158+
140159 int port_;
141160 std::thread server_thread_;
142161 std::vector<char > message_;
@@ -147,106 +166,122 @@ class SimpleTCPServer {
147166 int server_fd_ = -1 ;
148167};
149168
150- std::string to_visible_string (const std::vector<char >& in) {
151- std::string out;
152- char buf[5 ];
153- for (unsigned char c : in)
154- out += (c == ' \n ' ) ? " \\ n" :
155- (c == ' \r ' ) ? " \\ r" :
156- (c == ' \t ' ) ? " \\ t" :
157- (c == ' \\ ' ) ? " \\\\ " :
158- (c == ' \" ' ) ? " \\\" " :
159- std::isprint (c) ? std::string (1 , c) :
160- (std::snprintf (buf, sizeof (buf), " \\ x%02x" , c), buf);
161- return out;
169+ std::string to_visible_string (const std::vector<char >& in)
170+ {
171+ std::string out;
172+ char buf[5 ];
173+
174+ for (unsigned char c : in)
175+ out += (c == ' \n ' ) ? " \\ n" :
176+ (c == ' \r ' ) ? " \\ r" :
177+ (c == ' \t ' ) ? " \\ t" :
178+ (c == ' \\ ' ) ? " \\\\ " :
179+ (c == ' \" ' ) ? " \\\" " :
180+ std::isprint (c) ? std::string (1 , c) :
181+ (std::snprintf (buf, sizeof (buf), " \\ x%02x" , c), buf);
182+
183+ return out;
162184}
163185
164186
165187EOSCOMMONNAMESPACE_BEGIN
166188
167- TEST (WebNotifyTimeoutTests, HttpPostNotification_TimesOut) {
168- WebNotify notifier;
169- std::string url = " http://10.255.255.1:12345" ; // Non-routable IP (used for timeout testing)
170- std::string message = " {\" event\" :\" timeout_test\" }" ;
171-
172- EXPECT_FALSE (notifier.sendHttpPostNotification (url, message, 250 ));
189+ TEST (WebNotifyTimeoutTests, HttpPostNotification_TimesOut)
190+ {
191+ WebNotify notifier;
192+ std::string url =
193+ " http://10.255.255.1:12345" ; // Non-routable IP (used for timeout testing)
194+ std::string message = " {\" event\" :\" timeout_test\" }" ;
195+ EXPECT_FALSE (notifier.sendHttpPostNotification (url, message, 250 ));
173196}
174197
175198
176- TEST (WebNotifyTimeoutTests, HttpPostNotification_OK) {
177- WebNotify notifier;
178- std::string url = " http://localhost:12345" ;
179- std::string message = " {\" event\" :\" ok_test\" }" ;
180- SimpleTCPServer server (12345 );
181- EXPECT_FALSE (notifier.sendHttpPostNotification (url, message, 250 ));
182- auto response_message = server.GetMessage (); // Blocks until message is received
183- std::cerr << to_visible_string (response_message) << std::endl;
184- EXPECT_EQ (" POST / HTTP/1.1\r\n Host: localhost:12345\r\n Accept: */*\r\n Content-Type: application/json\r\n Content-Length: 19\r\n\r\n {\" event\" :\" ok_test\" }" ,std::string (response_message.begin (), response_message.end ()));
199+ TEST (WebNotifyTimeoutTests, HttpPostNotification_OK)
200+ {
201+ WebNotify notifier;
202+ std::string url = " http://localhost:12345" ;
203+ std::string message = " {\" event\" :\" ok_test\" }" ;
204+ SimpleTCPServer server (12345 );
205+ EXPECT_FALSE (notifier.sendHttpPostNotification (url, message, 250 ));
206+ auto response_message =
207+ server.GetMessage (); // Blocks until message is received
208+ std::cerr << to_visible_string (response_message) << std::endl;
209+ EXPECT_EQ (" POST / HTTP/1.1\r\n Host: localhost:12345\r\n Accept: */*\r\n Content-Type: application/json\r\n Content-Length: 19\r\n\r\n {\" event\" :\" ok_test\" }" ,
210+ std::string (response_message.begin (), response_message.end ()));
185211}
186212
187- TEST (WebNotifyTimeoutTests, GrpcNotification_TimesOut) {
188- WebNotify notifier;
189- std::string target = " 10.255.255.1:50051" ; // Unreachable IP
190- std::string message = " gRPC timeout check" ;
191- EXPECT_FALSE (notifier.sendGrpcNotification (target, message, 250 ));
213+ TEST (WebNotifyTimeoutTests, GrpcNotification_TimesOut)
214+ {
215+ WebNotify notifier;
216+ std::string target = " 10.255.255.1:50051" ; // Unreachable IP
217+ std::string message = " gRPC timeout check" ;
218+ EXPECT_FALSE (notifier.sendGrpcNotification (target, message, 250 ));
192219}
193220
194- TEST (WebNotifyTimeoutTests, GrpcNotification_Ok) {
195- WebNotify notifier;
196- std::string target = " localhost:12345" ;
197- std::string message = " gRPC timeout check" ;
198- SimpleTCPServer server (12345 );
199- EXPECT_FALSE (notifier.sendGrpcNotification (target, message, 250 ));
221+ TEST (WebNotifyTimeoutTests, GrpcNotification_Ok)
222+ {
223+ WebNotify notifier;
224+ std::string target = " localhost:12345" ;
225+ std::string message = " gRPC timeout check" ;
226+ SimpleTCPServer server (12345 );
227+ EXPECT_FALSE (notifier.sendGrpcNotification (target, message, 250 ));
200228}
201229
202- TEST (WebNotifyTimeoutTests, QClientNotification_TimesOut) {
203- WebNotify notifier;
204- std::string target = " 10.255.255.1" ; // Unreachable IP
205- int port = 50051 ;
206- std::string message = " QCLient timeout check" ;
207- std::string channel = " Notification" ;
208- EXPECT_FALSE (notifier.sendQClientNotification (target, port, channel, message, 250 ));
230+ TEST (WebNotifyTimeoutTests, QClientNotification_TimesOut)
231+ {
232+ WebNotify notifier;
233+ std::string target = " 10.255.255.1" ; // Unreachable IP
234+ int port = 50051 ;
235+ std::string message = " QCLient timeout check" ;
236+ std::string channel = " Notification" ;
237+ EXPECT_FALSE (notifier.sendQClientNotification (target, port, channel, message,
238+ 250 ));
209239}
210240
211- TEST (WebNotifyTimeoutTests, QClientNotification_Ok) {
212- WebNotify notifier;
213- std::string target = " localhost" ;
214- int port = 12345 ;
215- std::string message = " QClient timeout check" ;
216- std::string channel = " Notification" ;
217- SimpleTCPServer server (12345 );
218- EXPECT_FALSE (notifier.sendQClientNotification (target, port, channel, message, 250 ));
219- auto response_message = server.GetMessage (); // Blocks until message is received
220- std::cerr << to_visible_string (response_message) << std::endl;
221- EXPECT_EQ (" *2\r\n $4\r\n PING\r\n $33\r\n qclient-connection-initialization\r\n " , std::string (response_message.begin (), response_message.end ()));
241+ TEST (WebNotifyTimeoutTests, QClientNotification_Ok)
242+ {
243+ WebNotify notifier;
244+ std::string target = " localhost" ;
245+ int port = 12345 ;
246+ std::string message = " QClient timeout check" ;
247+ std::string channel = " Notification" ;
248+ SimpleTCPServer server (12345 );
249+ EXPECT_FALSE (notifier.sendQClientNotification (target, port, channel, message,
250+ 250 ));
251+ auto response_message =
252+ server.GetMessage (); // Blocks until message is received
253+ std::cerr << to_visible_string (response_message) << std::endl;
254+ EXPECT_EQ (" *2\r\n $4\r\n PING\r\n $33\r\n qclient-connection-initialization\r\n " ,
255+ std::string (response_message.begin (), response_message.end ()));
222256}
223257
224- TEST (WebNotifyTimeoutTests, ActiveMQNotification_TimesOut) {
225- WebNotify notifier;
226- std::string brokerURI = " tcp://10.255.255.1:61616 " ; // Unreachable
227- std::string queue = " timeout_test " ;
228- std::string message = " ActiveMQ timeout check " ;
229-
230- EXPECT_FALSE (notifier.sendActiveMQNotification (brokerURI, queue, message, 250 ));
258+ TEST (WebNotifyTimeoutTests, ActiveMQNotification_TimesOut)
259+ {
260+ WebNotify notifier;
261+ std::string brokerURI = " tcp://10.255.255.1:61616 " ; // Unreachable
262+ std::string queue = " timeout_test " ;
263+ std::string message = " ActiveMQ timeout check " ;
264+ EXPECT_FALSE (notifier.sendActiveMQNotification (brokerURI, queue, message, 250 ));
231265}
232266
233- TEST (WebNotifyTimeoutTests, ActiveMQNotification_Ok) {
234- WebNotify notifier;
235- std::string brokerURI = " tcp://localhost:12345" ;
236- std::string queue = " timeout_test" ;
237- std::string message = " ActiveMQ timeout check" ;
238- SimpleTCPServer server (12345 ); // Non-blocking
239- EXPECT_FALSE (notifier.sendActiveMQNotification (brokerURI, queue, message, 250 ));
240- auto response_message = server.GetMessage (); // Blocks until message is received
241- auto containsActiveMQ = [](const std::vector<char >& response) {
242- const std::string target = " ActiveMQ" ;
243- return std::search (
244- response.begin (), response.end (),
245- target.begin (), target.end ()
246- ) != response.end ();
247- };
248-
249- EXPECT_TRUE (containsActiveMQ (response_message));
267+ TEST (WebNotifyTimeoutTests, ActiveMQNotification_Ok)
268+ {
269+ WebNotify notifier;
270+ std::string brokerURI = " tcp://localhost:12345" ;
271+ std::string queue = " timeout_test" ;
272+ std::string message = " ActiveMQ timeout check" ;
273+ SimpleTCPServer server (12345 ); // Non-blocking
274+ EXPECT_FALSE (notifier.sendActiveMQNotification (brokerURI, queue, message, 250 ));
275+ auto response_message =
276+ server.GetMessage (); // Blocks until message is received
277+ auto containsActiveMQ = [](const std::vector<char >& response) {
278+ const std::string target = " ActiveMQ" ;
279+ return std::search (std::execution::seq,
280+ response.begin (), response.end (),
281+ target.begin (), target.end ()
282+ ) != response.end ();
283+ };
284+ EXPECT_TRUE (containsActiveMQ (response_message));
250285}
251286
252287
0 commit comments