mirror of
https://github.com/danog/parallel.git
synced 2024-12-04 18:47:50 +01:00
852f580915
Channel is now passed as the first argument.
35 lines
905 B
PHP
35 lines
905 B
PHP
<?php
|
|
|
|
namespace Amp\Parallel\Test\Sync;
|
|
|
|
use Amp\Loop;
|
|
use Amp\Parallel\Context\Thread;
|
|
use Amp\Parallel\Sync\Channel;
|
|
use Amp\Parallel\Sync\ThreadedParcel;
|
|
|
|
/**
|
|
* @requires extension pthreads
|
|
*/
|
|
class ThreadedParcelTest extends AbstractParcelTest {
|
|
protected function createParcel($value) {
|
|
return new ThreadedParcel($value);
|
|
}
|
|
|
|
public function testWithinThread() {
|
|
Loop::run(function () {
|
|
$value = 1;
|
|
$parcel = new ThreadedParcel($value);
|
|
|
|
$thread = Thread::spawn(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());
|
|
});
|
|
}
|
|
}
|