mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-04 00:59:51 +00:00
Modernize TesterPlugin
This commit is contained in:
parent
dc51af8b66
commit
73f913e279
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -26,6 +26,8 @@ namespace pmmp\TesterPlugin;
|
|||||||
use pocketmine\event\Listener;
|
use pocketmine\event\Listener;
|
||||||
use pocketmine\event\server\CommandEvent;
|
use pocketmine\event\server\CommandEvent;
|
||||||
use pocketmine\plugin\PluginBase;
|
use pocketmine\plugin\PluginBase;
|
||||||
|
use pocketmine\scheduler\CancelTaskException;
|
||||||
|
use pocketmine\scheduler\ClosureTask;
|
||||||
use function array_shift;
|
use function array_shift;
|
||||||
|
|
||||||
class Main extends PluginBase implements Listener{
|
class Main extends PluginBase implements Listener{
|
||||||
@ -41,7 +43,18 @@ class Main extends PluginBase implements Listener{
|
|||||||
|
|
||||||
public function onEnable() : void{
|
public function onEnable() : void{
|
||||||
$this->getServer()->getPluginManager()->registerEvents($this, $this);
|
$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 = [];
|
$this->waitingTests = [];
|
||||||
}
|
}
|
||||||
@ -56,11 +69,7 @@ class Main extends PluginBase implements Listener{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrentTest() : ?Test{
|
private function startNextTest() : bool{
|
||||||
return $this->currentTest;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function startNextTest() : bool{
|
|
||||||
$this->currentTest = array_shift($this->waitingTests);
|
$this->currentTest = array_shift($this->waitingTests);
|
||||||
if($this->currentTest !== null){
|
if($this->currentTest !== null){
|
||||||
$this->getLogger()->notice("Running test #" . (++$this->currentTestNumber) . " (" . $this->currentTest->getName() . ")");
|
$this->getLogger()->notice("Running test #" . (++$this->currentTestNumber) . " (" . $this->currentTest->getName() . ")");
|
||||||
@ -71,7 +80,7 @@ class Main extends PluginBase implements Listener{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onTestCompleted(Test $test) : void{
|
private function onTestCompleted(Test $test) : void{
|
||||||
$message = "Finished test #" . $this->currentTestNumber . " (" . $test->getName() . "): ";
|
$message = "Finished test #" . $this->currentTestNumber . " (" . $test->getName() . "): ";
|
||||||
switch($test->getResult()){
|
switch($test->getResult()){
|
||||||
case Test::RESULT_OK:
|
case Test::RESULT_OK:
|
||||||
@ -97,7 +106,7 @@ class Main extends PluginBase implements Listener{
|
|||||||
$this->currentTest = null;
|
$this->currentTest = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onAllTestsCompleted() : void{
|
private function onAllTestsCompleted() : void{
|
||||||
$this->getLogger()->notice("All tests finished, stopping the server");
|
$this->getLogger()->notice("All tests finished, stopping the server");
|
||||||
$this->getServer()->shutdown();
|
$this->getServer()->shutdown();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user