|
| 1 | +--TEST-- |
| 2 | +swoole_thread/server: listen |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +require __DIR__ . '/../../include/skipif.inc'; |
| 6 | +skip_if_nts(); |
| 7 | +?> |
| 8 | +--FILE-- |
| 9 | +<?php |
| 10 | +require __DIR__ . '/../../include/bootstrap.php'; |
| 11 | +require __DIR__ . '/functions.inc'; |
| 12 | + |
| 13 | +use Swoole\Thread; |
| 14 | + |
| 15 | +$port = get_constant_port(__FILE__); |
| 16 | + |
| 17 | +$serv = new Swoole\Server('127.0.0.1', $port + 1, SWOOLE_THREAD); |
| 18 | +$port2 = $serv->listen('127.0.0.1', $port, SWOOLE_SOCK_TCP); |
| 19 | +$serv->set(array( |
| 20 | + 'worker_num' => 2, |
| 21 | + 'log_level' => SWOOLE_LOG_ERROR, |
| 22 | + 'open_eof_check' => true, |
| 23 | + 'package_eof' => "\r\n", |
| 24 | + 'init_arguments' => function () { |
| 25 | + global $queue, $atomic; |
| 26 | + $queue = new Swoole\Thread\Queue(); |
| 27 | + $atomic = new Swoole\Thread\Atomic(1); |
| 28 | + return [$queue, $atomic]; |
| 29 | + } |
| 30 | +)); |
| 31 | +$serv->on('WorkerStart', function (Swoole\Server $serv, $workerId) use ($port) { |
| 32 | + [$queue, $atomic] = Thread::getArguments(); |
| 33 | + if ($workerId == 0) { |
| 34 | + $queue->push("begin\n", Thread\Queue::NOTIFY_ALL); |
| 35 | + } |
| 36 | +}); |
| 37 | +$serv->on('receive', function (Swoole\Server $serv, $fd, $rid, $data) { |
| 38 | + $json = json_decode(rtrim($data)); |
| 39 | + if ($json->type == 'eof') { |
| 40 | + $serv->send($fd, "EOF\r\n"); |
| 41 | + } |
| 42 | +}); |
| 43 | +$serv->on('shutdown', function () { |
| 44 | + global $queue, $atomic; |
| 45 | + echo 'shutdown', PHP_EOL; |
| 46 | + Assert::eq($atomic->get(), 0); |
| 47 | +}); |
| 48 | +$serv->addProcess(new Swoole\Process(function ($process) use ($serv) { |
| 49 | + [$queue, $atomic] = Thread::getArguments(); |
| 50 | + global $port; |
| 51 | + echo $queue->pop(-1); |
| 52 | + Co\run(function () use ($port) { |
| 53 | + Co::join([ |
| 54 | + Co\go(function () use ($port) { |
| 55 | + thread_server_test_eof_client($port); |
| 56 | + }), |
| 57 | + Co\go(function () use ($port) { |
| 58 | + thread_server_test_eof_client($port + 1); |
| 59 | + }) |
| 60 | + ]); |
| 61 | + }); |
| 62 | + $atomic->set(0); |
| 63 | + echo "done\n"; |
| 64 | + $serv->shutdown(); |
| 65 | +})); |
| 66 | +$serv->start(); |
| 67 | +?> |
| 68 | +--EXPECT-- |
| 69 | +begin |
| 70 | +done |
| 71 | +shutdown |
0 commit comments