示例#1
0
 /**
  * Pool 2
  *
  * @param bool $debug
  *
  * @throws Exception
  */
 function processPool2($debug = false)
 {
     $async = Thread::$useForks;
     if ($debug) {
         echo '-------------------------', PHP_EOL, "Additional thread pool test: ", $async ? 'Async' : 'Sync', PHP_EOL, '-------------------------', PHP_EOL;
     }
     // ReturnAllArguments
     $threads = 2;
     $thread = __NAMESPACE__ . '\\TestThreadReturnAllArguments';
     $pName = 'ReturnAllArguments';
     $pool = new ThreadPool($thread, $threads, $pName, null, $debug);
     $this->assertSame($pName, $pool->getThreadProcessName());
     $state = $pool->getThreadsState();
     $this->assertSame($async ? $threads : 1, count($state));
     foreach ($state as $s) {
         $this->assertTrue('INIT' === $s || 'WAIT' === $s);
     }
     if ($async) {
         $catched = 0;
         try {
             $this->assertFalse($pool->run());
         } catch (Exception $e) {
             $catched++;
         }
         try {
             $this->assertFalse($pool->run('example'));
         } catch (Exception $e) {
             $catched++;
         }
         $this->assertSame(2, $catched);
     }
     $data = array(array(), array(1), array(1, 2), array(1, 2, 3), array(1, 2, 3, 4), array(1, 2, 3, 4, 5), array(null), array(null, null));
     $worked = $jobs = array();
     $i = 0;
     $left = $num = count($data);
     $maxI = ceil($num * 1.5);
     do {
         while ($left > 0 && $pool->hasWaiting()) {
             $args = array_shift($data);
             $threadId = call_user_func_array(array($pool, 'run'), $args);
             $this->assertNotEmpty($threadId);
             $this->assertTrue(!isset($jobs[$threadId]), "Thread #{$threadId} is not failed correctly");
             $jobs[$threadId] = $args;
             $worked[$threadId] = true;
             $left--;
         }
         $results = $pool->wait($failed);
         $this->assertEmpty($failed, 'Failed results: ' . print_r($failed, true));
         if ($results) {
             foreach ($results as $threadId => $res) {
                 $this->assertTrue(isset($jobs[$threadId]), "Thread #{$threadId}");
                 $this->assertSame($jobs[$threadId], $res, "Thread #{$threadId}");
                 unset($jobs[$threadId]);
                 $num--;
             }
         }
         $i++;
     } while ($num > 0 && $i < $maxI);
     $this->assertSame(0, $num, 'All jobs must be done');
     $pool->cleanup();
     // TestThreadDoNothing
     $threads = 2;
     $thread = __NAMESPACE__ . '\\TestThreadDoNothing';
     $pool = new ThreadPool($thread, $threads, null, null, $debug);
     $i = 0;
     $left = $num = 1;
     $maxI = 6;
     do {
         while ($left > 0 && $pool->hasWaiting()) {
             $threadId = $pool->run();
             $jobs[$threadId] = null;
             $worked[$threadId] = true;
             $left--;
         }
         $results = $pool->wait($failed);
         $this->assertEmpty($failed, 'Failed results: ' . print_r($failed, true));
         if ($results) {
             foreach ($results as $threadId => $res) {
                 $this->assertTrue(array_key_exists($threadId, $jobs), "Thread #{$threadId}");
                 $this->assertSame($jobs[$threadId], $res, "Thread #{$threadId}");
                 unset($jobs[$threadId]);
                 $num--;
                 break 2;
             }
         }
         $i++;
     } while ($i < $maxI);
     $this->assertSame(0, $num, 'All jobs must be done');
     $pool->cleanup();
 }