在Boost单元测试中使用管道



我正在尝试为我的服务器编写Boost单元测试。我想启动我的服务器- start()在线程中启动服务器-打开客户端,连接它并尝试下载文件。

我是这样做的:

tftp_server* my_test_server;
BOOST_AUTO_TEST_SUITE (tftptest) // name of the test suite is tftptest
BOOST_AUTO_TEST_CASE (test1)
{
    my_test_server = new tftp_server(69);
    my_test_server->start();
    FILE *in;
    char buff[512];

    if(!(in = popen("tftp", "w"))){
        exit(1);
    }
    fputs ( "connect xx.xx.xx.xxn", in );
    fputs ( "mode binaryn", in );
    fputs ( "moden", in );
    fputs ( "get truc.txtn", in );
    while(fgets(buff, sizeof(buff), in)!=NULL)
        printf("%s", buff);
    sleep(10);
    BOOST_CHECK(true);
}
1)创建并启动服务器,

2)使用系统TFTP客户端(我在OSX上),

3)等待10秒。

它不工作:它只在测试完成后执行客户端。

Running 1 test case...
Server started on port 69
Server running.
*** No errors detected
$ Using octet mode to transfer files.
Transfer timed out.

你知道我该怎么解决这个问题吗?

谢谢!

编辑
void tftp_server::start()
{
    LOG_INFO("Server running.", 0);
    boost::thread bt(boost::bind(&boost::asio::io_service::run, &_io_service));
}

我添加了close,它工作了。感谢Arne Mertz。

if(!(in = popen("tftp", "w"))){
    exit(1);
}
//...
while(fgets(buff, sizeof(buff), in)!=NULL)
    printf("%s", buff);
pclose(in);

最新更新