-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
} | ||
} |