Skip to content

Commit

Permalink
Make session id optional and settable.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaynton committed Mar 2, 2017
1 parent 2c8ff96 commit c89c4da
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/TaskInstanceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class TaskInstanceState implements TaskInstanceStateInterface {
* TaskState constructor.
*
* @param int $task_id
* @param string $session_id
* The id of the session that is currently running this task instance.
* @param int $num_runnables_estimate
* Approximate number of runnables that will be run to complete the task.
* @param int[]|null $runner_ids
Expand All @@ -50,11 +48,14 @@ class TaskInstanceState implements TaskInstanceStateInterface {
* application must call setRunnerIds before run()ning the task instance.
*
* The array size must equal $num_runners.
*
* @param string|null $session_id
* The id of the session that is currently running this task instance.
* This optional property can help controllers verify that a request should
* be used for a runner of this task, but is not used by the framework.
* @throws \InvalidArgumentException
* If $runner_ids is given and not an array of length equal to $num_runners.
*/
public function __construct($task_id, $session_id, $num_runners, $num_runnables_estimate, $runner_ids = NULL) {
public function __construct($task_id, $num_runners, $num_runnables_estimate, $runner_ids = NULL, $session_id = NULL) {
$this->task_id = $task_id;

$this->session_id = $session_id;
Expand Down Expand Up @@ -112,6 +113,10 @@ public function getRunnerIds() {
return $this->runner_ids;
}

public function setOwnerSession($session_id) {
$this->session_id = $session_id;
}

public function getOwnerSession() {
return $this->session_id;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AbstractRunnerProgressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function sutFactory($task, $opts = []) {

if ($task !== NULL) {
$num_runnables = isset($opts['num_runnables']) ? $opts['num_runnables'] : 5;
$scheduledTask = new TaskInstanceState(AbstractRunnerTest::$monotonic_task_id++, '-', 1, $num_runnables);
$scheduledTask = new TaskInstanceState(AbstractRunnerTest::$monotonic_task_id++, 1, $num_runnables);
$scheduledTask->setRunnerIds([$sut->getRunnerId()]);

$this->current_schedule = $scheduledTask;
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/AbstractRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testAbstractRunnerInstantiates() {
}

public function testGetIncarnationTargetRuntime() {
$schedule = new TaskInstanceState($this->assignTaskId(), '-', 1, 10, [1]);
$schedule = new TaskInstanceState($this->assignTaskId(), 1, 10, [1]);
$sut = $this->sutFactory(20, 1, $schedule, ['target_completion_seconds' => 42]);

$this->assertEquals(
Expand All @@ -151,7 +151,7 @@ public function testGetIncarnationTargetRuntime() {
public function testAbstractRunnerCompletesTask_OneIncarnation() {
$task = new TaskMock();
$num_runnables = 10;
$schedule = new TaskInstanceState($this->assignTaskId(), '-', 1, $num_runnables, [1]);
$schedule = new TaskInstanceState($this->assignTaskId(), 1, $num_runnables, [1]);
$controller = new RunnerControllerMock();
$sut = $this->sutFactory(20, 1, $schedule, ['controller' => $controller]);
$result = $sut->run($task, $schedule);
Expand All @@ -168,7 +168,7 @@ public function testAbstractRunnerCompletesTask_OneIncarnation() {
public function testAbstractRunnerCompletesTask_MultipleIncarnations() {
$task = new TaskMock();
$num_runnables = 30;
$schedule = new TaskInstanceState($this->assignTaskId(), '-', 1, $num_runnables, [1]);
$schedule = new TaskInstanceState($this->assignTaskId(), 1, $num_runnables, [1]);
$sut = $this->sutFactory(10, 1, $schedule);
$num_incarnations = 1;
while (($result = $sut->run($task, $schedule)) === NULL) {
Expand All @@ -190,7 +190,7 @@ public function testAbstractRunnerCompletesTask_MultipleIncarnations_2() {
// last Runner lifecycle.
$num_runnables = 25;
$task = new TaskMock();
$schedule = new TaskInstanceState($this->assignTaskId(), '-', 1, $num_runnables, [1]);
$schedule = new TaskInstanceState($this->assignTaskId(), 1, $num_runnables, [1]);
$sut = $this->sutFactory(10, 1, $schedule);
$num_incarnations = 1;
while (($result = $sut->run($task, $schedule)) === NULL) {
Expand Down Expand Up @@ -257,7 +257,7 @@ protected function _testAbstractRunnerCompletesNonUnaryTasks($num_runnables, $su
protected function _multipleRunners_MultipleIncarnations(TaskInterface $task, $num_runnables) {
// Some arbitrary Runner IDs should not impact anything if in sorted order.
$runner_ids = [412 + static::$monotonic_runner_id++, 562 + static::$monotonic_runner_id++, 628 + static::$monotonic_runner_id++];
$schedule = new TaskInstanceState($this->assignTaskId(), '-', count($runner_ids), $num_runnables);
$schedule = new TaskInstanceState($this->assignTaskId(), count($runner_ids), $num_runnables);
$schedule->setRunnerIds($runner_ids);
foreach ($runner_ids as $runner_id) {
$runners[$runner_id] = $this->sutFactory(5, $runner_id, $schedule);
Expand Down Expand Up @@ -314,7 +314,7 @@ protected function _testRunnableEvents($runner_succeeds) {
throw new \Exception('Mocked runnable error.');
};
}
$scheduled_task = new TaskInstanceState($this->assignTaskId(), '-', 1, $num_runnables, [1]);
$scheduled_task = new TaskInstanceState($this->assignTaskId(), 1, $num_runnables, [1]);

$runner = $this->sutFactory(-1, 1, $scheduled_task, ['controller' => $controller]);

Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/TaskInstanceStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class TaskInstanceStateTest extends \PHPUnit_Framework_TestCase {
protected function sutFactory($opts) {
return new TaskInstanceState(
1,
isset($opts['session']) ? $opts['session'] : '-',
isset($opts['num_runners']) ? $opts['num_runners'] : 1,
isset($opts['num_runnables']) ? $opts['num_runnables'] : 10,
isset($opts['runner_ids']) ? $opts['runner_ids'] : [1]
isset($opts['runner_ids']) ? $opts['runner_ids'] : [1],
isset($opts['session']) ? $opts['session'] : NULL
);
}

Expand Down Expand Up @@ -45,6 +45,13 @@ public function testOwnerSession() {
'fred',
$sut->getOwnerSession()
);

$sut = $this->sutFactory([]);
$sut->setOwnerSession('fred23');
$this->assertEquals(
'fred23',
$sut->getOwnerSession()
);
}

public function testRunnableCountUpdate() {
Expand Down

0 comments on commit c89c4da

Please sign in to comment.