From c24ddad10a8adbbc4c0ece343853a2233766bd86 Mon Sep 17 00:00:00 2001 From: Klemens Morgenstern Date: Tue, 12 Sep 2023 20:49:15 +0800 Subject: [PATCH] moved interrupt & request_exit into target. --- test/v2/process.cpp | 111 ++++++++++++++++++-------------------------- test/v2/target.cpp | 55 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 66 deletions(-) diff --git a/test/v2/process.cpp b/test/v2/process.cpp index 5c013aba3..77af42757 100644 --- a/test/v2/process.cpp +++ b/test/v2/process.cpp @@ -26,6 +26,11 @@ #include #include +#if defined(BOOST_PROCESS_V2_WINDOWS) +#include +#include +#endif + #include #include #include @@ -43,35 +48,6 @@ namespace bpv = boost::process::v2; namespace asio = boost::asio; -#if defined(BOOST_PROCESS_V2_WINDOWS) -bpv::filesystem::path shell() -{ - return bpv::environment::find_executable("cmd"); -} - -bpv::filesystem::path closable() -{ - return bpv::environment::find_executable("notepad"); -} - -bpv::filesystem::path interruptable() -{ - return bpv::environment::find_executable("cmd"); -} -#else -bpv::filesystem::path shell() -{ - return bpv::environment::find_executable("sh"); -} -bpv::filesystem::path closable() -{ - return bpv::environment::find_executable("tee"); -} -bpv::filesystem::path interruptable() -{ - return bpv::environment::find_executable("tee"); -} -#endif BOOST_AUTO_TEST_SUITE(with_target); @@ -147,55 +123,54 @@ BOOST_AUTO_TEST_CASE(exit_code_async) BOOST_AUTO_TEST_CASE(terminate) { asio::io_context ctx; + using boost::unit_test::framework::master_test_suite; + const auto pth = master_test_suite().argv[1]; - auto sh = shell(); - - BOOST_CHECK_MESSAGE(!sh.empty(), sh); - bpv::process proc(ctx, sh, {}); + + bpv::process proc(ctx, pth, {"sleep", "10"}); proc.suspend(); proc.resume(); proc.terminate(); proc.wait(); + BOOST_CHECK_NE(proc.exit_code(), 0); } BOOST_AUTO_TEST_CASE(request_exit) { asio::io_context ctx; - auto sh = closable(); - BOOST_CHECK_MESSAGE(!sh.empty(), sh); + using boost::unit_test::framework::master_test_suite; + const auto pth = master_test_suite().argv[1]; - asio::readable_pipe rp{ctx}; - asio::writable_pipe wp{ctx}; - asio::connect_pipe(rp, wp); - bpv::process proc(ctx, sh, {}, bpv::process_stdio{rp} -#if defined(ASIO_WINDOWS) - , asio::windows::show_window_minimized_not_active + bpv::process proc(ctx, pth, {"sigterm"} +#if defined(BOOST_PROCESS_V2_WINDOWS) + , bpv::windows::show_window_minimized_not_active #endif ); BOOST_CHECK(proc.running()); std::this_thread::sleep_for(std::chrono::milliseconds(250)); proc.request_exit(); proc.wait(); + BOOST_CHECK_EQUAL(proc.exit_code(), 0); } BOOST_AUTO_TEST_CASE(interrupt) { asio::io_context ctx; + using boost::unit_test::framework::master_test_suite; + const auto pth = master_test_suite().argv[1]; - auto sh = interruptable(); - BOOST_CHECK_MESSAGE(!sh.empty(), sh); - - asio::writable_pipe wp{ctx}; - bpv::process proc(ctx, sh, {}, bpv::process_stdio{wp} -#if defined(ASIO_WINDOWS) - , asio::windows::create_new_process_group + bpv::process proc(ctx, pth, {"sigint"} +#if defined(BOOST_PROCESS_V2_WINDOWS) + , bpv::windows::create_new_process_group #endif ); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); proc.interrupt(); proc.wait(); + BOOST_CHECK_EQUAL(proc.exit_code(), 0); } void trim_end(std::string & str) @@ -593,15 +568,12 @@ BOOST_AUTO_TEST_CASE(bind_launcher) BOOST_AUTO_TEST_CASE(async_interrupt) { asio::io_context ctx; + using boost::unit_test::framework::master_test_suite; + const auto pth = bpv::filesystem::absolute(master_test_suite().argv[1]); - auto sh = interruptable(); - BOOST_CHECK_MESSAGE(!sh.empty(), sh); - BOOST_CHECK_MESSAGE(!sh.empty(), sh); - - asio::writable_pipe wp{ctx}; - bpv::process proc(ctx, sh, {}, bpv::process_stdio{wp} -#if defined(ASIO_WINDOWS) - , asio::windows::create_new_process_group + bpv::process proc(ctx, pth, {"sigint"} +#if defined(BOOST_PROCESS_V2_WINDOWS) + , bpv::windows::create_new_process_group #endif ); @@ -609,7 +581,13 @@ BOOST_AUTO_TEST_CASE(async_interrupt) asio::cancellation_signal sig; bpv::async_execute(std::move(proc), - asio::bind_cancellation_slot(sig.slot(), asio::detached)); + asio::bind_cancellation_slot( + sig.slot(), + [](boost::system::error_code ec, int res) + { + BOOST_CHECK(!ec); + BOOST_CHECK_EQUAL(res, 0); + })); tim.async_wait([&](bpv::error_code ec) { sig.emit(asio::cancellation_type::total); }); ctx.run(); @@ -618,15 +596,10 @@ BOOST_AUTO_TEST_CASE(async_interrupt) BOOST_AUTO_TEST_CASE(async_request_exit) { asio::io_context ctx; + using boost::unit_test::framework::master_test_suite; + const auto pth = bpv::filesystem::absolute(master_test_suite().argv[1]); - auto sh = closable(); - BOOST_CHECK_MESSAGE(!sh.empty(), sh); - - asio::readable_pipe rp{ctx}; - asio::writable_pipe wp{ctx}; - asio::connect_pipe(rp, wp); - - bpv::process proc(ctx, sh, {}, bpv::process_stdio{rp} + bpv::process proc(ctx, pth, {"sigterm"} #if defined(ASIO_WINDOWS) , asio::windows::show_window_minimized_not_active #endif @@ -636,7 +609,13 @@ BOOST_AUTO_TEST_CASE(async_request_exit) asio::cancellation_signal sig; bpv::async_execute(std::move(proc), - asio::bind_cancellation_slot(sig.slot(), asio::detached)); + asio::bind_cancellation_slot( + sig.slot(), + [](boost::system::error_code ec, int res) + { + BOOST_CHECK(!ec); + BOOST_CHECK_EQUAL(res, 0); + })); tim.async_wait([&](bpv::error_code ec) { sig.emit(asio::cancellation_type::partial); }); ctx.run(); diff --git a/test/v2/target.cpp b/test/v2/target.cpp index c4ccdab68..6d09b6922 100644 --- a/test/v2/target.cpp +++ b/test/v2/target.cpp @@ -2,6 +2,7 @@ #include #include #include +#include extern char **environ; @@ -78,6 +79,60 @@ int main(int argc, char * argv[]) return static_cast(si.dwFlags); } #endif + else if (mode == "sigterm") + { + + static boost::asio::io_context ctx; + static boost::asio::steady_timer tim{ctx, std::chrono::seconds(10)}; + +#if defined(BOOST_PROCESS_V2_WINDOWS) + BOOST_ASSERT(SetConsoleCtrlHandler( + [](DWORD kind) + { + if (kind == CTRL_CLOSE_EVENT) + { + tim.cancel(); + return TRUE; + } + else + return FALSE; + }, TRUE) != 0); +#else + signal(SIGTERM, [](int) { tim.cancel();}); +#endif + + boost::system::error_code ec; + tim.async_wait([&](boost::system::error_code ec_) { ec = ec_; }); + ctx.run(); + return ec ? EXIT_SUCCESS : 32; + } + else if (mode == "sigint") + { + static boost::asio::io_context ctx; + static boost::asio::steady_timer tim{ctx, std::chrono::seconds(10)}; + +#if defined(BOOST_PROCESS_V2_WINDOWS) + BOOST_ASSERT( + SetConsoleCtrlHandler( + [](DWORD kind) + { + if (kind == CTRL_C_EVENT) + { + tim.cancel(); + return TRUE; + } + else + return FALSE; + }, TRUE) != 0); +#else + signal(SIGINT, [](int) { tim.cancel();}); +#endif + + boost::system::error_code ec; + tim.async_wait([&](boost::system::error_code ec_) { ec = ec_; }); + ctx.run(); + return ec ? EXIT_SUCCESS : 33; + } else return 34;