public function testCommunication()
 {
     $process = new \Jenner\SimpleFork\Process(function () {
         $queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue();
         $queue->put('test');
     });
     $process->start();
     $process->wait();
     $queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue();
     $this->assertEquals($queue->size(), 1);
     $this->assertEquals($queue->get(), 'test');
 }
 public function testRemove()
 {
     $cache = new \Jenner\SimpleFork\Cache\SharedMemory();
     $cache->set('test', 'test');
     $process = new \Jenner\SimpleFork\Process(function () use($cache) {
         $cache->remove();
     });
     $this->assertEquals($cache->get('test'), 'test');
     $process->start();
     $process->wait();
     // maybe a php bug
     //$this->assertFalse($cache->get('test'));
 }
Пример #3
0
 public function testCommunication()
 {
     if (!extension_loaded("Redis")) {
         $this->markTestSkipped("Redis extension is not loaded");
     }
     $process = new \Jenner\SimpleFork\Process(function () {
         $queue = new \Jenner\SimpleFork\Queue\RedisQueue();
         $queue->put('test');
         $queue->close();
     });
     $process->start();
     $process->wait();
     $queue = new \Jenner\SimpleFork\Queue\RedisQueue();
     $this->assertEquals($queue->size(), 1);
     $this->assertEquals($queue->get(), 'test');
     $queue->close();
 }
Пример #4
0
 public function testBlock()
 {
     $pipe = new \Jenner\SimpleFork\Queue\Pipe();
     $pipe->setBlock(true);
     $process = new \Jenner\SimpleFork\Process(function () {
         $pipe = new \Jenner\SimpleFork\Queue\Pipe();
         sleep(2);
         $pipe->write('test');
         $pipe->close();
     });
     $start = time();
     $process->start();
     $this->assertEquals('test', $pipe->read(4));
     $end = time();
     $this->assertTrue($end - $start >= 2);
     $process->wait(true);
 }
Пример #5
0
 public function testCommunication()
 {
     if (version_compare(PHP_VERSION, '5.6.0') < 0) {
         $this->markTestSkipped("php version is too low");
     }
     $process = new \Jenner\SimpleFork\Process(function () {
         $lock = \Jenner\SimpleFork\Lock\Semaphore::create('test');
         $lock->acquire(false);
         sleep(5);
         $lock->release();
     });
     $process->start();
     sleep(3);
     $lock = \Jenner\SimpleFork\Lock\Semaphore::create("test");
     $this->assertFalse($lock->acquire(false));
     $process->wait();
     $this->assertTrue($lock->acquire(false));
     $this->assertTrue($lock->release());
 }
Пример #6
0
 public function testCommunication()
 {
     $lock_file = "/tmp/simple-fork.lock";
     if (!file_exists($lock_file)) {
         touch($lock_file);
     }
     $process = new \Jenner\SimpleFork\Process(function () use($lock_file) {
         $lock = \Jenner\SimpleFork\Lock\FileLock::create($lock_file);
         $lock->acquire(false);
         sleep(5);
         $lock->release();
     });
     $process->start();
     sleep(3);
     $lock = \Jenner\SimpleFork\Lock\FileLock::create($lock_file);
     $this->assertFalse($lock->acquire(false));
     $process->wait();
     $this->assertTrue($lock->acquire(false));
     $this->assertTrue($lock->release());
 }
Пример #7
0
 public function testShutdown()
 {
     $process = new \Jenner\SimpleFork\Process(function () {
         sleep(3);
     });
     $time = time();
     $process->start();
     $process->shutdown(SIGKILL);
     $this->assertFalse($process->isRunning());
     $this->assertTrue(time() - $time < 3);
     $this->assertTrue($process->ifSignal());
     $this->assertEquals(0, $process->errno());
 }
Пример #8
0
<?php

/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/12
 * Time: 19:09
 */
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable implements \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        echo "I am a sub process" . PHP_EOL;
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->start();
$process->wait();
Пример #9
0
<?php

/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/13
 * Time: 9:22
 */
declare (ticks=1);
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable implements \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        while (true) {
            echo "I am running" . PHP_EOL;
            sleep(1);
        }
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->start();
sleep(5);
$process->shutdown();
Пример #10
0
<?php

/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/13
 * Time: 9:22
 */
declare (ticks=1);
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable extends \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        while (true) {
            echo "I am running" . PHP_EOL;
            sleep(1);
        }
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->start();
sleep(5);
$process->stop();
Пример #11
0
<?php

/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/13
 * Time: 9:53
 */
declare (ticks=1);
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable implements \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        while (true) {
            echo "I am running" . PHP_EOL;
            sleep(1);
        }
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->on(\Jenner\SimpleFork\Process::BEFORE_EXIT, function () {
    return true;
});
$process->start();
sleep(5);
$process->shutdown();
Пример #12
0
/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/20
 * Time: 14:37
 */
declare (ticks=1);
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable implements \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        while (true) {
            echo "I am running" . PHP_EOL;
            sleep(1);
        }
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->on(\Jenner\SimpleFork\Process::BEFORE_START, function () {
    echo "start" . PHP_EOL;
    return true;
});
$process->start();
sleep(5);
$process->shutdown();
Пример #13
0
<?php

/**
 * Created by PhpStorm.
 * User: Jenner
 * Date: 2015/8/12
 * Time: 19:09
 */
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
class TestRunnable extends \Jenner\SimpleFork\Runnable
{
    /**
     * 进程执行入口
     * @return mixed
     */
    public function run()
    {
        echo "I am a sub process" . PHP_EOL;
    }
}
$process = new \Jenner\SimpleFork\Process(new TestRunnable());
$process->start();