16 #include "tensorflow_serving/util/net_http/server/internal/evhttp_server.h"
22 #include <gtest/gtest.h>
23 #include "absl/memory/memory.h"
24 #include "absl/synchronization/notification.h"
25 #include "tensorflow_serving/util/net_http/client/test_client/internal/evhttp_connection.h"
26 #include "tensorflow_serving/util/net_http/internal/fixed_thread_pool.h"
27 #include "tensorflow_serving/util/net_http/server/public/httpserver.h"
28 #include "tensorflow_serving/util/net_http/server/public/httpserver_interface.h"
29 #include "tensorflow_serving/util/net_http/server/public/server_request_interface.h"
31 namespace tensorflow {
36 class MyExecutor final :
public EventExecutor {
38 explicit MyExecutor(
int num_threads) : thread_pool_(num_threads) {}
40 void Schedule(std::function<
void()> fn)
override {
41 thread_pool_.Schedule(fn);
45 FixedThreadPool thread_pool_;
48 class EvHTTPServerTest :
public ::testing::Test {
50 void SetUp()
override { InitServer(); }
52 void TearDown()
override {
53 if (!server->is_terminating()) {
55 server->WaitForTermination();
60 std::unique_ptr<HTTPServerInterface> server;
64 auto options = absl::make_unique<ServerOptions>();
66 options->SetExecutor(absl::make_unique<MyExecutor>(4));
68 server = CreateEvHTTPServer(std::move(options));
70 ASSERT_TRUE(server !=
nullptr);
75 TEST_F(EvHTTPServerTest, AcceptingTerminating) {
76 EXPECT_FALSE(server->is_accepting_requests());
77 server->StartAcceptingRequests();
78 EXPECT_TRUE(server->is_accepting_requests());
81 EXPECT_TRUE(server->is_terminating());
83 server->WaitForTermination();
87 TEST_F(EvHTTPServerTest, ExactPathMatching) {
88 auto handler = [](ServerRequestInterface* request) {
89 request->WriteResponseString(
"OK");
92 server->RegisterRequestHandler(
"/ok", std::move(handler),
93 RequestHandlerOptions());
94 server->StartAcceptingRequests();
97 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
98 ASSERT_TRUE(connection !=
nullptr);
100 TestClientRequest request = {
"/ok?a=foo",
"GET", {},
""};
101 TestClientResponse response = {};
103 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
104 EXPECT_EQ(response.status, HTTPStatusCode::OK);
105 EXPECT_EQ(response.body,
"OK");
108 request = {
"/ok/",
"GET", {},
""};
111 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
112 EXPECT_EQ(response.status, HTTPStatusCode::NOT_FOUND);
115 server->WaitForTermination();
119 TEST_F(EvHTTPServerTest, RequestHandlerOverwriting) {
120 auto handler1 = [](ServerRequestInterface* request) {
121 request->WriteResponseString(
"OK1");
124 auto handler2 = [](ServerRequestInterface* request) {
125 request->WriteResponseString(
"OK2");
129 server->RegisterRequestHandler(
"/ok", std::move(handler1),
130 RequestHandlerOptions());
131 server->RegisterRequestHandler(
"/ok", std::move(handler2),
132 RequestHandlerOptions());
133 server->StartAcceptingRequests();
136 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
137 ASSERT_TRUE(connection !=
nullptr);
139 TestClientRequest request = {
"/ok",
"GET", {},
""};
140 TestClientResponse response = {};
142 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
143 EXPECT_EQ(response.status, HTTPStatusCode::OK);
144 EXPECT_EQ(response.body,
"OK2");
147 server->WaitForTermination();
151 TEST_F(EvHTTPServerTest, SingleRequestDispather) {
152 auto handler = [](ServerRequestInterface* request) {
153 request->WriteResponseString(
"OK");
157 auto dispatcher = [&handler](ServerRequestInterface* request) {
161 server->RegisterRequestDispatcher(std::move(dispatcher),
162 RequestHandlerOptions());
163 server->StartAcceptingRequests();
166 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
167 ASSERT_TRUE(connection !=
nullptr);
169 TestClientRequest request = {
"/ok",
"GET", {},
""};
170 TestClientResponse response = {};
172 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
173 EXPECT_EQ(response.status, HTTPStatusCode::OK);
174 EXPECT_EQ(response.body,
"OK");
177 server->WaitForTermination();
181 TEST_F(EvHTTPServerTest, UriPrecedesOverRequestDispather) {
182 auto handler1 = [](ServerRequestInterface* request) {
183 request->WriteResponseString(
"OK1");
187 server->RegisterRequestHandler(
"/ok", std::move(handler1),
188 RequestHandlerOptions());
190 auto handler2 = [](ServerRequestInterface* request) {
191 request->WriteResponseString(
"OK2");
195 auto dispatcher = [&handler2](ServerRequestInterface* request) {
198 server->RegisterRequestDispatcher(std::move(dispatcher),
199 RequestHandlerOptions());
201 server->StartAcceptingRequests();
204 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
205 ASSERT_TRUE(connection !=
nullptr);
207 TestClientRequest request = {
"/ok",
"GET", {},
""};
208 TestClientResponse response = {};
210 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
211 EXPECT_EQ(response.status, HTTPStatusCode::OK);
212 EXPECT_EQ(response.body,
"OK1");
214 request = {
"/okxx",
"GET", {},
""};
217 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
218 EXPECT_EQ(response.status, HTTPStatusCode::OK);
219 EXPECT_EQ(response.body,
"OK2");
222 server->WaitForTermination();
226 TEST_F(EvHTTPServerTest, InOrderRequestDispather) {
227 auto dispatcher1 = [](ServerRequestInterface* request) {
228 return [](ServerRequestInterface* request) {
229 request->WriteResponseString(
"OK1");
234 auto dispatcher2 = [](ServerRequestInterface* request) {
235 return [](ServerRequestInterface* request) {
236 request->WriteResponseString(
"OK2");
241 server->RegisterRequestDispatcher(std::move(dispatcher1),
242 RequestHandlerOptions());
243 server->RegisterRequestDispatcher(std::move(dispatcher2),
244 RequestHandlerOptions());
246 server->StartAcceptingRequests();
249 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
250 ASSERT_TRUE(connection !=
nullptr);
252 TestClientRequest request = {
"/ok",
"GET", {},
""};
253 TestClientResponse response = {};
255 EXPECT_TRUE(connection->BlockingSendRequest(request, &response));
256 EXPECT_EQ(response.status, HTTPStatusCode::OK);
257 EXPECT_EQ(response.body,
"OK1");
260 server->WaitForTermination();
264 TEST_F(EvHTTPServerTest, RequestHandlerInteraction) {
265 absl::Notification handler1_start;
266 auto handler1 = [&handler1_start](ServerRequestInterface* request) {
267 handler1_start.WaitForNotification();
268 request->WriteResponseString(
"OK1");
272 server->RegisterRequestHandler(
"/ok", std::move(handler1),
273 RequestHandlerOptions());
275 server->StartAcceptingRequests();
278 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
279 ASSERT_TRUE(connection !=
nullptr);
280 connection->SetExecutor(absl::make_unique<MyExecutor>(4));
282 absl::Notification response_done;
283 TestClientRequest request = {
"/ok",
"GET", {},
""};
284 TestClientResponse response = {};
285 response.done = [&response_done]() { response_done.Notify(); };
287 EXPECT_TRUE(connection->SendRequest(request, &response));
289 response_done.WaitForNotificationWithTimeout(absl::Milliseconds(50)));
291 handler1_start.Notify();
292 response_done.WaitForNotification();
294 EXPECT_EQ(response.status, HTTPStatusCode::OK);
295 EXPECT_EQ(response.body,
"OK1");
297 connection->Terminate();
300 server->WaitForTermination();
304 TEST_F(EvHTTPServerTest, ActiveRequestCountInShutdown) {
305 absl::Notification handler_enter;
306 absl::Notification handler_start;
307 auto handler = [&handler_enter,
308 &handler_start](ServerRequestInterface* request) {
309 handler_enter.Notify();
310 handler_start.WaitForNotification();
311 request->WriteResponseString(
"OK1");
315 server->RegisterRequestHandler(
"/ok", std::move(handler),
316 RequestHandlerOptions());
318 server->StartAcceptingRequests();
321 TestEvHTTPConnection::Connect(
"localhost", server->listen_port());
322 ASSERT_TRUE(connection !=
nullptr);
323 connection->SetExecutor(absl::make_unique<MyExecutor>(4));
325 TestClientRequest request = {
"/ok",
"GET", {},
""};
326 TestClientResponse response = {};
328 EXPECT_TRUE(connection->SendRequest(request, &response));
329 handler_enter.WaitForNotification();
332 EXPECT_FALSE(server->WaitForTerminationWithTimeout(absl::Milliseconds(50)));
334 handler_start.Notify();
335 EXPECT_TRUE(server->WaitForTerminationWithTimeout(absl::Milliseconds(5000)));
337 connection->Terminate();