X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=testing%2Flib%2FServer.cpp;h=481a9fd6eb582c3299eb1abfdebeb45f4cb2cf20;hb=e7f51c073a098b790b442c680cbee712a5dc2fef;hp=e5ef5ead68b12e4ddafb4e510941763f750c226d;hpb=d1c9b695e9b3fbfcc94230a242b0290b43f27006;p=m6w6%2Flibmemcached diff --git a/testing/lib/Server.cpp b/testing/lib/Server.cpp index e5ef5ead..481a9fd6 100644 --- a/testing/lib/Server.cpp +++ b/testing/lib/Server.cpp @@ -13,6 +13,9 @@ Server::Server(string binary_, Server::argv_t args_) Server::~Server() { stop(); wait(); + if (pipe != -1) { + close(pipe); + } if (holds_alternative(socket_or_port)) { unlink(get(socket_or_port).c_str()); } @@ -57,7 +60,7 @@ vector Server::createArgv() { vector arr; pushArg(arr, binary); - pushArg(arr, "-v"); + //pushArg(arr, "-v"); for (auto it = args.cbegin(); it != args.cend(); ++it) { if (holds_alternative(*it)) { @@ -87,23 +90,23 @@ vector Server::createArgv() { return arr; } -optional Server::start() { - if (pid) { - return pid; - } +optional Server::start() { + if (!pid) { + auto argv = createArgv(); + ForkAndExec fork_and_exec{binary.c_str(), argv.data()}; - auto argv = createArgv(); - auto child = ForkAndExec{binary.c_str(), argv.data()}(); + pipe = fork_and_exec.createPipe(); + pid = fork_and_exec(); - for (auto argp : argv) { - delete [] argp; - } + for (auto argp : argv) { + delete [] argp; + } - if (child.has_value()) { - pid = child.value(); + if (!pid) { + return {}; + } } - - return child; + return ChildProc{pid, pipe}; } bool Server::isListening() { @@ -140,7 +143,15 @@ bool Server::check() { bool Server::wait(int flags) { if (pid && pid == waitpid(pid, &status, flags)) { + if (drain().length()) { + cerr << "Ouput of " << *this << ":\n" << output << endl; + output.clear(); + } pid = 0; + if (pipe != -1) { + close(pipe); + pipe = -1; + } return true; } return false; @@ -179,3 +190,34 @@ const socket_or_port_t &Server::getSocketOrPort() const { return socket_or_port; } +int Server::getPipe() const { + return pipe; +} + +string &Server::drain() { + if (pipe != -1) { + again: + char read_buf[1<<12]; + auto read_len = read(pipe, read_buf, sizeof(read_buf)); + + if (read_len > 0) { + output.append(read_buf, read_len); + goto again; + } + if (read_len == -1) { + switch (errno) { + case EINTR: + goto again; + default: + perror("Server::drain read()"); + [[fallthrough]]; + case EAGAIN: +#if EWOULDBLOCK != EAGAIN + case EWOULDBLOCK: +#endif + break; + } + } + } + return output; +}