Skip to content

Commit 7a3982d

Browse files
committed
+started validating concepts pre-rearchitecting of BaseJob
-removed references to pthreads *reconfigured autoloading *tuned testing configuration
1 parent 3f771a5 commit 7a3982d

File tree

9 files changed

+171
-10
lines changed

9 files changed

+171
-10
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ src/Supervisor.php -text
2121
src/ThreadModel/Models.txt -text
2222
src/WatchDog.php -text
2323
src/test.php -text
24+
tests/BasicTest.php -text
25+
tests/NewThreadTest.php -text
2426
tests/_support/UnitTester.php -text
2527
tests/unit.suite.yml -text

codeception.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1+
2+
namespace: NxSys\Toolkits\Parallax
3+
14
suites:
25
unit:
36
path: .
47

58
settings:
69
shuffle: true
710
lint: true
11+
strict_xml: true
12+
be_strict_about_changes_to_global_state: true
13+
report_useless_tests: true
14+
disallow_test_output: false
15+
log_incomplete_skipped: true
16+
colors: true
17+
18+
coverage:
19+
enabled: true
20+
include:
21+
- src/*
22+
low_limit: 33
23+
high_limit: 80
24+
show_only_summary: false
25+
826
paths:
927
tests: tests
1028
output: tests/_output
1129
support: tests/_support
1230
data: tests
13-

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"require-dev":
1212
{
1313
"codeception/codeception": "^3.0"
14+
},
15+
"autoload":
16+
{
17+
"psr-4": { "NxSys\\Toolkits\\Parallax\\": "src" }
1418
},
1519
"license": "MIT",
1620
"authors":

src/Agent/BaseAgent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* @throws NxSys\Toolkits\Parallax\IException Well, does it?
4343
* @author Chris R. Feamster <cfeamster@f2developments.com>
4444
*/
45-
class BaseAgent extends Worker // implements IAgent
45+
class BaseAgent implements IAgent
4646
{
4747

4848
}

src/Job/BaseJob.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@
3333
/** Library Dependencies **/
3434
use NxSys\Core\ExtensibleSystemClasses as CoreEsc;
3535

36+
use parallel\Runtime as Thread_Runtime;
37+
use parallel\Channel as Thread_Channel;
38+
3639
//....
3740
use SplQueue;
38-
use Thread;
39-
use Stackable;
4041
use Exception;
4142
use Throwable;
43+
use Closure;
44+
45+
const DEFAULT_CHANNEL_CAPACITY = 1024;
4246

4347

4448
/**
@@ -50,15 +54,81 @@
5054
* @author Chris R. Feamster <cfeamster@f2developments.com>
5155
*/
5256
// abstract class BaseJob extends CoreEsc\pthreads\Thread implements IJob
53-
abstract class BaseJob extends Thread implements IJob
57+
class BaseJob implements IJob
5458
{
59+
const DEFAULT_CHANNEL_CAPACITY = DEFAULT_CHANNEL_CAPACITY;
60+
5561
protected $aLocalConstants = [];
62+
public $hThreadRuntime;
5663
public function __construct()
5764
{
58-
$this->aInData=new SplQueue ;
59-
$this->aOutData=new SplQueue ;
65+
66+
// $this->aInData=new CoreEsc\spl\SplQueue ;
67+
// $this->aOutData=new CoreEsc\spl\SplQueue ;
68+
$this->aInData=new Thread_Channel(DEFAULT_CHANNEL_CAPACITY);
69+
$this->aOutData=new Thread_Channel(DEFAULT_CHANNEL_CAPACITY);
70+
$this->setRunMethod();
71+
72+
}
73+
74+
public function setRunMethod(string $sMethodName = 'run')
75+
{
76+
if (!method_exists($this, $sMethodName))
77+
{
78+
throw new InvalidArgumentException($sMethodName." is a not a valid method on ".__CLASS__);
79+
}
80+
$this->sRunMethodName=$sMethodName;
81+
}
82+
83+
public function setRuntime(Thread_Runtime $hRuntime = null): ?Thread_Runtime
84+
{
85+
return $this->hThreadRuntime=$hRuntime;
86+
}
87+
88+
public function start(int $iLegacyOptions=0, array $aThreadArguments=[])
89+
{
90+
if (!$this->hThreadRuntime)
91+
{
92+
$this->hThreadRuntime=new Thread_Runtime(__DIR__.'\..\..\vendor\autoload.php');
93+
}
94+
$hScopedBooter=Closure::fromcallable([$this, 'bootThread']);
95+
//$hScopedBooter = $hScopedBooter->bindTo($this);
96+
$this->hThreadState=$this->hThreadRuntime->run($hScopedBooter, [new $this, [$this->aInData, $this->aOutData]]);//, [($this), $aThreadArguments]);
97+
return $this->hThreadState; #"Future"
98+
}
99+
100+
public function isRunning(): bool
101+
{
102+
return $this->hThreadState->cancelled() || $this->hThreadState->done();
103+
}
104+
105+
/**
106+
* @see parallel\Future::value
107+
*/
108+
public function resolveJobToValue()
109+
{
110+
return $this->hThreadState->value();
60111
}
61112

113+
protected function bootThread($oJob, array $aThreadArguments=[]) //: mixed
114+
{
115+
//require_once __DIR__.'\..\..\vendor\autoload.php';
116+
// $oJob = unserialize($oJob);
117+
$oJob->initConstants();
118+
//channel shenanigans
119+
return false?:call_user_func_array([$oJob, $oJob->sRunMethodName], $aThreadArguments);
120+
}
121+
122+
public function run()
123+
{
124+
$sGoal="I am!";
125+
return $sGoal;
126+
}
127+
128+
//protected abstract function run();
129+
130+
//---
131+
62132
public function setupConstants(array $aConstants): void
63133
{
64134
foreach ($aConstants as $name)

src/Job/IJob.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
* @throws NxSys\Toolkits\Parallax\IException Well, does it?
4343
* @author Chris R. Feamster <cfeamster@f2developments.com>
4444
*/
45-
interface IJob
45+
interface IJob
4646
{
47-
47+
//protected function run();
48+
function start();
49+
function isRunning();
4850
}

src/Supervisor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @throws NxSys\Toolkits\Parallax\IException Well, does it?
3838
* @author Chris R. Feamster <cfeamster@f2developments.com>
3939
*/
40-
class Supervisor extends Pool //CoreEsc\pthreads\Pool
40+
class Supervisor
4141
{
4242

4343
}

tests/BasicTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace NxSys\Toolkits\Parallax;
4+
5+
use NxSys\Toolkits\Parallax;
6+
7+
class BasicTest extends \Codeception\Test\Unit
8+
{
9+
10+
protected function _before()
11+
{
12+
codecept_debug("\n");
13+
codecept_debug(sprintf(">>>CHECKPOINT %s::%s:%s<<<\n", __CLASS__, __METHOD__, __LINE__));
14+
require_once __DIR__.'\..\vendor\autoload.php';
15+
}
16+
17+
protected function _after()
18+
{
19+
codecept_debug(sprintf(">>>CHECKPOINT %s::%s:%s<<<\n", __CLASS__, __METHOD__, __LINE__));
20+
}
21+
22+
// tests
23+
public function testBaseJobCreation()
24+
{
25+
codecept_debug(sprintf(">>>CHECKPOINT %s::%s:%s<<<\n", __CLASS__, __METHOD__, __LINE__));
26+
$oPlainJob=new class extends Parallax\Job\BaseJob
27+
{
28+
public function run()
29+
{
30+
$sGoal="I am!";
31+
echo $sGoal;
32+
}
33+
};
34+
$this->assertInstanceOf(Parallax\Job\BaseJob::class, $oPlainJob);
35+
}
36+
public function testBaseJobExecution()
37+
{
38+
codecept_debug(sprintf(">>>CHECKPOINT %s::%s:%s<<<\n", __CLASS__, __METHOD__, __LINE__));
39+
$sGoalValue='I am!';
40+
41+
$oPlainJob=new Parallax\Job\BaseJob;
42+
$hThreadFuture = $oPlainJob->start();
43+
$sJobReturnValue=$oPlainJob->resolveJobToValue();
44+
45+
$this->assertEquals($sGoalValue, $sJobReturnValue);
46+
$this->assertEquals($hThreadFuture->value(), $sJobReturnValue);
47+
}
48+
}

tests/NewThreadTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
class NewThreadTest extends \Codeception\Test\Unit
3+
{
4+
5+
protected function _before()
6+
{
7+
}
8+
9+
protected function _after()
10+
{
11+
}
12+
13+
// tests
14+
public function testSomeFeature()
15+
{
16+
17+
}
18+
}

0 commit comments

Comments
 (0)