Skip to content

Commit

Permalink
Merge pull request #16 from ElvenSpellmaker/feature/run-x-times
Browse files Browse the repository at this point in the history
Implements the ability to run `x` times.
  • Loading branch information
ElvenSpellmaker committed Jan 23, 2016
2 parents 96e1004 + a3d07a6 commit 70c41d7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/GrepExample.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
* @title Basic example to show grepping.
Expand Down
1 change: 1 addition & 0 deletions examples/LargeToSmallHeadExample.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
* @title Example to show large head into smaller head.
Expand Down
1 change: 1 addition & 0 deletions examples/SmallToLargeHeadExample.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
* @title Example to show small head into larger head.
Expand Down
1 change: 1 addition & 0 deletions examples/YesHeadExample.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
* @title Basic example to show that `head` terminates `yes`.
Expand Down
56 changes: 56 additions & 0 deletions examples/YesHeadYesExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env php
<?php
/**
* @title Tests two infintie commands sandwiched by a terminating command.
* @command yes | head | yes
* @expected y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n
*/

error_reporting(E_ALL);

include __DIR__ . '/../vendor/autoload.php';

use ElvenSpellmaker\PipeSys as PS;
use ElvenSpellmaker\PipeSys\Command as Command;

$c = new PS\Scheduler(new Command\StandardConnector);
$c->addCommand(new Command\Yes);
$c->addCommand(new Command\Head(1));
$c->addCommand(new Command\Yes);

$output = '';

if ($count = count($c->run(3)) !== 3)
{
$output .= "Expecting 3 elements, received $count\n";
}

$expected = [
0 => 'ElvenSpellmaker\PipeSys\Command\Yes',
2 => 'ElvenSpellmaker\PipeSys\Command\Yes',
];
$received = array_map('get_class', $c->run(1));

if ($received !== $expected)
{
echo '<pre>';
var_dump($expected, $received);
exit;

$output .= "Expecting the head to be missing!\n";
}

$expected = [2 => 'ElvenSpellmaker\PipeSys\Command\Yes'];
$received = array_map('get_class', $c->run(1));

if ($received !== $expected)
{
$output .= "Expecting the last yes to be present only!\n";
}

if (! count($c->run(5)))
{
$output .= "Command has terminated sometime before 10 runs!\n";
}

echo $output;
1 change: 1 addition & 0 deletions examples/YesYesHeadExample.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
* @title Tests two infinite commands and a terminating command.
Expand Down
35 changes: 33 additions & 2 deletions src/PipeSys/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Scheduler
*/
private $connector;

/**
* @var boolean
*/
private $started;

/**
* @param ConnectorInterface $connector The connector to connect commands
* together.
Expand All @@ -47,12 +52,26 @@ public function addCommand(AbstractCommand $command)
/**
* Runs all the commands added to the scheduler.
*/
public function run()
public function run($runs = true)
{
$this->connector->connect($this->commands);
if ($runs !== true && ! is_integer($runs))
{
$runs = (int)$runs;
}

if (! $this->started)
{
$this->started = true;
$this->connector->connect($this->commands);
}

while (count($this->commands))
{
if ($runs-- <= 0)
{
break;
}

foreach ($this->commands as $key => $command)
{
$result = $command->runOnce();
Expand All @@ -63,5 +82,17 @@ public function run()
}
}
}

return $this->commands;
}

/**
* Has the scheduler already been run before?
*
* @return boolean
*/
public function isStarted()
{
return $this->started;
}
}
Empty file modified tests/TestRig.php
100644 → 100755
Empty file.

0 comments on commit 70c41d7

Please sign in to comment.