Skip to main content
Tweeted twitter.com/StackCodeReview/status/1410025290126987267
Title should be purpose rather than mechanism; edited tags
Source Link
Toby Speight
  • 81.9k
  • 14
  • 101
  • 309

Implementation of a class that runs a Run an asynchronous child process with timeout using boost process in async mode

In the following code, I am trying to implement a programclass that runsstarts a process and returns its return code, stdout and stderr. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi threaded-threaded application.

Implementation of a class that runs a child process with timeout using boost process in async mode

In the following code, I am trying to implement a program that runs a process and returns its return code, stdout and stderr. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi threaded application.

Run an asynchronous child process with timeout

In the following code, I implement a class that starts a process and returns its return code, stdout and stderr. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi-threaded application.

Added tests.
Source Link
bisarch
  • 179
  • 1
  • 2
  • 7

Below is the code I am using to test it

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ProcessTest

#include <boost/test/unit_test.hpp>
#include "../src/Process.h"

BOOST_AUTO_TEST_CASE( ProcessTest )
{
    const std::vector<std::string> commands = {
      "ls"
    , "pwd"
    , "uname -a"
    , "cat /proc/cpuinfo"
    , "wget https://dl.google.com/dl/earth/client/current/google-earth-pro-stable-current.x86_64.rpm"};

    for(const auto& cmd: commands)
    {
    Process p(cmd, 3600);
    p.run();
    BOOST_CHECK( p.getReturnStatus() == 0);        // #1 continues on error
    }

    const std::vector<std::string> commandsThatShouldFail = {
      "ls doesnotexit"
    , "cat /proc/doesnotexist"
    , "wget https://dl.google.com/dl/earth/client/current/doesnotxist.rpm"};

    for(const auto& cmd: commandsThatShouldFail)
    {
    Process p(cmd, 3600);
    p.run();
    BOOST_CHECK( p.getReturnStatus() != 0);        // #1 continues on error
    }
}

Please provide your valuable comments and suggestions.

Please provide your valuable comments and suggestions.

Below is the code I am using to test it

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ProcessTest

#include <boost/test/unit_test.hpp>
#include "../src/Process.h"

BOOST_AUTO_TEST_CASE( ProcessTest )
{
    const std::vector<std::string> commands = {
      "ls"
    , "pwd"
    , "uname -a"
    , "cat /proc/cpuinfo"
    , "wget https://dl.google.com/dl/earth/client/current/google-earth-pro-stable-current.x86_64.rpm"};

    for(const auto& cmd: commands)
    {
    Process p(cmd, 3600);
    p.run();
    BOOST_CHECK( p.getReturnStatus() == 0);        // #1 continues on error
    }

    const std::vector<std::string> commandsThatShouldFail = {
      "ls doesnotexit"
    , "cat /proc/doesnotexist"
    , "wget https://dl.google.com/dl/earth/client/current/doesnotxist.rpm"};

    for(const auto& cmd: commandsThatShouldFail)
    {
    Process p(cmd, 3600);
    p.run();
    BOOST_CHECK( p.getReturnStatus() != 0);        // #1 continues on error
    }
}

Please provide your valuable comments and suggestions.

deleted 1 character in body
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

In the following code, I am trying to implement a program that runs a process and returns its return code, , stdoutstdout and andstderrstderr`. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi threaded application.

In the following code, I am trying to implement a program that runs a process and returns its return code, stdoutandstderr`. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi threaded application.

In the following code, I am trying to implement a program that runs a process and returns its return code, stdout and stderr. It also has a concept of timeout after which the child process is killed. This class will be used as a part of a multi threaded application.

Source Link
bisarch
  • 179
  • 1
  • 2
  • 7
Loading