Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

兼容swoole 4.0.3版本 #166

Merged
merged 5 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/framework/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before_install:
install:
- wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz -O hiredis.tar.gz && mkdir -p hiredis && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 && cd hiredis && sudo make -j$(nproc) && sudo make install && sudo ldconfig && cd ..
- echo 'no' | pecl install -f redis
- wget https://github.com/swoole/swoole-src/archive/v4.0.2.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- wget https://github.com/swoole/swoole-src/archive/v4.0.3.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
Expand Down
4 changes: 2 additions & 2 deletions src/framework/src/Core/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function resume($coroutineId)
*/
public static function isSupportCoroutine(): bool
{
if (swoole_version() >= '2.0.11') {
if (version_compare(swoole_version(), '2.0.11', '>=')) {
return true;
} else {
return App::isWorkerStatus();
Expand All @@ -119,6 +119,6 @@ public static function isSupportCoroutine(): bool
*/
public static function shouldWrapCoroutine()
{
return App::isWorkerStatus() && swoole_version() >= '2.0.11';
return App::isWorkerStatus() && version_compare(swoole_version(), '2.0.11', '>=');
}
}
13 changes: 11 additions & 2 deletions src/framework/src/Pool/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,18 @@ private function getConnectionByChannel(): ConnectionInterface
return $this->channel->pop();
}

// When swoole version is larger than 4.0.3, Channel->select is removed.
if (version_compare(swoole_version(), '4.0.3', '>=')) {
$result = $this->channel->pop($maxWaitTime);
if ($result === false) {
throw new ConnectionException('Connection pool waiting queue timeout, timeout=' . $maxWaitTime);
}
return $result;
}

$writes = [];
$reads = [$this->channel];
$result = $this->channel->select($reads, $writes, $maxWaitTime);
$reads = [$this->channel];
$result = $this->channel->select($reads, $writes, $maxWaitTime);

if ($result === false || empty($reads)) {
throw new ConnectionException('Connection pool waiting queue timeout, timeout='.$maxWaitTime);
Expand Down
33 changes: 33 additions & 0 deletions src/framework/test/Cases/Connection/DemoConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace SwoftTest\Connection;

use Swoft\Pool\AbstractConnection;

class DemoConnection extends AbstractConnection
{
/**
* @var Client
*/
protected $connection;

public function createConnection()
{
$this->connection = new \stdClass();
$this->connection->id = uniqid();
}

public function reconnect()
{
$this->createConnection();
}

public function check(): bool
{
return true;
}

public function getConnection()
{
return $this->connection;
}
}
22 changes: 22 additions & 0 deletions src/framework/test/Cases/CorouotineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace SwoftTest;

use Swoft\Core\Coroutine;

/**
* Class CorouotineTest
*
* @package Swoft\Test\Cases
*/
class CorouotineTest extends AbstractTestCase
{
public function testIsSupportCoroutine()
{
$this->assertTrue(Coroutine::isSupportCoroutine());
}

public function testShouldWrapCoroutine()
{
$this->assertFalse(Coroutine::shouldWrapCoroutine());
}
}
33 changes: 33 additions & 0 deletions src/framework/test/Cases/Pool/DemoPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace SwoftTest\Pool;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;
use Swoft\Pool\ConnectionInterface;
use Swoft\Pool\ConnectionPool;
use Swoft\Pool\PoolProperties;
use SwoftTest\Connection\DemoConnection;
use Swoft\Bean\Annotation\Inject;
use Swoft\Bean\Annotation\Pool;

/**
* Class DemoPool
* @Pool()
* @package SwoftTest\Pool
*/
class DemoPool extends ConnectionPool
{
/**
* The config of poolbPool
*
* @Inject()
*
* @var DemoPoolConfig
*/
protected $poolConfig;

public function createConnection(): ConnectionInterface
{
return new DemoConnection($this);
}
}
16 changes: 16 additions & 0 deletions src/framework/test/Cases/Pool/DemoPoolConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace SwoftTest\Pool;

use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Value;
use Swoft\Pool\PoolProperties;

/**
* the config of env
*
* @Bean
*/
class DemoPoolConfig extends PoolProperties
{

}
29 changes: 29 additions & 0 deletions src/framework/test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\App;
use SwoftTest\Pool\ConsulEnvConfig;
use SwoftTest\Pool\ConsulPptConfig;
use SwoftTest\Pool\DemoPool;
use SwoftTest\Pool\EnvAndPptFromPptPoolConfig;
use SwoftTest\Pool\EnvAndPptPoolConfig;
use SwoftTest\Pool\EnvPoolConfig;
Expand Down Expand Up @@ -141,4 +142,32 @@ public function testConsulEnv()
$this->assertEquals(2, $pConfig->getInterval());
$this->assertEquals([1, 2], $pConfig->getTags());
}

public function testGetConnection()
{
$pool = bean(DemoPool::class);
$connection = $pool->getConnection();
$connection2 = $pool->getConnection();

$id = $connection->getConnection()->id;
$id2 = $connection2->getConnection()->id;

$this->assertNotEquals($id, $id2);

$connection->release(true);
$connection->receive();
$connection3 = $pool->getConnection();
$id3 = $connection3->getConnection()->id;
$this->assertEquals($id, $id3);

$connection2->release(true);
$connection3->release(true);
}

public function testGetConnectionByCo()
{
go(function () {
$this->testGetConnection();
});
}
}