Skip to content

Commit

Permalink
Add a test case for when an Action throws an un-handled Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnetau committed Oct 13, 2021
1 parent 323c176 commit 1204d2b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Actions/exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

use EdgeTelemetrics\EventCorrelation\Library\Actions\ActionHelper;
use EdgeTelemetrics\JSON_RPC\Request as JsonRpcRequest;

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

/**
* Echo Action
* This action will log to the Scheduler the requested parameters
*/
new class() {
/**
* @var ActionHelper
*/
protected ActionHelper $processWrap;

public function __construct()
{
/** Initialise the Action Helper, this will handle the stdin/stdout for the process and also any signals */
$this->processWrap = new ActionHelper();

$this->processWrap->on(ActionHelper::ACTION_EXECUTE, function(JsonRpcRequest $rpc) {
throw new RuntimeException('Exception test case');
});

/** We have been requested to shut down, this is where we can perform any flushing actions before stopping. We let the Action Helper know we are done by calling stop() **/
$this->processWrap->on(ActionHelper::ACTION_SHUTDOWN, function() {
$this->processWrap->stop();
});
}
};
54 changes: 54 additions & 0 deletions tests/test_action_fatal_exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

/**
* Test: Unhandled exception thrown in action
*/
use Bref\Logger\StderrLogger;
use EdgeTelemetrics\EventCorrelation\Action;
use EdgeTelemetrics\EventCorrelation\Rule;
use EdgeTelemetrics\EventCorrelation\Scheduler;
use Psr\Log\LogLevel;

use function EdgeTelemetrics\EventCorrelation\php_cmd;

require __DIR__ . "/../vendor/autoload.php";

class TriggerException extends Rule {

const EVENTS = [[self::EVENT_MATCH_ANY]];

public function fire()
{
$action = new Action("exception", $this->consumedEvents[0]);
$this->emit('data', [$action]);
}
}

$rules = [
TriggerException::class,
];

define('STATE_FILE', tempnam(sys_get_temp_dir(), 'php-ec-test.'));

(new class($rules) extends Scheduler {
public function __construct(array $rules)
{
parent::__construct($rules);
set_exception_handler([$this, "handle_exception"]);
$this->setLogger(new StderrLogger(LogLevel::DEBUG));

$this->register_input_process('single_event', php_cmd(__DIR__ . "/Sources/single_event.php"));

$this->register_action('exception', php_cmd(__DIR__ . "/actions/exception.php"));

$this->setSavefileName(STATE_FILE);
$this->setSaveStateInterval(1);
}

public function handle_exception(Throwable $exception) {
$this->logger->emergency("Fatal", ['exception' => $exception,]);
}
})->run();

$state = json_decode(file_get_contents(STATE_FILE), true);
echo json_encode($state, JSON_PRETTY_PRINT);

0 comments on commit 1204d2b

Please sign in to comment.