Boost.测试和分叉

发布于 2024-09-07 02:45:04 字数 423 浏览 6 评论 0原文

我正在使用 Boost.Test 进行单元测试,目前正在单独的线程中运行各种模拟服务器,这些线程从每个测试中启动。为了更准确地测试我的代码,模拟服务器实际上应该位于单独的进程中。

我正在考虑按照这些思路做一些事情:

MY_TEST()
if (fork() == 0) {
    runMockServer();  // responds to test requests or times out, then returns
    exit(0);
}
// Connect to MockServ and Run actual test here
END_TEST()

但我担心这会搞砸测试框架。

这安全吗?有人做过这样的事吗?

如果重要的话,我在 Ubuntu 8.04 上使用 Boost 1.34.1。

I'm using Boost.Test for Unit testing and am currently running various mock servers in separate threads which get launched from within each test. In order to more accurately test my code the mock server's should really be in separate processes.

I was thinking about doing something along these lines:

MY_TEST()
if (fork() == 0) {
    runMockServer();  // responds to test requests or times out, then returns
    exit(0);
}
// Connect to MockServ and Run actual test here
END_TEST()

but I'm worried that this will screw up the testing framework.

Is this safe? Has anyone done something like this?

I'm using Boost 1.34.1 on Ubuntu 8.04 if that matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-09-14 02:45:04

我在类似的情况下使用了 Boost 单元测试库,并取得了积极的结果。我想要进行自动测试,看看一个库在分叉时是否能正常工作。尽管在我的例子中它也更接近于系统测试,但如果可用的工具能够实现您想要的效果,我完全赞成使用它们。

不过,需要克服的一个障碍是在不使用 boost 断言宏的情况下从子进程发出错误信号。例如,如果使用BOOST_REQUIRE,它将提前中止测试,并且任何后续测试都将在父进程和子进程中执行。我最终使用进程退出代码向等待的父进程发出错误信号。但是,请勿使用 exit() 因为 boost 有 atexit() 钩子即使子进程中没有错误,也会发出错误信号。请改用 _exit()

我用于测试的设置是这样的。

BOOST_AUTO_TEST_CASE(Foo)
{
  int pid = fork();
  BOOST_REQUIRE( pid >= 0 );
  if( pid  == 0 ) // child
  {
    // Don't use Boost assert macros here
    // signal errors with exit code

    // Don't use exit() since Boost test hooks 
    // and signal error in that case, use _exit instead.
    int rv = something(); 
    _exit(rv);
  }else{ // parent
    // OK to use boost assert macros in parent
    BOOST_REQUIRE_EQUAL(0,0);
    // Lastly wait for the child to exit
    int childRv;
    wait(&childRv);
    BOOST_CHECK_EQUAL(childRv, 0);
  }

}

I have used the Boost Unit Test library in similar circumstances with positive results. I wanted to have automatic tests to see if a library worked as it should when forking. Although it was also closer to a system test in my case I'm all for using available tools if they achieve what you want.

One obstacle to overcome though is to signal errors from the child process without using boost assert macros. If e.g. BOOST_REQUIRE would be used it would prematurely abort the test and any subsequent tests would be executed in both parent and child processes. I ended up using the process exit code to signal error to the waiting parent process. However, don't use exit() as boost have atexit() hooks that signal errors in the child process even if there are none. Use _exit() instead.

The setup I used for tests was something like this.

BOOST_AUTO_TEST_CASE(Foo)
{
  int pid = fork();
  BOOST_REQUIRE( pid >= 0 );
  if( pid  == 0 ) // child
  {
    // Don't use Boost assert macros here
    // signal errors with exit code

    // Don't use exit() since Boost test hooks 
    // and signal error in that case, use _exit instead.
    int rv = something(); 
    _exit(rv);
  }else{ // parent
    // OK to use boost assert macros in parent
    BOOST_REQUIRE_EQUAL(0,0);
    // Lastly wait for the child to exit
    int childRv;
    wait(&childRv);
    BOOST_CHECK_EQUAL(childRv, 0);
  }

}
梦里梦着梦中梦 2024-09-14 02:45:04

这听起来并不像是针对您想要实现的目标进行单元测试。虽然我不明白为什么它不安全。如果单元测试尚未准备好,您可能会遇到竞争条件,即单元测试连接到 MockServ,但这很容易解决。

我从来没有直接做过这样的事情,但是我已经为 fork/exec 子进程的库编写了单元测试,并且它可以完美地工作。

This doesn't really sound like unit-testing for what you want to achieve. Though I don't see why it would not be safe. You may have a race condition where your unit test connects to the MockServ if it isn't ready yet, but that is easily solvable.

I've never done something like this directly, but I have written unit tests for libraries that fork/exec child processes and it works flawlessly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文