Skip to content
Permalink
Browse files

Add the test: use return value to finish task.

  • Loading branch information...
twose committed Mar 4, 2019
1 parent ba7a214 commit ef978b3b432615b0f392958145d999f4e66584eb
Showing with 67 additions and 0 deletions.
  1. +67 −0 tests/swoole_server/task/task_enable_coroutine_return.phpt
@@ -0,0 +1,67 @@
--TEST--
swoole_runtime: task enable coroutine with return value to finish
--SKIPIF--
<?php
require __DIR__ . '/../../include/skipif.inc';
skip_if_pdo_not_support_mysql8();
?>
--FILE--
<?php
require __DIR__ . '/../../include/bootstrap.php';
Swoole\Runtime::enableCoroutine();
$pm = new ProcessManager;
$pm->parentFunc = function (int $pid) use ($pm) {
for ($i = MAX_CONCURRENCY_LOW; $i--;) {
go(function () use ($pm) {
$ret = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}");
assert($ret === 'Hello Swoole!');
});
}
swoole_event_wait();
echo "DONE\n";
$pm->kill();
};
$pm->childFunc = function () use ($pm) {
$http = new swoole_http_server('127.0.0.1', $pm->getFreePort(), SERVER_MODE_RANDOM);
$http->set([
'log_file' => '/dev/null',
'task_worker_num' => 4,
'task_enable_coroutine' => true
]);
$http->on('request', function (swoole_http_request $request, swoole_http_response $response) use ($http) {
assert($response->detach());
$http->task($response->fd, -1, function ($server, $taskId, $data) {
list($fd, $data) = $data;
$response = swoole_http_response::create($fd);
$response->end($data);
});
});
$http->on('task', function (swoole_http_server $server, swoole_server_task $task) {
defer(function ($data) {
if (!empty($data)) {
list($task, $result) = $data;
$task->finish($result);
}
});
$fd = $task->data;
$pdo = new PDO(
"mysql:host=" . MYSQL_SERVER_HOST . ";dbname=" . MYSQL_SERVER_DB . ";charset=utf8",
MYSQL_SERVER_USER, MYSQL_SERVER_PWD
);
$stmt = $pdo->query('SELECT "Hello Swoole!"');
assert($stmt->execute());
$ret = $stmt->fetchAll(PDO::FETCH_COLUMN)[0];
return [$task, [$fd, $ret]];
});
$http->on('finish', function ($server, $taskId, $data) {
list($fd, $ret) = $data;
$response = swoole_http_response::create($fd);
$response->end($ret);
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE

0 comments on commit ef978b3

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.