PHPUnit测试中的pcntl_fork()用法

时间:2014-01-17 21:10:01

标签: php phpunit fork pcntl

我在PHPUnit中使用pcntl_fork()时遇到了一些问题。我正在执行此代码

class ForkTest extends PHPUnit_Framework_TestCase {
    public function test1() {
        print('Start test with pid '.posix_getpid()."\n");

        $pids = [];

        for ($count = 0; $count < 3; ++$count) {
            $pids[$count] = pcntl_fork();
            if (-1 == $pids[$count]) {
                die("It's a trap, iteration $count\n");
            } else if (!$pids[$count]) {
                print("I'm child with pid ".posix_getpid()."\n");
                exit();
            }
            print('Parent: after fork, new child: '.$pids[$count]."\n");
        }

        for ($count = 0; $count < 3; ++$count) {
            $status = 0;
            if ($pids[$count] != pcntl_waitpid($pids[$count], $status)) {
                die("Error with wait pid $count.\n");
            } else {
                print('Process with pid '.$pids[$count]." finished\n");
            }
        }
    }
}

我得到以下内容:

Start test with pid 15886
Parent: after fork, new child: 15887
Parent: after fork, new child: 15888
I'm child with pid 15889
Start test with pid 15886
Parent: after fork, new child: 15887
I'm child with pid 15888
Start test with pid 15886
I'm child with pid 15887
.Start test with pid 15886
Parent: after fork, new child: 15887
Parent: after fork, new child: 15888
Parent: after fork, new child: 15889
Process with pid 15887 finished
Process with pid 15888 finished
Process with pid 15889 finished

但是我期待这样的事情(我在没有使用PHPUnit的情况下运行我的测试时得到了它):

Start test with pid 15907
Parent: after fork, new child: 15908
Parent: after fork, new child: 15909
Parent: after fork, new child: 15910
I'm child with pid 15910
I'm child with pid 15909
I'm child with pid 15908
Process with pid 15908 finished
Process with pid 15909 finished
Process with pid 15910 finished

那么,这是PHPUnit的预期行为?还有什么方法可以解决它吗?

0 个答案:

没有答案