Skip to content

behavior tests #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- master
- feature/tests
- feature/feature-tests
pull_request:
schedule:
- cron: '0 0 * * *'
Expand All @@ -29,7 +30,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json
extensions: json, posix, pcntl
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: xdebug
Expand Down
71 changes: 43 additions & 28 deletions tests/Feature/UdpConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
<?php
//example from manual
use Workerman\Connection\AsyncUdpConnection;
use Workerman\Timer;

use Symfony\Component\Process\PhpProcess;
use Workerman\Worker;

it('tests udp connection', function () {
/** @noinspection PhpObjectFieldsAreOnlyWrittenInspection */
$server = new Worker('udp://0.0.0.0:9292');
$server->onMessage = function ($connection, $data) {
expect($data)->toBe('hello');
$connection->send('xiami');
};
$server->onWorkerStart = function () {
//client
Timer::add(1, function () {
$client = new AsyncUdpConnection('udp://127.0.0.1:1234');
$client->onConnect = function ($client) {
$client->send('hello');
};
$client->onMessage = function ($client, $data) {
expect($data)->toBe('xiami');
//terminal this test
terminate_current_test();
};
$client->connect();
}, null, false);
};
Worker::runAll();
})->skipOnWindows() //require posix, multiple workers
->skip(message: 'this test needs to run isolated process while pest not support doing so yet');
$serverAddress = 'udp://127.0.0.1:6789';
beforeAll(function () use ($serverAddress) {
$process = new PhpProcess(<<<PHP
<?php
if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'w'));
if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'w'));
require './vendor/autoload.php';
use Workerman\Worker;

\$server = new Worker('$serverAddress');
\$server->onMessage = function (\$connection, \$data) {
if(str_starts_with(\$data, 'bye')) {
terminate_current_process();
}
\$connection->send('received: '.\$data);
};
global \$argv;
\$argv = ['', 'start'];
Worker::runAll();
PHP
);
$process->start();
sleep(5);
});

afterAll(function () use ($serverAddress) {
$socket = stream_socket_client(self::$serverAddress, timeout: 1);
fwrite($socket, 'bye');
fclose($socket);
});

it('tests udp connection', function () use ($serverAddress) {
$socket = stream_socket_client($serverAddress, $errno, $errstr, 1);
expect($errno)->toBeInt(0);
fwrite($socket, 'xiami');
$data = fread($socket, 1024);
expect($data)->toBeString('received: xiami');
fclose($socket);
})
->skipOnWindows(); //require posix
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function testWithConnectionClose(Closure $closure, string $dataContains = null,
}
}

function terminate_current_test()
function terminate_current_process()
{
posix_kill(posix_getppid(), SIGINT);
}