1--TEST--
2swoole_socket_coro: server and client concurrency
3--SKIPIF--
4<?php require __DIR__ . '/../include/skipif.inc'; ?>
5--FILE--
6<?php
7require __DIR__ . '/../include/bootstrap.php';
8
9Swoole\Runtime::enableCoroutine();
10
11$port = get_one_free_port();
12go(function () use ($port) {
13    $socket = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
14    Assert::assert($socket->bind('127.0.0.1', $port));
15    Assert::assert($socket->listen(MAX_CONCURRENCY_MID));
16    $i = 0;
17    while ($conn = $socket->accept()) {
18        go(function () use ($socket, $conn, &$i) {
19            for ($n = MAX_REQUESTS; $n--;) {
20                $data = $conn->recv(tcp_length($conn->recv(tcp_type_length())));
21                Assert::same($data, "Hello Swoole Server #{$n}!");
22                $conn->send(tcp_pack("Hello Swoole Client #{$n}!"));
23            }
24            $conn->close();
25            if (++$i === MAX_CONCURRENCY_MID) {
26                $socket->close();
27                echo "DONE\n";
28            }
29        });
30    }
31});
32for ($c = MAX_CONCURRENCY_MID; $c--;) {
33    go(function () use ($port) {
34        $client = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, 0);
35        Assert::assert($client->connect('127.0.0.1', $port));
36        for ($n = MAX_REQUESTS; $n--;) {
37            $client->send(tcp_pack("Hello Swoole Server #{$n}!"));
38            $data = $client->recv(tcp_length($client->recv(tcp_type_length())));
39            Assert::same($data, "Hello Swoole Client #{$n}!");
40        }
41        $client->close();
42    });
43}
44
45?>
46--EXPECT--
47DONE
48