开发者

Boost.Test and Forking

开发者 https://www.devze.com 2023-01-05 03:16 出处:网络
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 serv

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.


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);
  }

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消