upscale/swoole-launchpad

Swoole 服务器进程管理

2.1.0 2023-06-12 19:08 UTC

This package is auto-updated.

Last update: 2024-09-12 21:57:35 UTC


README

该库扩展了 Swoole / Open Swoole 的进程管理功能。

功能

  • 在子进程中启动 Swoole 服务器
  • Swoole 服务器进程终止
  • PHPUnit 测试框架兼容性

安装

通过 Composer 将该库作为依赖项安装

composer require upscale/swoole-launchpad

用法

PHPUnit 测试

该库特别适用于基于 PHPUnit 的自动化测试

vendor/bin/phpunit --process-isolation
class HttpServerTest extends \PHPUnit\Framework\TestCase
{
    protected \Swoole\Http\Server $server;

    protected \Upscale\Swoole\Launchpad\ProcessManager $processManager;

    protected int $pid;

    protected function setUp(): void
    {
        $this->server = new \Swoole\Http\Server('127.0.0.1', 8080);
        $this->server->set([
            'log_file' => '/dev/null',
            'log_level' => 4,
            'worker_num' => 1,
        ]);
        
        $this->processManager = new \Upscale\Swoole\Launchpad\ProcessManager();
    }

    protected function tearDown(): void
    {
        $this->processManager->kill($this->pid);
    }

    public function testResponseStatus()
    {
        $this->server->on('request', function ($request, $response) {
            $response->status(404);
            $response->end();
        });
        $this->pid = $this->processManager->spawn($this->server);

        $result = `curl http://127.0.0.1:8080/ -s -i`;
        $this->assertStringStartsWith('HTTP/1.1 404 Not Found', $result);
    }
    
    public function testResponseBody()
    {
        $this->server->on('request', function ($request, $response) {
            $response->end('Success');
        });
        $this->pid = $this->processManager->spawn($this->server);

        $result = `curl http://127.0.0.1:8080/ -s -i`;
        $this->assertStringStartsWith('HTTP/1.1 200 OK', $result);
        $this->assertStringEndsWith('Success', $result);
    }
}

更紧凑的版本

class HttpServerTest extends \Upscale\Swoole\Launchpad\Tests\TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
    
        $this->server = new \Swoole\Http\Server('127.0.0.1', 8080);
        $this->server->set([
            'log_file' => '/dev/null',
            'log_level' => 4,
            'worker_num' => 1,
        ]);
    }

    public function testResponseStatus()
    {
        $this->server->on('request', function ($request, $response) {
            $response->status(404);
            $response->end();
        });
        $this->spawn($this->server);

        $result = $this->curl('http://127.0.0.1:8080/');
        $this->assertStringStartsWith('HTTP/1.1 404 Not Found', $result);
    }
    
    public function testResponseBody()
    {
        $this->server->on('request', function ($request, $response) {
            $response->end('Success');
        });
        $this->spawn($this->server);

        $result = $this->curl('http://127.0.0.1:8080/');
        $this->assertStringStartsWith('HTTP/1.1 200 OK', $result);
        $this->assertStringEndsWith('Success', $result);
    }
}

确保在您的 composer.json 中自动加载测试类

{
    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "upscale/swoole-launchpad": "^2.0"
    },
    "autoload-dev": {
        "psr-4": {
            "Upscale\\Swoole\\Launchpad\\Tests\\": "vendor/upscale/swoole-launchpad/tests/"
        }
    }
}

贡献

欢迎提交带有修复和改进的拉取请求!

许可证

版权所有 © Upscale Software。保留所有权利。

根据 Apache License, Version 2.0 许可。