-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathreset_concurrency_with_process.phpt
97 lines (88 loc) · 2.83 KB
/
reset_concurrency_with_process.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--TEST--
swoole_http_server: reset concurrency [SWOOLE_PROCESS]
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
use Swoole\Coroutine\Http\Client;
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Table;
use Swoole\Atomic;
use function Swoole\Coroutine\run;
const N = 64;
$counter = new Atomic(0);
$table = new Table(1024);
$table->column('pid', Table::TYPE_INT);
$table->create();
$pm = new SwooleTest\ProcessManager;
$pm->parentFunc = function () use ($pm) {
run(function () use ($pm) {
$n = N;
$coroutines = [];
while ($n--) {
$coroutines[] = go(function () use ($pm) {
$client = new Client('127.0.0.1', $pm->getFreePort());
$client->set(['timeout' => 10]);
Assert::eq($client->get('/'), false);
Assert::eq($client->getStatusCode(), SWOOLE_HTTP_CLIENT_ESTATUS_SERVER_RESET);
});
}
Co::sleep(0.1);
$pm->wait();
$client = new Client('127.0.0.1', $pm->getFreePort());
Assert::assert($client->get('/'));
$stats = json_decode($client->getBody());
Assert::eq($stats->concurrency, 1);
/**
* PROCESS 模式下 Worker 进程退出时连接不会被关闭,这与 BASE 模式不同,因此需要先关闭服务器,其他正在运行的协程才会获得返回值
*/
$pm->kill();
Co::join($coroutines);
echo "DONE\n";
});
};
$pm->childFunc = function () use ($pm, $counter, $table) {
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_PROCESS);
$http->set([
'worker_num' => 4,
'max_concurrency' => 160,
'log_file' => '/dev/null',
]);
$http->on('workerStart', function ($server, $wid) use ($pm, $table) {
if ($wid === 0) {
$pm->wakeup();
}
$pid = posix_getpid();
$table->set('worker_' . $wid, ['pid' => $pid]);
// echo "Worker #{$wid}(pid=$pid) is started\n";
});
$http->on('request', function (Request $request, Response $response) use ($http, $counter, $table) {
$c = $counter->add();
if ($c < N) {
Co::sleep(100);
} elseif ($c == N) {
$stats = $http->stats();
Assert::eq($stats['concurrency'], N);
$pid = posix_getpid();
foreach ($table as $val) {
if ($val['pid'] !== $pid) {
posix_kill($val['pid'], SIGKILL);
}
}
posix_kill($pid, SIGKILL);
} else {
$stats = $http->stats();
Assert::eq($stats['concurrency'], 1);
$response->end(json_encode($stats));
}
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECT--
DONE