/** * @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); } }
/** * @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(); }