-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
272 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
language: php | ||
|
||
php: | ||
- '7.2' | ||
- '7.3' | ||
|
||
dist: trusty | ||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- ~/.composer | ||
- ~/.php-ext | ||
|
||
before_install: | ||
- source <(curl -sL https://github.com/carno-php/travis-ci/raw/master/tests-prepare.sh) | ||
|
||
install: | ||
- composer update | ||
|
||
script: | ||
- testing | ||
|
||
after_success: | ||
- coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.3/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Default"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<logging> | ||
<log type="coverage-clover" target="coverage.xml"/> | ||
</logging> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/** | ||
* Invoker test | ||
* User: moyo | ||
* Date: 2019-04-07 | ||
* Time: 00:01 | ||
*/ | ||
|
||
namespace Carno\HRPC\Client\Tests; | ||
|
||
use Carno\Cluster\Resources; | ||
use Carno\Console\App; | ||
use Carno\Console\Contracts\Bootable; | ||
use Carno\Container\DI; | ||
use function Carno\Coroutine\ctx; | ||
use function Carno\Coroutine\go; | ||
use Carno\HRPC\Client\Clustered; | ||
use Carno\HRPC\Client\Selector; | ||
use Carno\HRPC\Client\Tests\Mocked\EndpointsMod; | ||
use Carno\RPC\Contracts\Client\Cluster; | ||
use Carno\RPC\Contracts\Client\Invoker; | ||
use Carno\RPC\Exception\RemoteLogicException; | ||
use Carno\RPC\Exception\RemoteSystemException; | ||
use Carno\RPC\Protocol\Request; | ||
use Carno\RPC\Protocol\Response; | ||
use Carno\Serving\Components\Discovery\Assigning; | ||
use Carno\Serving\Components\Discovery\Classify; | ||
use Carno\Serving\Components\Discovery\Resourced; | ||
use Carno\Serving\Contracts\Options; | ||
use Carno\Serving\Contracts\ScopedConf; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Input\InputDefinition; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Throwable; | ||
|
||
class InvokerTest extends TestCase | ||
{ | ||
private const SERVER = 'ns.g.s'; | ||
|
||
public function testInvoke() | ||
{ | ||
$def = new InputDefinition; | ||
$def->addOption(new InputOption( | ||
Options::SERVICE_DISCOVERY, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'', | ||
'config' | ||
)); | ||
|
||
$app = new App; | ||
$app->inputs(new ArrayInput([], $def)); | ||
|
||
$components = CARNO_COMPONENTS_HRPCC; | ||
array_unshift($components, Classify::class, Assigning::class, Resourced::class); | ||
|
||
foreach ($components as $cc) { | ||
/** | ||
* @var Bootable $boot | ||
*/ | ||
$boot = DI::object($cc); | ||
$boot->runnable() && $boot->starting($app); | ||
} | ||
|
||
/** | ||
* @var Resources $resources | ||
*/ | ||
$resources = DI::get(Resources::class); | ||
$resources->startup(); | ||
|
||
config(ScopedConf::SRV)->set(sprintf(':%s', self::SERVER), '127.0.0.1:80 #TEST'); | ||
|
||
/** | ||
* @var Cluster $cluster | ||
* @var Clustered $cluster | ||
*/ | ||
$cluster = DI::get(Cluster::class); | ||
|
||
$cluster->modifier(new EndpointsMod); | ||
$cluster->joining(self::SERVER); | ||
|
||
$selector = new Selector($cluster); | ||
|
||
$tester = $this; | ||
go(static function () use ($tester, $selector) { | ||
/** | ||
* @var Invoker $invoker | ||
*/ | ||
$invoker = DI::get(Invoker::class); | ||
|
||
// test normal | ||
|
||
$selector->inbound( | ||
$req = (new Request(self::SERVER, 'ss', 'rpc'))->setPayload($body = uniqid()), | ||
yield ctx() | ||
); | ||
/** | ||
* @var Response $resp | ||
*/ | ||
$resp = yield $invoker->call($req); | ||
$tester->assertEquals($body, $resp->getPayload()); | ||
|
||
// test error logic | ||
|
||
$selector->inbound( | ||
$req = (new Request(self::SERVER, 'error', 'logic'))->setPayload(''), | ||
yield ctx() | ||
); | ||
$ee = null; | ||
try { | ||
yield $invoker->call($req); | ||
} catch (Throwable $e) { | ||
$ee = get_class($e); | ||
} | ||
$tester->assertEquals(RemoteLogicException::class, $ee); | ||
|
||
// test error system | ||
|
||
$selector->inbound( | ||
$req = (new Request(self::SERVER, 'error', 'system'))->setPayload(''), | ||
yield ctx() | ||
); | ||
$ee = null; | ||
try { | ||
yield $invoker->call($req); | ||
} catch (Throwable $e) { | ||
$ee = get_class($e); | ||
} | ||
$tester->assertEquals(RemoteSystemException::class, $ee); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Client instance | ||
* User: moyo | ||
* Date: 2019-04-07 | ||
* Time: 01:37 | ||
*/ | ||
|
||
namespace Carno\HRPC\Client\Tests\Mocked; | ||
|
||
use Carno\HRPC\Client\Contracts\Defined; | ||
use Carno\HTTP\Standard\Request; | ||
use Carno\HTTP\Standard\Response; | ||
|
||
class ClientInstance | ||
{ | ||
public function perform(Request $request) | ||
{ | ||
switch ($request->getUri()->getPath()) { | ||
case '/invoke/error/logic': | ||
return new Response(200, [Defined::X_ERR_CODE => 1001, Defined::X_ERR_MESSAGE => 'logic']); | ||
case '/invoke/error/system': | ||
return new Response(500, [Defined::X_ERR_CODE => 1002, Defined::X_ERR_MESSAGE => 'system']); | ||
default: | ||
return new Response(200, $request->getHeaders(), (string) $request->getBody()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Client router | ||
* User: moyo | ||
* Date: 2019-04-07 | ||
* Time: 01:36 | ||
*/ | ||
|
||
namespace Carno\HRPC\Client\Tests\Mocked; | ||
|
||
use Carno\Cluster\Routing\Typed; | ||
use Carno\HRPC\Client\Contracts\Defined; | ||
use Carno\Net\Endpoint; | ||
|
||
class ClientRouter implements Typed | ||
{ | ||
private $nodes = []; | ||
|
||
public function picked(string ...$tags) : array | ||
{ | ||
return $this->nodes; | ||
} | ||
|
||
public function classify(string $tag, Endpoint $node) : void | ||
{ | ||
$this->nodes[] = $node->setOptions([ | ||
Defined::HJ_CLIENT => ClientInstance::class, | ||
]); | ||
} | ||
|
||
public function release(Endpoint $node) : void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* Endpoints modify | ||
* User: moyo | ||
* Date: 2019-04-07 | ||
* Time: 01:29 | ||
*/ | ||
|
||
namespace Carno\HRPC\Client\Tests\Mocked; | ||
|
||
use Carno\HRPC\Client\Contracts\Modify; | ||
use Carno\HRPC\Client\Endpoints; | ||
|
||
class EndpointsMod implements Modify | ||
{ | ||
public function handle(Endpoints $eps) : void | ||
{ | ||
$eps->routing()->typeset()->extend(new ClientRouter); | ||
} | ||
} |