2017-11-29 21:40:07 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-29 22:01:32 +01:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2017-11-29 21:40:07 +01:00
|
|
|
|
2017-11-29 22:01:32 +01:00
|
|
|
use Amp\Parallel\Context\Thread;
|
2017-12-10 23:43:19 +01:00
|
|
|
use Amp\Parallel\Sync\Channel;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\Parallel\Sync\Parcel;
|
2017-11-29 22:01:32 +01:00
|
|
|
use Amp\Parallel\Sync\ThreadedParcel;
|
2017-11-29 21:40:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires extension pthreads
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
class ThreadedParcelTest extends AbstractParcelTest
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
protected function createParcel($value): Parcel
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
return new ThreadedParcel($value);
|
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testWithinThread()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$value = 1;
|
|
|
|
$parcel = new ThreadedParcel($value);
|
2017-11-29 21:40:07 +01:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$thread = yield Thread::run(function (Channel $channel, ThreadedParcel $parcel) {
|
|
|
|
$parcel->synchronized(function (int $value) {
|
|
|
|
return $value + 1;
|
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}, $parcel);
|
2017-11-29 21:40:07 +01:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->assertSame(0, yield $thread->join());
|
|
|
|
$this->assertSame($value + 1, yield $parcel->unwrap());
|
2017-11-29 21:40:07 +01:00
|
|
|
}
|
|
|
|
}
|