From 83bcfaf42687cb8b9afc33209e163bcf6887c9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 8 Aug 2018 15:20:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RpcClient=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=85=8D=E7=BD=AE=E7=9A=84=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20(https://github.com/swoft-cloud/swoft-component/pul?= =?UTF-8?q?l/152)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加测试使用的配置 * 完善单测配置 * 完善单测 * 增加components配置 * 增加大文本返回时,client正确接收的单测 * 允许用户在tcp.client中自定义client的配置 * 测试配置,增加open_eof_split * Update server.php * 更改组件加载配置 * 更改组件加载配置 * Update ServiceConnection.php --- .travis.yml | 2 + composer.json | 10 +++- src/Service/ServiceConnection.php | 13 +++++ src/Service/ServiceProxy.php | 4 +- test/.env | 2 + test/Cases/AbstractTestCase.php | 8 +++- test/Cases/DemoTest.php | 26 ++++++++++ test/Testing/Breaker/ServiceBreaker.php | 47 +++++++++++++++++++ test/Testing/Clients/DemoServiceClient.php | 26 ++++++++++ test/Testing/Fallback/DemoServiceFallback.php | 27 +++++++++++ test/Testing/Lib/DemoServiceInterface.php | 17 +++++++ .../Pool/Config/DemoServicePoolConfig.php | 20 ++++++++ test/Testing/Pool/DemoServicePool.php | 21 +++++++++ test/Testing/Services/DemoService.php | 30 ++++++++++++ test/bootstrap.php | 4 +- test/config/properties/app.php | 8 +++- test/config/server.php | 28 +++++++---- test/server.php | 9 ++++ 18 files changed, 287 insertions(+), 15 deletions(-) create mode 100644 test/Testing/Breaker/ServiceBreaker.php create mode 100644 test/Testing/Clients/DemoServiceClient.php create mode 100644 test/Testing/Fallback/DemoServiceFallback.php create mode 100644 test/Testing/Lib/DemoServiceInterface.php create mode 100644 test/Testing/Pool/Config/DemoServicePoolConfig.php create mode 100644 test/Testing/Pool/DemoServicePool.php create mode 100644 test/Testing/Services/DemoService.php create mode 100644 test/server.php diff --git a/.travis.yml b/.travis.yml index 65287248..eb2488cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,5 +14,7 @@ install: before_script: - phpenv config-rm xdebug.ini - composer update + - php test/server.php & + - sleep 10 script: composer test diff --git a/composer.json b/composer.json index 2e070d7c..5a12e9ed 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,12 @@ }, "require-dev": { "swoft/swoole-ide-helper": "dev-master", - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "swoft/rpc": "^1.0", + "swoft/rpc-server": "^1.0", + "swoft/task": "^1.0", + "swoft/process": "^1.0", + "swoft/service-governance": "^1.0" }, "autoload": { "psr-4": { @@ -23,7 +28,8 @@ }, "autoload-dev": { "psr-4": { - "SwoftTest\\Rpc\\Client\\": "test/Cases" + "SwoftTest\\Rpc\\Client\\": "test/Cases/", + "SwoftTest\\Rpc\\Testing\\": "test/Testing/" } }, "repositories": [ diff --git a/src/Service/ServiceConnection.php b/src/Service/ServiceConnection.php index 1ef6eedb..d35e8bae 100644 --- a/src/Service/ServiceConnection.php +++ b/src/Service/ServiceConnection.php @@ -25,6 +25,9 @@ public function createConnection() $address = $this->pool->getConnectionAddress(); $timeout = $this->pool->getTimeout(); + $setting = $this->getTcpClientSetting(); + $setting && $client->set($setting); + list($host, $port) = explode(':', $address); if (!$client->connect($host, $port, $timeout)) { $error = sprintf('Service connect fail errorCode=%s host=%s port=%s', $client->errCode, $host, $port); @@ -76,4 +79,14 @@ public function recv(): string { return $this->connection->recv(); } + + /** + * 返回TCP客户端的配置 + * + * @return array + */ + public function getTcpClientSetting(): array + { + return App::getAppProperties()->get('server.tcp.client', []); + } } diff --git a/src/Service/ServiceProxy.php b/src/Service/ServiceProxy.php index 9a462736..9578eb63 100644 --- a/src/Service/ServiceProxy.php +++ b/src/Service/ServiceProxy.php @@ -22,7 +22,9 @@ public static function loadProxyClass(string $className, string $interfaceClass) $template .= self::getMethodsTemplate($reflectionMethods); $template .= "}"; - eval($template); + if (!class_exists($className)) { + eval($template); + } } /** diff --git a/test/.env b/test/.env index c0280f47..fcdcbb56 100644 --- a/test/.env +++ b/test/.env @@ -48,3 +48,5 @@ PROVIDER_CONSUL_ADDRESS=http://127.0.0.1:82 PROVIDER_CONSUL_TAGS=1,2 PROVIDER_CONSUL_TIMEOUT=2 PROVIDER_CONSUL_INTERVAL=2 + +TCP_OPEN_EOF_CHECK=true diff --git a/test/Cases/AbstractTestCase.php b/test/Cases/AbstractTestCase.php index 53bda7b0..30ed2e35 100644 --- a/test/Cases/AbstractTestCase.php +++ b/test/Cases/AbstractTestCase.php @@ -13,5 +13,11 @@ */ class AbstractTestCase extends TestCase { - + protected function tearDown() + { + parent::tearDown(); + swoole_timer_after(6 * 1000, function () { + swoole_event_exit(); + }); + } } \ No newline at end of file diff --git a/test/Cases/DemoTest.php b/test/Cases/DemoTest.php index c2305d56..60bdd5bc 100644 --- a/test/Cases/DemoTest.php +++ b/test/Cases/DemoTest.php @@ -2,10 +2,36 @@ namespace SwoftTest\Rpc\Client; +use Swoft\Rpc\Client\Bean\Collector\ReferenceCollector; +use Swoft\Rpc\Client\Service\ServiceProxy; +use SwoftTest\Rpc\Testing\Clients\DemoServiceClient; +use SwoftTest\Rpc\Testing\Lib\DemoServiceInterface; +use SwoftTest\Rpc\Testing\Pool\Config\DemoServicePoolConfig; + class DemoTest extends AbstractTestCase { public function testDemo() { $this->assertTrue(true); } + + public function testVersion() + { + $client = bean(DemoServiceClient::class); + $this->assertEquals('1.0.0', $client->version()); + } + + public function testLongMessageByCo() + { + go(function () { + $client = bean(DemoServiceClient::class); + $string = 'Hi, Agnes! '; + $str = $client->longMessage($string); + $expect = ''; + for ($i = 0; $i < 50000; $i++) { + $expect .= $string; + } + $this->assertEquals($expect, $str); + }); + } } \ No newline at end of file diff --git a/test/Testing/Breaker/ServiceBreaker.php b/test/Testing/Breaker/ServiceBreaker.php new file mode 100644 index 00000000..355ba8da --- /dev/null +++ b/test/Testing/Breaker/ServiceBreaker.php @@ -0,0 +1,47 @@ +demoService->$name(...$arguments); + } +} diff --git a/test/Testing/Fallback/DemoServiceFallback.php b/test/Testing/Fallback/DemoServiceFallback.php new file mode 100644 index 00000000..4f962120 --- /dev/null +++ b/test/Testing/Fallback/DemoServiceFallback.php @@ -0,0 +1,27 @@ +init(); \ No newline at end of file +$initApplicationContext->init(); + +$server = new \Swoft\Rpc\Server\Rpc\RpcServer(); \ No newline at end of file diff --git a/test/config/properties/app.php b/test/config/properties/app.php index 518ab2d3..c8c640aa 100644 --- a/test/config/properties/app.php +++ b/test/config/properties/app.php @@ -3,7 +3,8 @@ "version" => '1.0', 'autoInitBean' => true, 'beanScan' => [ - 'Swoft\\Rpc\\Client\\Testing' => BASE_PATH."/Testing" + 'SwoftTest\\Rpc\\Testing' => BASE_PATH . "/Testing", + 'Swoft\\Rpc\\Client' => BASE_PATH . '/../src', ], 'I18n' => [ 'sourceLanguage' => '@root/resources/messages/', @@ -15,5 +16,10 @@ 'timeout' => 3000 ] ], + 'components' => [ + 'custom' => [ + 'Swoft\\Rpc\\Client' => BASE_PATH . '/../src', + ], + ], 'cache' => require dirname(__FILE__) . DS . "cache.php", ]; diff --git a/test/config/server.php b/test/config/server.php index b83d5526..73b0810d 100644 --- a/test/config/server.php +++ b/test/config/server.php @@ -10,15 +10,23 @@ 'tcp' => [ 'host' => env('TCP_HOST', '0.0.0.0'), 'port' => env('TCP_PORT', 8099), - 'mode' => env('TCP_MODE', SWOOLE_PROCESS), + 'mode' => env('TCP_MODE', SWOOLE_PROCESS), 'type' => env('TCP_TYPE', SWOOLE_SOCK_TCP), 'package_max_length' => env('TCP_PACKAGE_MAX_LENGTH', 2048), 'open_eof_check' => env('TCP_OPEN_EOF_CHECK', false), + 'open_eof_split' => env('TCP_OPEN_EOF_SPLIT', true), + 'package_eof' => "\r\n", + 'client' => [ + 'package_max_length' => env('TCP_CLIENT_PACKAGE_MAX_LENGTH', 1024 * 1024 * 2), + 'open_eof_check' => env('TCP_CLIENT_OPEN_EOF_CHECK', false), + 'open_eof_split' => env('TCP_CLIENT_OPEN_EOF_SPLIT', true), + 'package_eof' => "\r\n", + ], ], 'http' => [ 'host' => env('HTTP_HOST', '0.0.0.0'), 'port' => env('HTTP_PORT', 80), - 'mode' => env('HTTP_MODE', SWOOLE_PROCESS), + 'mode' => env('HTTP_MODE', SWOOLE_PROCESS), 'type' => env('HTTP_TYPE', SWOOLE_SOCK_TCP), ], 'crontab' => [ @@ -26,12 +34,14 @@ 'task_queue' => env('CRONTAB_TASK_QUEUE', 2048), ], 'setting' => [ - 'worker_num' => env('WORKER_NUM', 1), - 'max_request' => env('MAX_REQUEST', 10000), - 'daemonize' => env('DAEMONIZE', 0), - 'dispatch_mode' => env('DISPATCH_MODE', 2), - 'log_file' => env('LOG_FILE', '@runtime/logs/swoole.log'), - 'task_worker_num' => env('TASK_WORKER_NUM', 1), - 'upload_tmp_dir' => env('UPLOAD_TMP_DIR', '@runtime/uploadfiles'), + 'worker_num' => env('WORKER_NUM', 1), + 'max_request' => env('MAX_REQUEST', 10000), + 'daemonize' => env('DAEMONIZE', 0), + 'dispatch_mode' => env('DISPATCH_MODE', 2), + 'log_file' => env('LOG_FILE', '@runtime/logs/swoole.log'), + 'task_worker_num' => env('TASK_WORKER_NUM', 1), + 'upload_tmp_dir' => env('UPLOAD_TMP_DIR', '@runtime/uploadfiles'), + 'message_queue_key' => env('MESSAGE_QUEUE_KEY', 0x70001001), + 'task_tmpdir' => env('TASK_TMPDIR', '/tmp'), ], ]; diff --git a/test/server.php b/test/server.php new file mode 100644 index 00000000..191974a2 --- /dev/null +++ b/test/server.php @@ -0,0 +1,9 @@ +start(); \ No newline at end of file