Skip to content

Commit

Permalink
1. 增加测试用例
Browse files Browse the repository at this point in the history
2. 增加对非super user的优先级设置抛出错误的忽略
  • Loading branch information
chaz6chez committed Jun 20, 2022
1 parent b84b425 commit 450cc44
Show file tree
Hide file tree
Showing 7 changed files with 561 additions and 14 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,22 @@ $p->wait(function(\WorkBunny\Process\Runtime $parent, int $status){

**注:作用范围为父Runtime的方法仅在父Runtime内有有效响应**

| 方法名 | 作用范围 | 是否产生分叉 | 描述 |
|:-------------:|:--------:|:------:|:-----------------------------:|
| fork() | 父Runtime || 分叉一个子Runtime |
| run() | 父Runtime || 快速分支N个子Runtime |
| wait() | 父Runtime | × | 监听所有子Runtime状态 |
| parent() | 父Runtime | × | 为父Runtime增加回调响应 |
| isChild() | 所有 | × | 判断是否是子Runtime |
| getId() | 所有 | × | 获取当前Runtime序号 |
| getPid() | 所有 | × | 获取当前RuntimePID |
| getPidMap() | 父Runtime | × | 获取所有子RuntimePID |
| number() | 父Runtime | × | 获取Runtime数量 or 产生子Runtime自增序号 |
| setPriority() | 所有 | × | 为当前Runtime设置优先级 |
| getPriority() | 所有 | × | 获取当前Runtime优先级 |
| 方法名 | 作用范围 | 是否产生分叉 | 描述 |
|:-------------:|:----------:|:------:|:---------------------------------------:|
| fork() | 父Runtime || 分叉一个子Runtime |
| run() | 父Runtime || 快速分叉N个子Runtime |
| wait() | 父Runtime | × | 监听所有子Runtime状态 |
| parent() | 父Runtime | × | 为父Runtime增加回调响应 |
| isChild() | 所有 | × | 判断是否是子Runtime |
| getId() | 所有 | × | 获取当前Runtime序号 |
| getPid() | 所有 | × | 获取当前RuntimePID |
| getPidMap() | 父Runtime | × | 获取所有子RuntimePID |
| number() | 父Runtime | × | 获取Runtime数量 or 产生子Runtime自增序号 |
| setConfig() | 所有 且 分叉发生前 | × | 设置config |
| getConfig() | 所有 | × | 获取config |
| getPidMap() | 父Runtime | × | 获取所有子RuntimePID |
| setPriority() | 所有 | × | 为当前Runtime设置优先级 **需要当前执行用户为super user** |
| getPriority() | 所有 | × | 获取当前Runtime优先级 |

# 说明

Expand Down
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false"
colors="true"
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="process test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
</phpunit>
12 changes: 11 additions & 1 deletion src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function __construct(array $config = [])
$this->_config = $config;
}

/**
* @see Runtime::__construct
* @param array $config
* @return void
*/
public function setConfig(array $config): void
{
$this->_config = $config;
}

/**
* @see Runtime::__construct
* @return array
Expand Down Expand Up @@ -228,7 +238,7 @@ public function fork(Closure $handler, int $priority = 0): void
public function setPriority(int $id, int $priority): void
{
if($this->getId() === $id){
pcntl_setpriority($priority);
@pcntl_setpriority($priority);
}
}

Expand Down
65 changes: 65 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);

namespace Tests;

use PHPUnit\Framework\TestCase;
use WorkBunny\Process\Runtime;

class BaseTestCase extends TestCase
{
protected ?Runtime $_runtime = null;

/**
* @return void
*/
public function setUp(): void
{
$this->_runtime = new Runtime();
parent::setUp();
}

/**
* @return Runtime|null
*/
public function runtime(): ?Runtime
{
return $this->_runtime;
}

public function write(string $file, string $content)
{
file_put_contents(__DIR__ . '/cache/' . $file, $content, FILE_APPEND|LOCK_EX);
}

public function read(string $file)
{
return file_exists($file = __DIR__ . '/cache/' . $file) ? file_get_contents($file) : '';
}

/**
* @return void
*/
public function removeCache(string $file)
{
if(file_exists($file = __DIR__ . '/cache/' . $file)){
@unlink($file);
}
}

public function assertEqualsAndRmCache($expected, $actual, string $file = ''): void
{
$this->removeCache($file);
$this->assertEquals($expected, $actual);
}

public function assertContainsHasAndRmCache(array $expected, array $actual, string $file = ''){
$this->removeCache($file);

$this->assertCount(count($expected), $actual);
foreach ($expected as $value){
$this->assertContains($value, $actual);
}
}

}
Loading

0 comments on commit 450cc44

Please sign in to comment.