adamdebreceni commented on a change in pull request #818:
URL: https://github.com/apache/nifi-minifi-cpp/pull/818#discussion_r444744658



##########
File path: extensions/http-curl/tests/TestServer.h
##########
@@ -24,65 +24,87 @@
 #include "CivetServer.h"
 #include "civetweb.h"
 #include "HTTPUtils.h"
+#include "ServerAwareHandler.h"
 
-
-/* Server context handle */
-static std::string resp_str;
-
-void init_webserver() {
-  mg_init_library(0);
-}
-
-
-CivetServer * start_webserver(std::string &port, std::string &rooturi, 
CivetHandler *handler, struct mg_callbacks *callbacks, std::string &cert, 
std::string &ca_cert) {
-  const char *options[] = { "document_root", ".", "listening_ports", 
port.c_str(), "error_log_file",
-      "error.log", "ssl_certificate", ca_cert.c_str(), "ssl_protocol_version", 
"4", "ssl_cipher_list",
-      "ALL", "request_timeout_ms", "10000", "enable_auth_domain_check", "no", 
"ssl_verify_peer", "no", 0 };
-// ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS
-  std::vector<std::string> cpp_options;
-  for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
-    cpp_options.push_back(options[i]);
-  }
-
-  if (!mg_check_feature(2)) {
-      fprintf(stderr,
-              "Error: Embedded example built with SSL support, "
-              "but civetweb library build without.\n");
-      return 0;
+/**
+ * A wrapper around CivetServer which notifies Handlers before shutdown,
+ * so they wouldn't get stuck (if a handler returns after shutdown is
+ * initiated it might get stuck inside worker_thread_run > consume_socket)
+ */
+class TestServer{
+  struct CivetLibrary{
+    CivetLibrary() {
+      if (getCounter()++ == 0) {
+        mg_init_library(0);
+      }
+    }
+    ~CivetLibrary() {
+      if (--getCounter() == 0) {
+        mg_exit_library();
+      }
+    }
+   private:
+    std::atomic<int>& getCounter() {
+      static std::atomic<int> counter{0};
+      return counter;
+    }
+  };
+ public:
+  TestServer(std::string &port, std::string &rooturi, CivetHandler *handler, 
struct mg_callbacks *callbacks, std::string &cert, std::string &ca_cert) {
+    const char *options[] = { "document_root", ".", "listening_ports", 
port.c_str(), "error_log_file",
+                              "error.log", "ssl_certificate", ca_cert.c_str(), 
"ssl_protocol_version", "4", "ssl_cipher_list",
+                              "ALL", "request_timeout_ms", "10000", 
"enable_auth_domain_check", "no", "ssl_verify_peer", "no", 0 };
+    // ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS
+    std::vector<std::string> cpp_options;
+    for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
+      cpp_options.emplace_back(options[i]);
     }
 
+    if (!mg_check_feature(2)) {
+      throw std::runtime_error("Error: Embedded example built with SSL 
support, "
+                               "but civetweb library build without.\n");
+    }
 
-  //mg_init_library(MG_FEATURES_SSL);
 
-  CivetServer *server = new CivetServer(cpp_options, 
(CivetCallbacks*)callbacks);
+    //mg_init_library(MG_FEATURES_SSL);
 
-  server->addHandler(rooturi, handler);
+    server_ = utils::make_unique<CivetServer>(cpp_options, 
(CivetCallbacks*)callbacks);
 
-  return server;
+    addHandler(rooturi, handler);
+  }
 
-}
+  TestServer(std::string &port, std::string &rooturi, CivetHandler *handler) {
+    const char *options[] = {"document_root", ".", "listening_ports", 
port.c_str(), 0};

Review comment:
       done

##########
File path: extensions/http-curl/tests/TestServer.h
##########
@@ -24,65 +24,87 @@
 #include "CivetServer.h"
 #include "civetweb.h"
 #include "HTTPUtils.h"
+#include "ServerAwareHandler.h"
 
-
-/* Server context handle */
-static std::string resp_str;
-
-void init_webserver() {
-  mg_init_library(0);
-}
-
-
-CivetServer * start_webserver(std::string &port, std::string &rooturi, 
CivetHandler *handler, struct mg_callbacks *callbacks, std::string &cert, 
std::string &ca_cert) {
-  const char *options[] = { "document_root", ".", "listening_ports", 
port.c_str(), "error_log_file",
-      "error.log", "ssl_certificate", ca_cert.c_str(), "ssl_protocol_version", 
"4", "ssl_cipher_list",
-      "ALL", "request_timeout_ms", "10000", "enable_auth_domain_check", "no", 
"ssl_verify_peer", "no", 0 };
-// ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS
-  std::vector<std::string> cpp_options;
-  for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
-    cpp_options.push_back(options[i]);
-  }
-
-  if (!mg_check_feature(2)) {
-      fprintf(stderr,
-              "Error: Embedded example built with SSL support, "
-              "but civetweb library build without.\n");
-      return 0;
+/**
+ * A wrapper around CivetServer which notifies Handlers before shutdown,
+ * so they wouldn't get stuck (if a handler returns after shutdown is
+ * initiated it might get stuck inside worker_thread_run > consume_socket)
+ */
+class TestServer{
+  struct CivetLibrary{
+    CivetLibrary() {
+      if (getCounter()++ == 0) {
+        mg_init_library(0);
+      }
+    }
+    ~CivetLibrary() {
+      if (--getCounter() == 0) {
+        mg_exit_library();
+      }
+    }
+   private:
+    std::atomic<int>& getCounter() {
+      static std::atomic<int> counter{0};
+      return counter;
+    }
+  };
+ public:
+  TestServer(std::string &port, std::string &rooturi, CivetHandler *handler, 
struct mg_callbacks *callbacks, std::string &cert, std::string &ca_cert) {
+    const char *options[] = { "document_root", ".", "listening_ports", 
port.c_str(), "error_log_file",
+                              "error.log", "ssl_certificate", ca_cert.c_str(), 
"ssl_protocol_version", "4", "ssl_cipher_list",
+                              "ALL", "request_timeout_ms", "10000", 
"enable_auth_domain_check", "no", "ssl_verify_peer", "no", 0 };
+    // ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS
+    std::vector<std::string> cpp_options;
+    for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
+      cpp_options.emplace_back(options[i]);
     }
 
+    if (!mg_check_feature(2)) {
+      throw std::runtime_error("Error: Embedded example built with SSL 
support, "
+                               "but civetweb library build without.\n");
+    }
 
-  //mg_init_library(MG_FEATURES_SSL);
 
-  CivetServer *server = new CivetServer(cpp_options, 
(CivetCallbacks*)callbacks);
+    //mg_init_library(MG_FEATURES_SSL);
 
-  server->addHandler(rooturi, handler);
+    server_ = utils::make_unique<CivetServer>(cpp_options, 
(CivetCallbacks*)callbacks);
 
-  return server;
+    addHandler(rooturi, handler);
+  }
 
-}
+  TestServer(std::string &port, std::string &rooturi, CivetHandler *handler) {
+    const char *options[] = {"document_root", ".", "listening_ports", 
port.c_str(), 0};
 
-CivetServer * start_webserver(std::string &port, std::string &rooturi, 
CivetHandler *handler) {
-  const char *options[] = { "document_root", ".", "listening_ports", 
port.c_str(), 0 };
+    std::vector<std::string> cpp_options;
+    for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
+      cpp_options.emplace_back(options[i]);
+    }
+    server_ = utils::make_unique<CivetServer>(cpp_options);
 
-  std::vector<std::string> cpp_options;
-  for (size_t i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
-    cpp_options.push_back(options[i]);
+    addHandler(rooturi, handler);
   }
-  CivetServer *server = new CivetServer(cpp_options);
-
-  server->addHandler(rooturi, handler);
 
-  return server;
+  void addHandler(const std::string& uri, CivetHandler* handler) {
+    handlers_.emplace_back(handler);

Review comment:
       good advice, done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to