1--TEST--
2swoole_socket_coro: peek and checkLiveness
3--SKIPIF--
4<?php require __DIR__ . '/../include/skipif.inc'; ?>
5--FILE--
6<?php
7require __DIR__ . '/../include/bootstrap.php';
8
9use Swoole\Coroutine;
10use Swoole\Coroutine\Socket;
11
12$pm = new ProcessManager;
13$pm->initRandomDataEx(MAX_CONCURRENCY_MID, 1, 1024);
14$pm->parentFunc = function () use ($pm) {
15    Coroutine\run(function () use ($pm) {
16        for ($c = MAX_CONCURRENCY_MID; $c--;) {
17            Coroutine::create(function () use ($pm, $c) {
18                $socket = new Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
19                Assert::true($socket->connect('127.0.0.1', $pm->getFreePort()));
20                $random = get_safe_random();
21                Assert::same($socket->sendAll($random), strlen($random));
22                for ($n = 100; $n--;) {
23                    Coroutine::sleep(0.001);
24                    $data = $socket->peek(strlen($random));
25                    if ($data === $random) {
26                        break;
27                    }
28                }
29                Assert::greaterThan($n, 0);
30                /* clean the socket buffer */
31                $socket->recv();
32                /* then we check the liveness */
33                Assert::false($socket->checkLiveness());
34            });
35        }
36    });
37    $pm->kill();
38    echo "DONE\n";
39};
40$pm->childFunc = function () use ($pm) {
41    $server = new Swoole\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
42    $server->on('WorkerStart', function () use ($pm) {
43        $pm->wakeup();
44    });
45    $server->on('Receive', function (Swoole\Server $server, int $fd, int $rid, string $data) use ($pm) {
46        $server->send($fd, $data);
47        $server->close($fd);
48    });
49    $server->start();
50};
51$pm->childFirst();
52$pm->run();
53?>
54--EXPECT--
55DONE
56