Skip to content

Commit

Permalink
Added http chunk test for Coroutine\Http\Client; Optimize write_func …
Browse files Browse the repository at this point in the history
…tests --filter=[unit]
  • Loading branch information
matyhtf committed Nov 18, 2024
1 parent c4b3271 commit 3562377
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/swoole_http_client_coro/http_chunk.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
swoole_http_client_coro: http chunk
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Event;

const N = 8;
$chunks = [];
$body = '';
$n = N;
while ($n--) {
$chunk = base64_encode(random_bytes(random_int(256, 4096)));
$body .= $chunk;
$chunks[] = $chunk;
}

$pm = new ProcessManager;
$pm->parentFunc = function ($pid) use ($pm, $chunks, $body) {
Co\run(function () use ($pm, $chunks, $body) {
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', $pm->getFreePort());
Assert::assert($cli->get('/'));
Assert::eq($cli->getBody(), $body);
});
$pm->kill();
echo "DONE\n";
};

$pm->childFunc = function () use ($pm, $chunks) {
Co\run(function () use ($pm, $chunks) {
Event::defer(function () use ($pm) {
$pm->wakeup();
});
$server = new Swoole\Coroutine\Http\Server('127.0.0.1', $pm->getFreePort());
$server->handle('/', function ($req, $resp) use ($server, $chunks) {
foreach ($chunks as $chunk) {
$resp->write($chunk);
usleep(mt_rand(10, 50) * 100);
}
});
$server->start();
});
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE
5 changes: 5 additions & 0 deletions tests/swoole_http_client_coro/write_func_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ swoole_http_client_coro: write func 1
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Event;

const N = 8;
$chunks = [];
$n = N;
Expand All @@ -30,6 +32,9 @@ $pm->parentFunc = function ($pid) use ($pm, $chunks) {

$pm->childFunc = function () use ($pm, $chunks) {
Co\run(function () use ($pm, $chunks) {
Event::defer(function () use ($pm) {
$pm->wakeup();
});
$server = new Swoole\Coroutine\Http\Server('127.0.0.1', $pm->getFreePort());
$server->handle('/', function ($req, $resp) use ($server, $chunks) {
foreach ($chunks as $chunk) {
Expand Down
5 changes: 5 additions & 0 deletions tests/swoole_http_client_coro/write_func_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ swoole_http_client_coro: write func 1
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Event;

const N = 16;
$chunks = [];
$n = N;
Expand Down Expand Up @@ -35,6 +37,9 @@ $pm->parentFunc = function ($pid) use ($pm, $chunks) {

$pm->childFunc = function () use ($pm, $chunks) {
Co\run(function () use ($pm, $chunks) {
Event::defer(function () use ($pm) {
$pm->wakeup();
});
$server = new Swoole\Coroutine\Http\Server('127.0.0.1', $pm->getFreePort());
$server->handle('/', function ($req, $resp) use ($server, $chunks) {
foreach ($chunks as $chunk) {
Expand Down

0 comments on commit 3562377

Please sign in to comment.