Skip to content

Commit

Permalink
兼容swoole 4.0.3版本 (swoft-cloud/swoft-component#166)
Browse files Browse the repository at this point in the history
* 判断swoole版本使用version_compare

* 更新单测

* 当swoole版本号大于4.0.3,使用channel->pop代替channel->select

* 增加单测

* 修改 Travis Ci swoole版本
  • Loading branch information
limingxinleo authored and huangzhhui committed Aug 7, 2018
1 parent 779189d commit 0f178b2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .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/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/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 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 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 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 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 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();
});
}
}

0 comments on commit 0f178b2

Please sign in to comment.