3

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 の予想される動作ですか? そして、それを修正する方法はありますか?

4

0 に答える 0