Skip to content

Commit

Permalink
fix(libsinsp_e2e): add retry to subprocess class
Browse files Browse the repository at this point in the history
Signed-off-by: Roberto Scolaro <[email protected]>
  • Loading branch information
therealbobo committed May 4, 2024
1 parent 3ba859e commit cfd8e6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
40 changes: 24 additions & 16 deletions test/libsinsp_e2e/subprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ limitations under the License.
#include <sys/wait.h>
#include <ext/stdio_filebuf.h>

subprocess::subprocess(std::string command, std::vector<std::string> arguments, bool start_now)
: m_pid(-1)
subprocess::subprocess(std::string command, std::vector<std::string> arguments,
bool start_now, int retry_attempts):
m_pid(-1),
m_retry_attemps(retry_attempts),
m_command(command),
m_args(arguments)
{
m_command = command;
m_args = arguments;
if(start_now)
{
start();
Expand All @@ -60,20 +62,26 @@ void subprocess::wait_for_start()
timeout.tv_usec = 0;

int result = select(m_out_pipe[0] + 1, &read_set, nullptr, nullptr, &timeout);
int attempt = 0;

switch(result)
while(attempt < m_retry_attemps)
{
case -1:
perror("select");
break;
case 0:
std::cerr << "Timeout waiting for process to start." << std::endl;
break;
default:
if (!FD_ISSET(m_out_pipe[0], &read_set)) {
std::cerr << "Unexpected error during select." << std::endl;
}
break;
switch(result)
{
case -1:
perror("select");
break;
case 0:
std::cerr << "Timeout waiting for process to start. Retry n."
<< (attempt + 1) << "/" << m_retry_attemps << std::endl;
break;
default:
if (!FD_ISSET(m_out_pipe[0], &read_set)) {
std::cerr << "Unexpected error during select." << std::endl;
}
break;
}
attempt++;
}

}
Expand Down
5 changes: 3 additions & 2 deletions test/libsinsp_e2e/subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ limitations under the License.

class subprocess {
public:

subprocess(std::string command, std::vector<std::string> arguments, bool start_now=true);
subprocess(std::string command, std::vector<std::string> arguments,
bool start_now=true, int retry_attempts=3);
~subprocess();

void wait_for_start();
Expand All @@ -51,6 +51,7 @@ class subprocess {
pid_t m_pid;
int m_in_pipe[2];
int m_out_pipe[2];
int m_retry_attemps;

std::ostream* m_in_stream;
std::istream* m_out_stream;
Expand Down

0 comments on commit cfd8e6b

Please sign in to comment.