From e3dec95b756545e8dc0075013e3fd35b7c043eb4 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 11 May 2020 11:26:56 +0100 Subject: [PATCH] Convert AsyncPool tests into PHPUnit tests --- tests/phpunit/scheduler/AsyncPoolTest.php | 74 +++++++++++++++++++ .../scheduler/LeakTestAsyncTask.php} | 29 +------- .../PublishProgressRaceAsyncTask.php | 40 ++++++++++ .../src/pmmp/TesterPlugin/Main.php | 5 +- .../AsyncTaskPublishProgressRaceTest.php | 69 ----------------- 5 files changed, 119 insertions(+), 98 deletions(-) create mode 100644 tests/phpunit/scheduler/AsyncPoolTest.php rename tests/{plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php => phpunit/scheduler/LeakTestAsyncTask.php} (59%) create mode 100644 tests/phpunit/scheduler/PublishProgressRaceAsyncTask.php delete mode 100644 tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php diff --git a/tests/phpunit/scheduler/AsyncPoolTest.php b/tests/phpunit/scheduler/AsyncPoolTest.php new file mode 100644 index 000000000..f2a521ead --- /dev/null +++ b/tests/phpunit/scheduler/AsyncPoolTest.php @@ -0,0 +1,74 @@ +mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog")); + $this->pool = new AsyncPool(2, 1024, new \BaseClassLoader(), $this->mainLogger); + } + + public function tearDown() : void{ + $this->pool->shutdown(); + $this->mainLogger->shutdown(); + $this->mainLogger->join(); + } + + public function testTaskLeak() : void{ + $start = microtime(true); + $this->pool->submitTask(new LeakTestAsyncTask()); + while(!LeakTestAsyncTask::$destroyed and microtime(true) < $start + 30){ + usleep(50 * 1000); + $this->pool->collectTasks(); + } + self::assertTrue(LeakTestAsyncTask::$destroyed, "Task was not destroyed after 30 seconds"); + } + + public function testPublishProgressRace() : void{ + $task = new PublishProgressRaceAsyncTask(); + $this->pool->submitTask($task); + while($this->pool->collectTasks()){ + usleep(50 * 1000); + } + self::assertTrue(PublishProgressRaceAsyncTask::$success, "Progress was not reported before task completion"); + } +} diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php b/tests/phpunit/scheduler/LeakTestAsyncTask.php similarity index 59% rename from tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php rename to tests/phpunit/scheduler/LeakTestAsyncTask.php index eba13c43c..942b751d7 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php +++ b/tests/phpunit/scheduler/LeakTestAsyncTask.php @@ -21,33 +21,12 @@ declare(strict_types=1); -namespace pmmp\TesterPlugin\tests; +namespace pocketmine\scheduler; -use pmmp\TesterPlugin\Test; -use pocketmine\scheduler\AsyncTask; +use function usleep; -class AsyncTaskMemoryLeakTest extends Test{ - - public function run(){ - $this->getPlugin()->getServer()->getAsyncPool()->submitTask(new TestAsyncTask()); - } - - public function tick(){ - if(TestAsyncTask::$destroyed === true){ - $this->setResult(Test::RESULT_OK); - } - } - - public function getName() : string{ - return "AsyncTask memory leak after completion"; - } - - public function getDescription() : string{ - return "Regression test for AsyncTasks objects not being destroyed after completion"; - } -} - -class TestAsyncTask extends AsyncTask{ +class LeakTestAsyncTask extends AsyncTask{ + /** @var bool */ public static $destroyed = false; public function onRun() : void{ diff --git a/tests/phpunit/scheduler/PublishProgressRaceAsyncTask.php b/tests/phpunit/scheduler/PublishProgressRaceAsyncTask.php new file mode 100644 index 000000000..6c70d2620 --- /dev/null +++ b/tests/phpunit/scheduler/PublishProgressRaceAsyncTask.php @@ -0,0 +1,40 @@ +publishProgress("hello"); + } + + public function onProgressUpdate($progress) : void{ + if($progress === "hello"){ + // thread local on main thread + self::$success = true; + } + } +} diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php index 7b40795b8..b28d8c7e6 100644 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php +++ b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php @@ -42,10 +42,7 @@ class Main extends PluginBase implements Listener{ $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getScheduler()->scheduleRepeatingTask(new CheckTestCompletionTask($this), 10); - $this->waitingTests = [ - new tests\AsyncTaskMemoryLeakTest($this), - new tests\AsyncTaskPublishProgressRaceTest($this) - ]; + $this->waitingTests = []; } public function onServerCommand(CommandEvent $event){ diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php b/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php deleted file mode 100644 index ea637a57d..000000000 --- a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php +++ /dev/null @@ -1,69 +0,0 @@ -getPlugin()->getServer()->getAsyncPool()->submitTask(new class($this) extends AsyncTask{ - private const TLS_KEY_TEST = "test"; - - private static $success = false; - - public function __construct(AsyncTaskPublishProgressRaceTest $t){ - $this->storeLocal(self::TLS_KEY_TEST, $t); - } - - public function onRun() : void{ - $this->publishProgress("hello"); - } - - public function onProgressUpdate($progress) : void{ - if($progress === "hello"){ - // thread local on main thread - self::$success = true; - } - } - - public function onCompletion() : void{ - /** @var AsyncTaskPublishProgressRaceTest $t */ - $t = $this->fetchLocal(self::TLS_KEY_TEST); - $t->setResult(self::$success ? Test::RESULT_OK : Test::RESULT_FAILED); - } - }); - } -}