1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 10:38:30 +01:00
parallel/test/Sync/ThreadedParcelTest.php

36 lines
857 B
PHP
Raw Normal View History

2017-11-29 21:40:07 +01:00
<?php
namespace Amp\Parallel\Test\Sync;
2017-11-29 21:40:07 +01:00
use Amp\Parallel\Context\Thread;
use Amp\Parallel\Sync\Channel;
2019-08-27 19:17:41 +02:00
use Amp\Parallel\Sync\Parcel;
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
}
}