Boost::Process Pipe Streams and Unit Test - 小众知识

Boost::Process Pipe Streams and Unit Test

2022-08-09 05:01:14 苏内容
  标签:
阅读:3051

问题


I have source which two stream like this:

bp::ipstream StdOut;
bp::opstream StdIn;
bp::child MyProcess = bp::child(processPath, bp::std_out > StdOut, bp::std_in < StdIn);// Doing some stuff with StdOut and StdIn

I wanted to know if there is a way to write manually into this StdOut and read from StdIn in order to do unit tests? Thank you very much!


回答1:


You can write to them as a normal stream:

Live On Coliru

#include <boost/process.hpp>#include <iostream>namespace bp = boost::process;int main() {
    bp::ipstream StdOut;
    bp::opstream StdIn;
    bp::child MyProcess = bp::child("/usr/bin/rev", bp::std_out > StdOut, bp::std_in < StdIn);    for (int i = 0; i<100; ++i) {
        StdIn << "Sure no problem, " << i << std::endl;
    }
    StdIn.flush();
    StdIn.pipe().close();    std::string il;    while (getline(StdOut, il)) {        std::cout << il << std::endl;
    }    std::cout << "Bye\n";
}

Prints

0 ,melborp on eruS1 ,melborp on eruS2 ,melborp on eruS...99 ,melborp on eruSBye

Note, like the documentation warns, if your process requires async IO you could end up deadlocking (e.g. waiting for output while the child process is waiting for input).

To achieve this, use Asio's asynchronous operations:

Live On Coliru

#include <boost/process.hpp>#include <boost/asio.hpp>#include <boost/process/async.hpp>#include <iostream>namespace ba = boost::asio;namespace bp = boost::process;

int main() {
    ba::io_context io;

   bp::async_pipe ip(io), op(io);

   //bp::async_pipe StdIn(io);

   bp::child MyProcess = bp::child("/usr/bin/tac", bp::std_out > op, bp::std_in < ip, io);    std::function<void()> send_loop, recv_loop;    ba::streambuf sb;

   send_loop = [&, i=0]() mutable {

       if (++i<100) {

           std::ostream(&sb) << "Sure no problem, " << i << std::endl;

           ba::async_write(ip, sb, [&send_loop](auto ec, auto /*tx*/) {

               //std::cout << "Sent " << tx << " bytes (" << ec.message() << ")\n";

               if (!ec) send_loop();            });        } else ip.close();    };    ba::streambuf rb;    recv_loop = [&]() {

       ba::async_read_until(op, rb, "\n", [&](auto ec, auto /*tx*/) {

           //std::cout << "Received " << tx << " bytes (" << ec.message() << ")\n";

           std::cout << &rb << std::flush;

           if (!ec) recv_loop();

       });    };    io.post(send_loop);    io.post(recv_loop);    io.run();    std::cout << "Bye\n"; }

Prints the expected:

Sure no problem, 99Sure no problem, 98Sure no problem, 97Sure no problem, 96Sure no problem, 95Sure no problem, 94Sure no problem, 93Sure no problem, 92Sure no problem, 91Sure no problem, 90Sure no problem, 89Sure no problem, 88Sure no problem, 87Sure no problem, 86Sure no problem, 85Sure no problem, 84Sure no problem, 83Sure no problem, 82Sure no problem, 81Sure no problem, 80Sure no problem, 79Sure no problem, 78Sure no problem, 77Sure no problem, 76Sure no problem, 75Sure no problem, 74Sure no problem, 73Sure no problem, 72Sure no problem, 71Sure no problem, 70Sure no problem, 69Sure no problem, 68Sure no problem, 67Sure no problem, 66Sure no problem, 65Sure no problem, 64Sure no problem, 63Sure no problem, 62Sure no problem, 61Sure no problem, 60Sure no problem, 59Sure no problem, 58Sure no problem, 57Sure no problem, 56Sure no problem, 55Sure no problem, 54Sure no problem, 53Sure no problem, 52Sure no problem, 51Sure no problem, 50Sure no problem, 49Sure no problem, 48Sure no problem, 47Sure no problem, 46Sure no problem, 45Sure no problem, 44Sure no problem, 43Sure no problem, 42Sure no problem, 41Sure no problem, 40Sure no problem, 39Sure no problem, 38Sure no problem, 37Sure no problem, 36Sure no problem, 35Sure no problem, 34Sure no problem, 33Sure no problem, 32Sure no problem, 31Sure no problem, 30Sure no problem, 29Sure no problem, 28Sure no problem, 27Sure no problem, 26Sure no problem, 25Sure no problem, 24Sure no problem, 23Sure no problem, 22Sure no problem, 21Sure no problem, 20Sure no problem, 19Sure no problem, 18Sure no problem, 17Sure no problem, 16Sure no problem, 15Sure no problem, 14Sure no problem, 13Sure no problem, 12Sure no problem, 11Sure no problem, 10Sure no problem, 9Sure no problem, 8Sure no problem, 7Sure no problem, 6Sure no problem, 5Sure no problem, 4Sure no problem, 3Sure no problem, 2Sure no problem, 1Sure no problem, 1



来源:https://stackoverflow.com/questions/61155985/boostprocess-pipe-streams-and-unit-test


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1