Skip to content

Commit

Permalink
moved interrupt & request_exit into target.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Sep 12, 2023
1 parent 9da880b commit c24ddad
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 66 deletions.
111 changes: 45 additions & 66 deletions test/v2/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include <boost/process/v2/stdio.hpp>
#include <boost/process/v2/bind_launcher.hpp>

#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <boost/process/v2/windows/creation_flags.hpp>
#include <boost/process/v2/windows/show_window.hpp>
#endif

#include <boost/test/unit_test.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/connect_pipe.hpp>
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -593,23 +568,26 @@ 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
);

asio::steady_timer tim{ctx, std::chrono::milliseconds(50)};
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();
Expand All @@ -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
Expand All @@ -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();
Expand Down
55 changes: 55 additions & 0 deletions test/v2/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <thread>
#include <boost/process/v2/environment.hpp>
#include <boost/asio/steady_timer.hpp>

extern char **environ;

Expand Down Expand Up @@ -78,6 +79,60 @@ int main(int argc, char * argv[])
return static_cast<int>(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;

Expand Down

0 comments on commit c24ddad

Please sign in to comment.