mirror of
https://github.com/danog/parallel.git
synced 2024-12-11 16:49:51 +01:00
165 lines
3.5 KiB
PHP
165 lines
3.5 KiB
PHP
<?php
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
use Icicle\Concurrent\Threading\Thread;
|
|
use Icicle\Coroutine;
|
|
use Icicle\Loop;
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
/**
|
|
* @group threading
|
|
* @requires extension pthreads
|
|
*/
|
|
class ThreadTest extends TestCase
|
|
{
|
|
public function testIsRunning()
|
|
{
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
usleep(100);
|
|
});
|
|
|
|
$this->assertFalse($thread->isRunning());
|
|
|
|
$thread->start();
|
|
|
|
$this->assertTrue($thread->isRunning());
|
|
|
|
yield $thread->join();
|
|
|
|
$this->assertFalse($thread->isRunning());
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
public function testKill()
|
|
{
|
|
$thread = new Thread(function () {
|
|
usleep(100);
|
|
});
|
|
|
|
$thread->start();
|
|
|
|
$thread->kill();
|
|
$this->assertFalse($thread->isRunning());
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Icicle\Concurrent\Exception\StatusError
|
|
*/
|
|
public function testStartWhileRunningThrowsError()
|
|
{
|
|
$thread = new Thread(function () {
|
|
usleep(100);
|
|
});
|
|
|
|
$thread->start();
|
|
$thread->start();
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Icicle\Concurrent\Exception\StatusError
|
|
*/
|
|
public function testStartMultipleTimesThrowsError()
|
|
{
|
|
Loop\loop();
|
|
|
|
$this->assertRunTimeBetween(function () {
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
sleep(1);
|
|
});
|
|
|
|
$thread->start();
|
|
yield $thread->join();
|
|
|
|
$thread->start();
|
|
yield $thread->join();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}, 2, 2.2);
|
|
}
|
|
|
|
public function testSpawnStartsThread()
|
|
{
|
|
Coroutine\create(function () {
|
|
$thread = Thread::spawn(function () {
|
|
usleep(100);
|
|
});
|
|
|
|
yield $thread->join();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Icicle\Concurrent\Exception\PanicError
|
|
*/
|
|
public function testExceptionInThreadPanics()
|
|
{
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
throw new \Exception('Exception in thread.');
|
|
});
|
|
|
|
$thread->start();
|
|
yield $thread->join();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
public function testJoinWaitsForChild()
|
|
{
|
|
Loop\loop();
|
|
|
|
$this->assertRunTimeBetween(function () {
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
sleep(1);
|
|
});
|
|
|
|
$thread->start();
|
|
yield $thread->join();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}, 1, 1.1);
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Icicle\Concurrent\Exception\StatusError
|
|
*/
|
|
public function testJoinWithoutStartThrowsError()
|
|
{
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
usleep(100);
|
|
});
|
|
|
|
yield $thread->join();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
public function testJoinResolvesWithThreadReturn()
|
|
{
|
|
Coroutine\create(function () {
|
|
$thread = new Thread(function () {
|
|
return 42;
|
|
});
|
|
|
|
$thread->start();
|
|
$this->assertEquals(42, (yield $thread->join()));
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
}
|