Browse Source

More diagnostics for unit test errors in mg_start

bel2125 5 năm trước cách đây
mục cha
commit
7e258fd4eb
1 tập tin đã thay đổi với 51 bổ sung38 xóa
  1. 51 38
      unittest/public_server.c

+ 51 - 38
unittest/public_server.c

@@ -232,6 +232,7 @@ START_TEST(test_threading)
 }
 END_TEST
 
+static const char *lastMessage = NULL;
 
 static int
 log_msg_func(const struct mg_connection *conn, const char *message)
@@ -250,6 +251,7 @@ log_msg_func(const struct mg_connection *conn, const char *message)
 
 	printf("LOG_MSG_FUNC: %s\n", message);
 	mark_point();
+	lastMessage = message;
 
 	return 1; /* Return 1 means "already handled" */
 }
@@ -262,6 +264,7 @@ test_log_message(const struct mg_connection *conn, const char *message)
 
 	printf("LOG_MESSAGE: %s\n", message);
 	mark_point();
+	lastMessage = message;
 
 	return 0; /* Return 0 means "not yet handled" */
 }
@@ -270,7 +273,8 @@ test_log_message(const struct mg_connection *conn, const char *message)
 static struct mg_context *
 test_mg_start(const struct mg_callbacks *callbacks,
               void *user_data,
-              const char **configuration_options)
+              const char **configuration_options,
+			  unsigned line)
 {
 	struct mg_context *ctx;
 	struct mg_callbacks cb;
@@ -291,9 +295,15 @@ test_mg_start(const struct mg_callbacks *callbacks,
 	ctx = mg_start(&cb, user_data, configuration_options);
 	mark_point();
 	if (ctx) {
-		/* Give the server some time to start in the test VM */
-		/* Don't need to do this if mg_start failed */
+		/* Give the server some time to start in the test VM. */
+		/* Don't need to do this if mg_start failed. */
 		test_sleep(SLEEP_AFTER_MG_START);
+	} else if (line>0) {
+		/* mg_start is not supposed to fail anywhere, except for
+		 * special tests (for them, line is 0). */
+		ck_abort_msg("mg_start failed in line %u, message %s",
+		             line,
+					 (lastMessage ? lastMessage : "<NULL>"));
 	}
 	mark_point();
 
@@ -302,10 +312,11 @@ test_mg_start(const struct mg_callbacks *callbacks,
 
 
 static void
-test_mg_stop(struct mg_context *ctx)
+test_mg_stop(struct mg_context *ctx, unsigned line)
 {
+	(void)line;
 #ifdef __MACH__
-	/* For unknown reasons, there are sporadic hands
+	/* For unknown reasons, there are sporadic hangs
 	 * for OSX if mark_point is called here */
 	test_sleep(SLEEP_BEFORE_MG_STOP);
 	mg_stop(ctx);
@@ -376,7 +387,7 @@ test_mg_start_stop_http_server_impl(int ipv6, int bound)
 
 	callbacks.log_message = log_msg_func;
 
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
 
 	ck_assert_str_eq(errmsg, "");
 	ck_assert(ctx != NULL);
@@ -532,7 +543,7 @@ test_mg_start_stop_http_server_impl(int ipv6, int bound)
 	test_sleep(1);
 
 	/* End test */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 	mark_point();
 }
 
@@ -611,7 +622,7 @@ START_TEST(test_mg_start_stop_https_server)
 
 	callbacks.log_message = log_msg_func;
 
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
 
 	ck_assert_str_eq(errmsg, "");
 	ck_assert(ctx != NULL);
@@ -683,7 +694,7 @@ START_TEST(test_mg_start_stop_https_server)
 
 	test_sleep(1);
 
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 	mark_point();
 #endif
 }
@@ -748,7 +759,7 @@ START_TEST(test_mg_server_and_client_tls)
 
 	callbacks.log_message = log_msg_func;
 
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
 
 	ck_assert_str_eq(errmsg, "");
 	ck_assert(ctx != NULL);
@@ -814,7 +825,7 @@ START_TEST(test_mg_server_and_client_tls)
 
 	test_sleep(1);
 
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 #endif
 	mark_point();
 }
@@ -1254,7 +1265,7 @@ START_TEST(test_request_handlers)
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
 
-	ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
+	ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
 
 	ck_assert(ctx != NULL);
 	g_ctx = ctx;
@@ -1488,6 +1499,7 @@ START_TEST(test_request_handlers)
 
 #ifdef _WIN32
 	f = fopen("test.cgi", "wb");
+	ck_assert(f != NULL);
 	cgi_script_content = "#!test.cgi.cmd\r\n";
 	fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
 	fclose(f);
@@ -1502,6 +1514,7 @@ START_TEST(test_request_handlers)
 	fclose(f);
 #else
 	f = fopen("test.cgi", "w");
+	ck_assert(f != NULL);
 	cgi_script_content = "#!/bin/sh\n\n"
 	                     "printf \"Connection: close\\r\\n\"\n"
 	                     "printf \"Content-Type: text/plain\\r\\n\"\n"
@@ -2186,7 +2199,7 @@ START_TEST(test_request_handlers)
 
 	/* Close the server */
 	g_ctx = NULL;
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 	mark_point();
 
 #ifdef USE_WEBSOCKET
@@ -2595,7 +2608,7 @@ START_TEST(test_handle_form)
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
 
-	ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
+	ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
 
 	ck_assert(ctx != NULL);
 	g_ctx = ctx;
@@ -3119,7 +3132,7 @@ START_TEST(test_handle_form)
 
 	/* Close the server */
 	g_ctx = NULL;
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 	mark_point();
 }
 END_TEST
@@ -3166,7 +3179,7 @@ START_TEST(test_http_auth)
 	mark_point();
 
 	/* Start with default options */
-	ctx = test_mg_start(NULL, NULL, OPTIONS);
+	ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
 
 	ck_assert(ctx != NULL);
 	domain = mg_get_option(ctx, "authentication_domain");
@@ -3456,7 +3469,7 @@ START_TEST(test_http_auth)
 
 
 	/* Stop the server and clean up */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 	(void)remove(test_file);
 	(void)remove(passwd_file);
 	(void)remove("put_delete_auth_file.csv");
@@ -3493,7 +3506,7 @@ START_TEST(test_keep_alive)
 
 	mark_point();
 
-	ctx = test_mg_start(NULL, NULL, OPTIONS);
+	ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
 
 	ck_assert(ctx != NULL);
 
@@ -3540,7 +3553,7 @@ START_TEST(test_keep_alive)
 	 * (will only work if NO_FILES is not set). */
 
 	/* Stop the server and clean up */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	mark_point();
 }
@@ -3587,7 +3600,7 @@ START_TEST(test_error_handling)
 
 	/* test with unknown option */
 	memset(errmsg, 0, sizeof(errmsg));
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
 
 	/* Details of errmsg may vary, but it may not be empty */
 	ck_assert_str_ne(errmsg, "");
@@ -3603,7 +3616,7 @@ START_TEST(test_error_handling)
 
 	/* Test with bad num_thread option */
 	memset(errmsg, 0, sizeof(errmsg));
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
 
 	/* Details of errmsg may vary, but it may not be empty */
 	ck_assert_str_ne(errmsg, "");
@@ -3619,7 +3632,7 @@ START_TEST(test_error_handling)
 
 	/* Test with bad num_thread option */
 	memset(errmsg, 0, sizeof(errmsg));
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
 
 	/* Details of errmsg may vary, but it may not be empty */
 	ck_assert_str_ne(errmsg, "");
@@ -3642,7 +3655,7 @@ START_TEST(test_error_handling)
 	/* This time start the server with a valid configuration */
 	sprintf(bad_thread_num, "%i", 1);
 	memset(errmsg, 0, sizeof(errmsg));
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
 
 	ck_assert_str_eq(errmsg, "");
 	ck_assert(ctx != NULL);
@@ -3807,7 +3820,7 @@ START_TEST(test_error_handling)
 
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 
 	/* HTTP 1.1 GET request - must not work, since server is already stopped  */
@@ -3858,7 +3871,7 @@ START_TEST(test_error_log_file)
 #endif
 	OPTIONS[opt_cnt] = NULL;
 
-	ctx = test_mg_start(NULL, 0, OPTIONS);
+	ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
 	ck_assert(ctx != NULL);
 
 	/* Remove log files (they may exist from previous incomplete runs of
@@ -3894,7 +3907,7 @@ START_TEST(test_error_log_file)
 	mg_close_connection(client);
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 
 	/* Check access.log */
@@ -3930,7 +3943,7 @@ START_TEST(test_error_log_file)
 	ck_assert_str_eq(OPTIONS[0], "listening_ports");
 	OPTIONS[1] = "bad !"; /* no r or s in string */
 
-	ctx = test_mg_start(NULL, 0, OPTIONS);
+	ctx = test_mg_start(NULL, 0, OPTIONS, 0);
 	ck_assert_msg(
 	    ctx == NULL,
 	    "Should not be able to start server with bad port configuration");
@@ -4066,7 +4079,7 @@ START_TEST(test_throttle)
 	callbacks.begin_request = test_throttle_begin_request;
 	callbacks.end_request = test_throttle_end_request;
 
-	ctx = test_mg_start(&callbacks, 0, OPTIONS);
+	ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
 	ck_assert(ctx != NULL);
 
 	/* connect client */
@@ -4122,7 +4135,7 @@ START_TEST(test_throttle)
 	mg_close_connection(client);
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	mark_point();
 }
@@ -4236,7 +4249,7 @@ START_TEST(test_large_file)
 	callbacks.begin_request = test_large_file_begin_request;
 	callbacks.log_message = log_msg_func;
 
-	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
+	ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
 	ck_assert_str_eq(errmsg, "");
 	ck_assert(ctx != NULL);
 
@@ -4306,7 +4319,7 @@ START_TEST(test_large_file)
 #endif
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	mark_point();
 }
@@ -4480,7 +4493,7 @@ START_TEST(test_mg_store_body)
 	test_sleep(5);
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	/* Un-initialize the library */
 	mg_exit_library();
@@ -4558,7 +4571,7 @@ START_TEST(test_file_in_memory)
 	memset(&callbacks, 0, sizeof(callbacks));
 	callbacks.open_file = test_file_in_memory_open_file;
 
-	ctx = test_mg_start(&callbacks, 0, OPTIONS);
+	ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
 	ck_assert(ctx != NULL);
 
 	/* connect client */
@@ -4608,7 +4621,7 @@ START_TEST(test_file_in_memory)
 	mg_close_connection(client);
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	/* Free test data */
 	free(file_in_mem_data);
@@ -4810,7 +4823,7 @@ START_TEST(test_minimal_http_server_callback)
 	mg_init_library(0);
 
 	/* Start the server */
-	ctx = test_mg_start(NULL, 0, NULL);
+	ctx = test_mg_start(NULL, 0, NULL, __LINE__);
 	ck_assert(ctx != NULL);
 
 	/* Add some handler */
@@ -4834,7 +4847,7 @@ START_TEST(test_minimal_http_server_callback)
 
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	/* Un-initialize the library */
 	mg_exit_library();
@@ -4903,7 +4916,7 @@ START_TEST(test_minimal_https_server_callback)
 
 
 	/* Start the server */
-	ctx = test_mg_start(NULL, 0, OPTIONS);
+	ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
 	ck_assert(ctx != NULL);
 
 	/* Add some handler */
@@ -4927,7 +4940,7 @@ START_TEST(test_minimal_https_server_callback)
 
 
 	/* Stop the server */
-	test_mg_stop(ctx);
+	test_mg_stop(ctx, __LINE__);
 
 	/* Un-initialize the library */
 	mg_exit_library();