Modernize TesterPlugin

This commit is contained in:
Dylan K. Taylor 2021-04-19 14:16:05 +01:00
parent dc51af8b66
commit 73f913e279
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 17 additions and 58 deletions

View File

@ -1,50 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pmmp\TesterPlugin;
use pocketmine\scheduler\Task;
class CheckTestCompletionTask extends Task{
/** @var Main */
private $plugin;
public function __construct(Main $plugin){
$this->plugin = $plugin;
}
public function onRun() : void{
$test = $this->plugin->getCurrentTest();
if($test === null){
if(!$this->plugin->startNextTest()){
$this->getHandler()->cancel();
$this->plugin->onAllTestsCompleted();
}
}elseif($test->isFinished() or $test->isTimedOut()){
$this->plugin->onTestCompleted($test);
}else{
$test->tick();
}
}
}

View File

@ -26,6 +26,8 @@ namespace pmmp\TesterPlugin;
use pocketmine\event\Listener;
use pocketmine\event\server\CommandEvent;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\CancelTaskException;
use pocketmine\scheduler\ClosureTask;
use function array_shift;
class Main extends PluginBase implements Listener{
@ -41,7 +43,18 @@ class Main extends PluginBase implements Listener{
public function onEnable() : void{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getScheduler()->scheduleRepeatingTask(new CheckTestCompletionTask($this), 10);
$this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function() : void{
if($this->currentTest === null){
if(!$this->startNextTest()){
$this->onAllTestsCompleted();
throw new CancelTaskException();
}
}elseif($this->currentTest->isFinished() || $this->currentTest->isTimedOut()){
$this->onTestCompleted($this->currentTest);
}else{
$this->currentTest->tick();
}
}), 10);
$this->waitingTests = [];
}
@ -56,11 +69,7 @@ class Main extends PluginBase implements Listener{
}
}
public function getCurrentTest() : ?Test{
return $this->currentTest;
}
public function startNextTest() : bool{
private function startNextTest() : bool{
$this->currentTest = array_shift($this->waitingTests);
if($this->currentTest !== null){
$this->getLogger()->notice("Running test #" . (++$this->currentTestNumber) . " (" . $this->currentTest->getName() . ")");
@ -71,7 +80,7 @@ class Main extends PluginBase implements Listener{
return false;
}
public function onTestCompleted(Test $test) : void{
private function onTestCompleted(Test $test) : void{
$message = "Finished test #" . $this->currentTestNumber . " (" . $test->getName() . "): ";
switch($test->getResult()){
case Test::RESULT_OK:
@ -97,7 +106,7 @@ class Main extends PluginBase implements Listener{
$this->currentTest = null;
}
public function onAllTestsCompleted() : void{
private function onAllTestsCompleted() : void{
$this->getLogger()->notice("All tests finished, stopping the server");
$this->getServer()->shutdown();
}