From 9c1b4fd1cdc445cd535d66ca67e58031d03daccf Mon Sep 17 00:00:00 2001 From: Covered123 <58715544+JavierLeon9966@users.noreply.github.com> Date: Mon, 19 Apr 2021 09:41:51 -0300 Subject: [PATCH] Added CancelTaskException (#4186) --- src/scheduler/CancelTaskException.php | 32 +++++++++++++ src/scheduler/Task.php | 2 + src/scheduler/TaskHandler.php | 2 + tests/phpunit/scheduler/TaskSchedulerTest.php | 48 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/scheduler/CancelTaskException.php create mode 100644 tests/phpunit/scheduler/TaskSchedulerTest.php diff --git a/src/scheduler/CancelTaskException.php b/src/scheduler/CancelTaskException.php new file mode 100644 index 000000000..a500c4d78 --- /dev/null +++ b/src/scheduler/CancelTaskException.php @@ -0,0 +1,32 @@ +timings->startTiming(); try{ $this->task->onRun(); + }catch(CancelTaskException $e){ + $this->cancel(); }finally{ $this->timings->stopTiming(); } diff --git a/tests/phpunit/scheduler/TaskSchedulerTest.php b/tests/phpunit/scheduler/TaskSchedulerTest.php new file mode 100644 index 000000000..bcb1ae97e --- /dev/null +++ b/tests/phpunit/scheduler/TaskSchedulerTest.php @@ -0,0 +1,48 @@ +scheduler = new TaskScheduler(); + } + + public function tearDown() : void{ + $this->scheduler->shutdown(); + } + + public function testCancel() : void{ + $task = $this->scheduler->scheduleTask(new ClosureTask(function() : void{ + throw new CancelTaskException(); + })); + $this->scheduler->mainThreadHeartbeat(0); + self::assertTrue($task->isCancelled(), "Task was not cancelled"); + } +}