Skip to content

Commit b28332c

Browse files
committed
Add tests for get_running_fork_id
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
1 parent ef8c2fd commit b28332c

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

tests/python/tests/coroutine/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
entry: script
2+
components:
3+
script:
4+
image: KPHP
5+
scope: Request
6+
args: {}
7+
links: {}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
function test_get_running_fork_id() {
4+
$f = function() {
5+
// 1. Non-"main" fork. Before std function which is coroutine
6+
$before = (int)get_running_fork_id();
7+
8+
sched_yield_sleep(0.01);
9+
10+
$nested_f = function () {
11+
// 2. Yet another non-"main" fork. Before std function which is coroutine
12+
$before = (int)get_running_fork_id();
13+
sched_yield_sleep(0.01);
14+
assert_fork_id($before);
15+
return null;
16+
};
17+
18+
assert_fork_id($before);
19+
$fut = fork($nested_f());
20+
assert_fork_id($before);
21+
wait($fut);
22+
assert_fork_id($before);
23+
return null;
24+
};
25+
$fut = fork($f());
26+
27+
// 3. "main" fork. Before std function which is coroutine
28+
$before = (int)get_running_fork_id();
29+
sched_yield_sleep(0.01);
30+
assert_fork_id($before);
31+
wait($fut);
32+
assert_fork_id($before);
33+
}
34+
35+
36+
function assert_fork_id(int $expected_fork_id) {
37+
$current_fork_id = (int)get_running_fork_id();
38+
if ($current_fork_id != $expected_fork_id) {
39+
critical_error("unexpected fork id, expected: " . $expected_fork_id . " got: " . $current_fork_id);
40+
}
41+
}
42+
43+
function main() {
44+
switch ($_SERVER["PHP_SELF"]) {
45+
case "/test_get_running_fork_id": {
46+
test_get_running_fork_id();
47+
return;
48+
}
49+
}
50+
51+
critical_error("unknown test");
52+
}
53+
54+
main();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
import pytest
3+
4+
from python.lib.testcase import WebServerAutoTestCase
5+
from python.lib.http_client import RawResponse
6+
7+
class TestErrors(WebServerAutoTestCase):
8+
@classmethod
9+
def extra_class_setup(cls):
10+
if not cls.should_use_k2():
11+
cls.web_server.update_options({
12+
"--workers-num": 1
13+
})
14+
15+
def test_get_running_fork_id(self):
16+
response = self.web_server.http_request(uri="/test_get_running_fork_id", method='GET')
17+
self.assertEqual(200, response.status_code)

0 commit comments

Comments
 (0)