1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/test/Sync/ThreadedParcelTest.php

38 lines
917 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\Loop;
use Amp\Parallel\Context\Thread;
use Amp\Parallel\Sync\Channel;
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());
});
}
}