AsyncTask::setResult(): permit returning ThreadSafe objects to the main thread

this is now supported thanks to the object rescue feature implemented in pthreads 5.1, making returning of thread-safe values from async tasks possible.
This needs to be explicitly supported, since otherwise it will attempt to serialize them, which isn't supported anymore.
This commit is contained in:
Dylan K. Taylor
2023-05-21 16:37:41 +01:00
parent fdb724c646
commit 69273f3ff7
3 changed files with 77 additions and 3 deletions

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use PHPUnit\Framework\TestCase;
use pmmp\thread\ThreadSafeArray;
use pocketmine\promise\PromiseResolver;
use pocketmine\snooze\SleeperHandler;
use pocketmine\utils\MainLogger;
use function define;
@ -69,4 +71,21 @@ class AsyncPoolTest extends TestCase{
}
self::assertTrue(PublishProgressRaceAsyncTask::$success, "Progress was not reported before task completion");
}
public function testThreadSafeSetResult() : void{
$resolver = new PromiseResolver();
$resolver->getPromise()->onCompletion(
function(ThreadSafeArray $result) : void{
self::assertCount(1, $result);
self::assertSame(["foo"], (array) $result);
},
function() : void{
self::fail("Promise failed");
}
);
$this->pool->submitTask(new ThreadSafeResultAsyncTask($resolver));
while($this->pool->collectTasks()){
usleep(50 * 1000);
}
}
}