mirror of
https://github.com/danog/parallel.git
synced 2024-12-02 09:37:57 +01:00
36 lines
857 B
PHP
36 lines
857 B
PHP
<?php
|
|
|
|
namespace Amp\Parallel\Test\Sync;
|
|
|
|
use Amp\Parallel\Context\Thread;
|
|
use Amp\Parallel\Sync\Channel;
|
|
use Amp\Parallel\Sync\Parcel;
|
|
use Amp\Parallel\Sync\ThreadedParcel;
|
|
|
|
/**
|
|
* @requires extension pthreads
|
|
*/
|
|
class ThreadedParcelTest extends AbstractParcelTest
|
|
{
|
|
protected function createParcel($value): Parcel
|
|
{
|
|
return new ThreadedParcel($value);
|
|
}
|
|
|
|
public function testWithinThread()
|
|
{
|
|
$value = 1;
|
|
$parcel = new ThreadedParcel($value);
|
|
|
|
$thread = yield Thread::run(function (Channel $channel, ThreadedParcel $parcel) {
|
|
$parcel->synchronized(function (int $value) {
|
|
return $value + 1;
|
|
});
|
|
return 0;
|
|
}, $parcel);
|
|
|
|
$this->assertSame(0, yield $thread->join());
|
|
$this->assertSame($value + 1, yield $parcel->unwrap());
|
|
}
|
|
}
|