/** * Pool, errors * * @param bool $debug * * @throws Exception */ function processPoolErrorable($debug = false) { $num = 12; $threads = 4; if ($debug) { echo '-----------------------', PHP_EOL, "Errorable thread pool test: ", Thread::$useForks ? 'Async' : 'Sync', PHP_EOL, '-----------------------', PHP_EOL; } $pool = new ThreadPool(__NAMESPACE__ . '\\TestThreadErrorable', $threads, null, null, $debug); $jobs = array(); $i = $j = 0; $left = $num; $maxI = ceil($num * 2.5); do { while ($left > 0 && $pool->hasWaiting()) { $arg = mt_rand(1000000, 200000000); $threadId = $pool->run($arg, $j); $this->assertTrue(!isset($jobs[$threadId]), "Thread #{$threadId} is not failed correctly"); $jobs[$threadId] = $arg; $left--; $j++; } if ($results = $pool->wait($failed)) { foreach ($results as $threadId => $res) { $num--; $this->assertTrue(isset($jobs[$threadId]), "Thread #{$threadId}"); $this->assertEquals($jobs[$threadId], $res, "Thread #{$threadId}"); unset($jobs[$threadId]); } } if ($failed) { foreach ($failed as $threadId => $errArray) { list($errCode, $errMsg) = $errArray; $this->assertTrue(isset($jobs[$threadId]), "Thread #{$threadId}"); $this->assertNotEmpty($errCode, 'Error code needed'); $this->assertTrue(is_int($errCode), 'Error code needed'); $this->assertNotEmpty($errMsg, 'Error message needed'); unset($jobs[$threadId]); $left++; } } $i++; } while ($num > 0 && $i < $maxI); $this->assertSame(0, $num, 'All jobs must be done'); $pool->cleanup(); $this->assertSame(0, $pool->getThreadsCount()); $this->assertEmpty($pool->getThreads()); $this->assertFalse($pool->hasWaiting()); }