Prechádzať zdrojové kódy

Check and fix effects of SO_EXCLUSIVEADDRUSE on Windows unit test

bel 10 rokov pred
rodič
commit
86eb1ed0e5
2 zmenil súbory, kde vykonal 24 pridanie a 13 odobranie
  1. 5 5
      src/civetweb.c
  2. 19 8
      test/unit_test.c

+ 5 - 5
src/civetweb.c

@@ -8260,11 +8260,11 @@ static int set_ports_option(struct mg_context *ctx)
 			            * if someone already has the socket -- DTL */
 			           setsockopt(so.sock,
 			                      SOL_SOCKET,
-			                      /* TODO (high): unit test only works with
-			                       * SO_REUSEADDR, but not with
-			                       * SO_EXCLUSIVEADDRUSE. Maybe connection
-			                       * open/close is too fast in unit_test.c
-			                       * ==> needs to be checked
+			                      /* NOTE: If SO_EXCLUSIVEADDRUSE is used,
+			                       * Windows might need a few seconds before
+			                       * the same port can be used again in the
+			                       * same process, so a short Sleep may be
+			                       * required between mg_stop and mg_start.
 			                       */
 			                      SO_EXCLUSIVEADDRUSE,
 			                      (SOCK_OPT_TYPE)&on,

+ 19 - 8
test/unit_test.c

@@ -403,6 +403,17 @@ static char *read_conn(struct mg_connection *conn, int *size) {
     return data;
 }
 
+extern unsigned long mg_memory_debug_blockCount;
+extern unsigned long mg_memory_debug_totalMemUsed;
+
+static void ut_mg_stop(struct mg_context *ctx) {
+    /* mg_stop for unit_test */
+    mg_stop(ctx);
+    ASSERT(mg_memory_debug_blockCount == 0);
+    ASSERT(mg_memory_debug_totalMemUsed == 0);
+    mg_sleep(31000); /* This is required to ensure the operating system already allows to use the port again */
+}
+
 static void test_mg_download(int use_ssl) {
 
     const char *test_data = "123456789A123456789B";
@@ -571,7 +582,7 @@ static void test_mg_download(int use_ssl) {
     mg_close_connection(conn);
 
     /* Stop the test server */
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 }
 
 static int websocket_data_handler(const struct mg_connection *conn, int flags, char *data, size_t data_len, void *cbdata)
@@ -628,7 +639,7 @@ static void test_mg_websocket_client_connect(int use_ssl) {
                              "/", "http://websocket.org", websocket_data_handler, NULL, NULL);
     ASSERT(conn != NULL);
 
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 }
 
 static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
@@ -722,7 +733,7 @@ static void test_mg_upload(void) {
     mg_close_connection(conn);
 #endif
 
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 }
 
 static void test_base64_encode(void) {
@@ -889,7 +900,7 @@ static void test_request_replies(void) {
             tests[i].request)) != NULL);
         mg_close_connection(conn);
     }
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 
 #ifndef NO_SSL
     ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
@@ -898,7 +909,7 @@ static void test_request_replies(void) {
             tests[i].request)) != NULL);
         mg_close_connection(conn);
     }
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 #endif
 }
 
@@ -947,7 +958,7 @@ static void test_request_handlers(void) {
     mg_sleep(1000);
     mg_close_connection(conn);
 
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 
 }
 
@@ -985,7 +996,7 @@ static void test_api_calls(void) {
     ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
         ebuf, sizeof(ebuf), "%s", request)) != NULL);
     mg_close_connection(conn);
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 }
 
 static void test_url_decode(void) {
@@ -1164,7 +1175,7 @@ int __cdecl main(void) {
     ctx = mg_start(NULL, NULL, OPTIONS);
     REQUIRE(ctx != NULL);
     mg_sleep(1000);
-    mg_stop(ctx);
+    ut_mg_stop(ctx);
 
     /* create test data */
     fetch_data = (char *) mg_malloc(fetch_data_size);