upscale/swoole-launchpad

Swoole server process management

2.1.0 2023-06-12 19:08 UTC

This package is auto-updated.

Last update: 2024-12-12 22:44:59 UTC


README

This library extends the process management capabilities of Swoole / Open Swoole.

Features:

  • Swoole server launch in child process
  • Swoole server process termination
  • PHPUnit testing framework compatibility

Installation

The library is to be installed via Composer as a dependency:

composer require upscale/swoole-launchpad

Usage

PHPUnit Tests

The library is particularly useful in PHPUnit-based automated tests:

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);
    }
}

More compact version:

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);
    }
}

Make sure to autoload the test classes in your 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/"
        }
    }
}

Contributing

Pull Requests with fixes and improvements are welcome!

License

Copyright © Upscale Software. All rights reserved.

Licensed under the Apache License, Version 2.0.