Эх сурвалжийг харах

T20M5021 #comment 对进程监控的案例修复

gifur 5 жил өмнө
parent
commit
190f2cced0

+ 1 - 1
CMakeSettings.json

@@ -32,7 +32,7 @@
       "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
       "cmakeCommandArgs": "-D BUILD_TESTING=ON",
       "buildCommandArgs": "",
-      "ctestCommandArgs": "",
+      "ctestCommandArgs": "--output-on-failure",
       "inheritEnvironments": [ "linux_x64" ],
       "remoteMachineName": "-530210665;10.42.0.1 (username=root, port=1022, authentication=Password)",
       "remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/src",

+ 1 - 1
libtoolkit/process_monitor.c

@@ -259,7 +259,7 @@ TOOLKIT_API int process_monitor_destroy(process_monitor_t *monitor)
 	}
 	DeleteCriticalSection(&monitor->lock);
 	free(monitor);
-
+	printf("process_monitor_destroy\n");
 	return 0;
 }
 

+ 4 - 4
libtoolkit/test/CMakeLists.txt

@@ -63,14 +63,14 @@ foreach(test ${${MODULE_PREFIX}_TESTS})
 	# 会将 测试的执行程序生成在指定的文件夹,并且带上编译的配置条件比如 Debug
 	set_target_properties(${test_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
 	message(STATUS "add test case: ${test_name}...")
+	if(${test_name} STREQUAL "test_file" OR ${test_name} STREQUAL "test_log")
 	add_test(NAME ${test_name} COMMAND ${TESTING_OUTPUT_DIRECTORY}/${test_name}  ${TEST_AREA})
+	else()
+	add_test(NAME ${test_name} COMMAND ${TESTING_OUTPUT_DIRECTORY}/${test_name})
+	endif()
 	set_tests_properties(${teset_name} PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED;ERROR;Failed;error")
 endforeach()
 
-
-if(NOT MSVC)
-	target_link_libraries(test_process winpr)
-endif()
 add_executable(single_proc single_proc.cpp)
 set_property(TARGET single_proc PROPERTY FOLDER "test/libtoolkit")
 set_target_properties(single_proc PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")

+ 36 - 37
libtoolkit/test/test_file.cpp

@@ -10,11 +10,14 @@ TEST(FileUtilTest, TestGetSubFile)
 	std::string test_dir("");
 	if (args.size() == 1) {
 		auto dir = args[0].find_last_of('/');
+		if (dir == std::string::npos) {
+			dir = args[0].find_last_of('\\');
+		}
 		if (dir != std::string::npos) {
 			test_dir = args[0].substr(0, dir + 1);
 			test_dir += "test_libtoolkit_area";
 		}
-		ASSERT_TRUE(!test_dir.empty());
+		ASSERT_TRUE(!test_dir.empty()) << test_dir;
 	}
 	else {
 		test_dir = args[1];
@@ -55,20 +58,44 @@ TEST(FileUtilTest, TestGetSubFile)
 	ASSERT_TRUE(array_empty(files));
 }
 
-#if 0
 /*
-D:\GitReposity\Framework\out\build\x86-Debug\bin\Debug\test_libtoolkit_area
+D:/GitReposity/Framework/out/build/x86-Debug/bin/Debug/test_libtoolkit_area
  */
-TEST(FileUtilTest, DealWithBackSlash)
+TEST(FileUtilTest, DealWithSlash)
 {
 	const auto args = ::testing::internal::GetArgvs();
 	EXPECT_FALSE(args.empty());
-	const auto test_dir = args[1];
+	std::string test_dir("");
+	if (args.size() == 1) {
+		auto dir = args[0].find_last_of('/');
+		if (dir == std::string::npos) {
+			dir = args[0].find_last_of('\\');
+		}
+		if(dir != std::string::npos)
+		{
+			test_dir = args[0].substr(0, dir + 1);
+			test_dir += "test_libtoolkit_area";
+		}
+		EXPECT_TRUE(!test_dir.empty());
+	} else {
+		test_dir = args[1];
+	}
 	std::cout << "test dir: " << test_dir << std::endl;
 	ASSERT_TRUE(ExistsDirA(test_dir.c_str()));
+
+#ifdef _WIN32
 	ASSERT_TRUE(ExistsFileA((test_dir + "\\test_file3.txt").c_str()));
 	ASSERT_TRUE(ExistsFileA((test_dir + "\\test_dir_3\\test_dir_3_file_3.txt").c_str()));
 
+	/*create dir*/
+	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_3").c_str()));
+	ASSERT_TRUE(CreateDirRecursiveA((test_dir + "\\new_dir_3\\new_dir_3_1").c_str()));
+	EXPECT_TRUE(ExistsDirA((test_dir + "\\new_dir_3\\new_dir_3_1").c_str()));
+	/*remove dir*/
+	ASSERT_TRUE(RemoveDirRecursiveA((test_dir + "\\new_dir_3").c_str()));
+	ASSERT_FALSE(ExistsDirA((test_dir + "\\new_dir_3\\new_dir_3_1").c_str()));
+	ASSERT_FALSE(ExistsDirA((test_dir + "\\new_dir_3").c_str()));
+
 	/*copy dir*/
 	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_1").c_str()));
 	ASSERT_TRUE(CopyDirA((test_dir + "\\test_dir_3").c_str(), (test_dir + "\\new_dir_1").c_str()));
@@ -96,14 +123,7 @@ TEST(FileUtilTest, DealWithBackSlash)
 	EXPECT_TRUE(CreateDirA((test_dir + "\\new_dir_2\\new_dir_2_1").c_str(), TRUE));
 	EXPECT_TRUE(ExistsDirA((test_dir + "\\new_dir_2\\new_dir_2_1").c_str()));
 	/*remove dir*/
-	ASSERT_TRUE(RemoveDirRecursiveA((test_dir + "\\new_dir_2").c_str()));
-	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_2\\new_dir_2_1").c_str()));
-	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_2").c_str()));
-	/*create dir*/
-	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_3").c_str()));
-	EXPECT_TRUE(CreateDirRecursiveA((test_dir + "\\new_dir_3\\new_dir_3_1").c_str()));
-	EXPECT_TRUE(ExistsDirA((test_dir + "\\new_dir_3\\new_dir_3_1").c_str()));
-	RemoveDirRecursiveA((test_dir + "\\new_dir_3").c_str());
+	RemoveDirRecursiveA((test_dir + "\\new_dir_2").c_str());
 	/*create parent dir*/
 	EXPECT_FALSE(ExistsDirA((test_dir + "\\new_dir_4").c_str()));
 	EXPECT_FALSE(CreateParentDirA((test_dir + "\\new_dir_4\\new_dir_4_1\\new_dir_4_1_1").c_str(), FALSE));
@@ -120,30 +140,8 @@ TEST(FileUtilTest, DealWithBackSlash)
 	/*rollback new_dir_2*/
 	CopyDirA((test_dir + "\\new_bk_2").c_str(), (test_dir + "\\test_dir_2").c_str());
 	RemoveDirRecursiveA((test_dir + "\\new_bk_2").c_str());
-}
 
 #else
-/*
-D:/GitReposity/Framework/out/build/x86-Debug/bin/Debug/test_libtoolkit_area
- */
-TEST(FileUtilTest, DealWithSlash)
-{
-	const auto args = ::testing::internal::GetArgvs();
-	EXPECT_FALSE(args.empty());
-	std::string test_dir("");
-	if (args.size() == 1) {
-		auto dir = args[0].find_last_of('/');
-		if(dir != std::string::npos)
-		{
-			test_dir = args[0].substr(0, dir + 1);
-			test_dir += "test_libtoolkit_area";
-		}
-		EXPECT_TRUE(!test_dir.empty());
-	} else {
-		test_dir = args[1];
-	}
-	std::cout << "test dir: " << test_dir << std::endl;
-	ASSERT_TRUE(ExistsDirA(test_dir.c_str()));
 	ASSERT_TRUE(ExistsFileA((test_dir + "/test_file3.txt").c_str()));
 	ASSERT_TRUE(ExistsFileA((test_dir + "/test_dir_3/test_dir_3_file_3.txt").c_str()));
 
@@ -200,6 +198,7 @@ TEST(FileUtilTest, DealWithSlash)
 	/*rollback new_dir_2*/
 	CopyDirA((test_dir + "/new_bk_2").c_str(), (test_dir + "/test_dir_2").c_str());
 	RemoveDirRecursiveA((test_dir + "/new_bk_2").c_str());
-}
 
-#endif
+#endif // _WIN32
+
+}

+ 47 - 12
libtoolkit/test/test_process.cpp

@@ -1,17 +1,26 @@
 #include <iostream>
 #include<thread>
+#include <cstring>
 #include <gtest/gtest.h>
 #include "process_monitor.h"
 #include "list.h"
+#ifndef _WIN32
 #include <winpr/thread.h>
 #include <winpr/file.h>
 #include <winpr/error.h>
 #include <winpr/library.h>
+#endif
 
 
 static const int process_create_max_count = 10;
 static const int process_parent_wait_max_time = 15;
 static int process_exit_count = 0;
+static char module_name[256] = { 0 };
+#ifdef _WIN32
+static char process_name[] = "single_proc.exe";
+#else
+static char process_name[] = "single_proc";
+#endif // _WIN32
 
 static int on_detect_process_end(process_monitor_t* monitor, HANDLE hproc, void* user_data)
 {
@@ -25,18 +34,8 @@ static HANDLE create_process_test(void)
 	STARTUPINFOA si = { sizeof(STARTUPINFOA) };
 	PROCESS_INFORMATION pi;
 	HANDLE process = NULL;
-#ifdef _WIN32
-	auto ret = CreateProcessA("single_proc.exe", "single_proc.exe",
+	auto ret = CreateProcessA(module_name, NULL,
 		NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
-#else
-	char module_name[256] = { 0 };
-	DWORD len = GetModuleFileNameA(NULL, module_name, sizeof(module_name));
-	strrchr(module_name, '/')[1] = '\0';
-	strcat(module_name, "single_proc");
-	std::cout << "single proc full path: " << module_name << std::endl;
-	auto ret = CreateProcessA("single_proc", module_name, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
-#endif // _WIN32
-
 	if (ret) {
 		process = pi.hProcess;
 		if (process == INVALID_HANDLE_VALUE)
@@ -51,18 +50,54 @@ static HANDLE create_process_test(void)
 
 TEST(TestProcessMonitor, TestProcess)
 {
+	char* pt = NULL;
+	const auto args = ::testing::internal::GetArgvs();
+	if (args.size() > 1) {
+		std::string test_dir("");
+		auto dir = args[1].find_last_of('/');
+		if (dir == std::string::npos)
+			dir = args[1].find_last_of('\\');
+		if (dir != std::string::npos) {
+			test_dir = args[1].substr(0, dir + 1);
+		}
+		else {
+			test_dir = args[1];
+		}
+		strcpy(module_name, test_dir.c_str());
+	}
+	else {
+		DWORD len = GetModuleFileNameA(NULL, module_name, sizeof(module_name));
+		if ((pt = strrchr(module_name, '/')) == NULL) {
+			pt = strrchr(module_name, '\\');
+		}
+		pt[1] = '\0';
+	}
+#ifdef _WIN32
+	strcat(module_name, "single_proc.exe");
+#else
+	strcat(module_name, "single_proc");
+#endif // _WIN32
+	std::cout << "single proc full path: " << module_name << std::endl;
+
 	process_monitor_t* pm = NULL;
 	ASSERT_TRUE(process_monitor_create(&pm) == 0);
 	process_monitor_set_cb(pm, on_detect_process_end, NULL);
 	ASSERT_TRUE(process_monitor_start(pm) == 0);
+	/**/
 	for (int i = 0; i < process_create_max_count; ++i) {
 		std::cout << "create test process[" << i << "]" << std::endl;
 		HANDLE cur = create_process_test();
 		EXPECT_TRUE(cur != NULL);
 		EXPECT_TRUE(0 == process_monitor_add(pm, cur));
 	}
-	std::this_thread::sleep_for(std::chrono::seconds(process_parent_wait_max_time + 2));
+	const int elapse_sec = 1;
+	const int count = process_parent_wait_max_time / elapse_sec + 1;
+	int n = 0;
+	while (process_exit_count < process_create_max_count && n++ < count) {
+		std::this_thread::sleep_for(std::chrono::seconds(elapse_sec));
+	}
 	ASSERT_TRUE(process_create_max_count == process_exit_count);
+	
 	ASSERT_TRUE(process_monitor_stop(pm) == 0);
 	process_monitor_destroy(pm);
 }

+ 2 - 0
winpr/CMakeLists.txt

@@ -25,6 +25,8 @@ if(RVC_VERSION)
 	set(RVC_BUILD 1)
 endif()
 
+#set(BUILD_TESTING OFF)
+
 # Include cmake modules
 include(CheckIncludeFiles)
 include(CheckLibraryExists)