Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moolex committed Apr 6, 2019
1 parent 1c2a272 commit b926689
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 3 deletions.
File renamed without changes.
25 changes: 25 additions & 0 deletions .travis.yml
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
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
"psr-4": {
"Carno\\HRPC\\Client\\": "src"
},
"files": [
"src/.init.php"
]
"files": [".init.php"]
},
"require-dev": {
"carno-php/console": "^1.0",
"carno-php/serving": "^1.0"
},
"autoload-dev": {
"psr-4": {
"Carno\\HRPC\\Client\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"extra": {
Expand Down
22 changes: 22 additions & 0 deletions phpunit.xml.dist
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>
133 changes: 133 additions & 0 deletions tests/InvokerTest.php
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);
});
}
}
28 changes: 28 additions & 0 deletions tests/Mocked/ClientInstance.php
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());
}
}
}
34 changes: 34 additions & 0 deletions tests/Mocked/ClientRouter.php
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
{
}
}
20 changes: 20 additions & 0 deletions tests/Mocked/EndpointsMod.php
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);
}
}

0 comments on commit b926689

Please sign in to comment.