-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test case for when an Action throws an un-handled Exception
- Loading branch information
1 parent
323c176
commit 1204d2b
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |