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
|
|
|
|
|
|
|
use Amp\Loop;
|
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;
|
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
|
|
|
|
{
|
|
|
|
protected function createParcel($value)
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
return new ThreadedParcel($value);
|
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testWithinThread()
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
$parcel = new ThreadedParcel($value);
|
|
|
|
|
2018-10-06 17:10:02 +02:00
|
|
|
$thread = yield Thread::run(function (Channel $channel, ThreadedParcel $parcel) {
|
2017-11-29 21:40:07 +01:00
|
|
|
$parcel->synchronized(function (int $value) {
|
|
|
|
return $value + 1;
|
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}, $parcel);
|
|
|
|
|
|
|
|
$this->assertSame(0, yield $thread->join());
|
|
|
|
$this->assertSame($value + 1, yield $parcel->unwrap());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|