/** * @throws \Axvf\Threadable\Exceptions\NoSuchProcessException */ public function testStart() { $Class = $this->getAbstraction(); $Class->spawnThread(); $this->assertGreaterThan(1, \Axvf\Threadable\ProcessControl::getProcessIdsOfThreadKey($Class->getThreadKey())[0]); $this->assertTrue(Redis::client()->exists($Class->getThreadKey())); $Class->killThread(); $this->assertFalse(Redis::client()->exists($Class->getThreadKey())); }
/** * Kill this thread after it's been launched. * @throws Exceptions\NoSuchProcessException * @throws InvalidThreadKeyException * @return void */ public final function killThread() { $this->getRedisInstance()->delete($this->getThreadKey()); foreach (ProcessControl::getProcessIdsOfThreadKey($this->getThreadKey()) as $ProcessId) { ProcessControl::kill($ProcessId); } }
use Axvf\Test\ThreadableExample; echo Align::singleLine(['Threadable Demo.']); echo "\n"; echo Align::singleLine(['This will spawn 10 processes then']); echo Align::singleLine(['kill a random process every 0.2']); echo Align::singleLine(['seconds till none are left.']); echo "\n\n"; echo Align::singleLine(['You can use these two commands']); echo Align::singleLine(['in another terminal to watch the']); echo Align::singleLine(['processes being spawned & killed.']); echo "\n\n"; echo Align::singleLine(['watch -t -n 0.1 \'screen -list\'']); echo "\n"; echo Align::singleLine(['watch -t -n 0.1 \'echo "select 0\\n keys *" | redis-cli\'']); echo "\n\n"; \Axvf\Threadable\ProcessControl::killAllThreads(); $Threadables = []; for ($i = 0; $i < 10; $i++) { $Threadables[] = new ThreadableExample(); } foreach ($Threadables as $Threadable) { /** * @var \Axvf\Threadable\ThreadableInterface $Threadable */ echo $Threadable->spawnThread() . "\n"; } sleep(2); try { while (count($Threadables) > 0) { $Rand = array_rand($Threadables); $Threadable = $Threadables[$Rand];
/** * @param ThreadableInterface $Threadables * @dataProvider randomThreadableProvider */ public function testGetScreenListMatchesRandom(ThreadableInterface ...$Threadables) { $this->setClass(new ProcessControl()); ProcessControl::killAllThreads(); $this->myTestForEmptyScreenlist(); foreach ($Threadables as $Threadable) { /** * @var ThreadableInterface $Threadable */ $Threadable->spawnThread(); } $RawScreenList = $this->getPrivateMethod('getRawScreenlist'); $this->assertInternalType('string', $RawScreenList); $this->assertStringStartsWith('There are screens on', $RawScreenList); $this->assertStringEndsWith("\n", $RawScreenList); $this->assertRegexMatchesFormat($this->getPrivateMethod('getScreenlistMatches'), count($Threadables)); $this->assertEquals(count($Threadables), count($this->getPrivateMethod('getProcessIds'))); foreach ($Threadables as $Threadable) { /** * @var ThreadableInterface $Threadable */ $this->assertContains($Threadable->getThreadKey(), $this->getPrivateMethod('getScreenlistMatches')[2]); $this->assertArrayHasKey($Threadable->getThreadKey(), $this->getPrivateMethod('getProcessIds')); $ProcessIds = ProcessControl::getProcessIdsOfThreadKey($Threadable->getThreadKey()); $this->assertArrayHasKey(0, $ProcessIds); $this->assertEquals(1, count($ProcessIds)); $this->assertRegExp('/^[0-9]{2,6}$/', $ProcessIds[0]); $Threadable->killThread(); $this->assertNotContains($Threadable->getThreadKey(), $this->getPrivateMethod('getScreenlistMatches')[2]); $this->assertArrayNotHasKey($Threadable->getThreadKey(), $this->getPrivateMethod('getProcessIds')); try { ProcessControl::getProcessIdsOfThreadKey($Threadable->getThreadKey()); $this->fail('Expected NoSuchProcessException to be thrown.'); } catch (NoSuchProcessException $Exception) { } catch (Exception $Exception) { $this->fail('Unexpected Exception - ' . get_class($Exception)); } } $this->myTestForEmptyScreenlist(); }