Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 17, 2024
1 parent fc9ba2c commit ddb758b
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: PHP Unit Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
php-version: [ '8.1', '8.2', '8.3', '8.4' ]
runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: posix, sockets, pcntl, openssl, parallel, curl, ev
env:
phpts: ts

- name: Install dependencies
run: composer install

- name: Run PHPUnit
run: ./vendor/bin/phpunit
99 changes: 99 additions & 0 deletions tests/WsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php declare(strict_types=1);


use Co\Net;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psc\Core\Coroutine\Promise;
use Psc\Core\WebSocket\Options;
use Psc\Core\WebSocket\Server\Connection;
use Psc\Utils\Output;

use function Co\cancelAll;
use function Co\defer;
use function Co\wait;

/**
* @Author cclilshy
* @Date 2024/8/15 14:49
*/
class WsTest extends TestCase
{
/**
* @Author cclilshy
* @Date 2024/8/15 14:49
* @return void
* @throws Throwable
*/
#[Test]
public function test_wsServer(): void
{
defer(function () {
for ($i = 0; $i < 10; $i++) {
try {
$this->wsTest()->await();
} catch (Throwable $exception) {
Output::error($exception->getMessage());
}
}

\gc_collect_cycles();
$baseMemory = \memory_get_usage();

for ($i = 0; $i < 10; $i++) {
try {
$this->wsTest()->await();
} catch (Throwable $exception) {
Output::error($exception->getMessage());
}
}

\gc_collect_cycles();
if ($baseMemory !== \memory_get_usage()) {
Output::warning('There may be a memory leak');
}
cancelAll();
$this->assertTrue(true);
});

$context = \stream_context_create([
'socket' => [
'so_reuseport' => 1,
'so_reuseaddr' => 1,
],
]);

$server = Net::WebSocket()->server(
'ws://127.0.0.1:8001/',
$context,
new Options(true, true)
);
$server->onMessage(static function (string $data, Connection $connection) {
$connection->send($data);
});
$server->listen();
wait();
}

/**
* @Author cclilshy
* @Date 2024/8/15 14:49
* @return Promise
*/
private function wsTest(): Promise
{
return \Co\promise(function ($r) {
$hash = \md5(\uniqid());
$client = Net::WebSocket()->connect('ws://127.0.0.1:8001/');
$client->onOpen(static function () use ($client, $hash) {
\Co\sleep(0.1);
$client->send($hash);
});

$client->onMessage(function (string $data) use ($hash, $r) {
$this->assertEquals($hash, $data);
$r();
});
});
}
}

0 comments on commit ddb758b

Please sign in to comment.