Skip to content

Commit

Permalink
[THRIFT-5757] Unit tests for php lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 8, 2024
1 parent 3e3cfb4 commit eaf5e0f
Show file tree
Hide file tree
Showing 25 changed files with 627 additions and 87 deletions.
61 changes: 35 additions & 26 deletions lib/php/lib/Transport/TSocketPool.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -24,24 +25,6 @@

use Thrift\Exception\TException;

/**
* This library makes use of APCu cache to make hosts as down in a web
* environment. If you are running from the CLI or on a system without APCu
* installed, then these null functions will step in and act like cache
* misses.
*/
if (!function_exists('apcu_fetch')) {
function apcu_fetch($key)
{
return false;
}

function apcu_store($key, $var, $ttl = 0)
{
return false;
}
}

/**
* Sockets implementation of the TTransport interface that allows connection
* to a pool of servers.
Expand Down Expand Up @@ -91,6 +74,12 @@ class TSocketPool extends TSocket
*/
private $alwaysTryLast_ = true;

/**
* Use apcu cache
* @var bool
*/
private $useApcuCache;

/**
* Socket pool constructor
*
Expand All @@ -116,9 +105,13 @@ public function __construct(
}

foreach ($hosts as $key => $host) {
$this->servers_ [] = array('host' => $host,
'port' => $ports[$key]);
$this->servers_ [] = array(
'host' => $host,
'port' => $ports[$key]
);
}

$this->useApcuCache = function_exists('apcu_fetch');
}

/**
Expand Down Expand Up @@ -206,7 +199,7 @@ public function open()
$failtimeKey = 'thrift_failtime:' . $host . ':' . $port . '~';

// Cache miss? Assume it's OK
$lastFailtime = apcu_fetch($failtimeKey);
$lastFailtime = $this->apcuFetch($failtimeKey);
if ($lastFailtime === false) {
$lastFailtime = 0;
}
Expand Down Expand Up @@ -251,7 +244,7 @@ public function open()

// Only clear the failure counts if required to do so
if ($lastFailtime > 0) {
apcu_store($failtimeKey, 0);
$this->apcuStore($failtimeKey, 0);
}

// Successful connection, return now
Expand All @@ -265,7 +258,7 @@ public function open()
$consecfailsKey = 'thrift_consecfails:' . $host . ':' . $port . '~';

// Ignore cache misses
$consecfails = apcu_fetch($consecfailsKey);
$consecfails = $this->apcuFetch($consecfailsKey);
if ($consecfails === false) {
$consecfails = 0;
}
Expand All @@ -284,12 +277,12 @@ public function open()
);
}
// Store the failure time
apcu_store($failtimeKey, time());
$this->apcuStore($failtimeKey, time());

// Clear the count of consecutive failures
apcu_store($consecfailsKey, 0);
$this->apcuStore($consecfailsKey, 0);
} else {
apcu_store($consecfailsKey, $consecfails);
$this->apcuStore($consecfailsKey, $consecfails);
}
}
}
Expand All @@ -307,4 +300,20 @@ public function open()
}
throw new TException($error);
}

/**
* This library makes use of APCu cache to make hosts as down in a web
* environment. If you are running from the CLI or on a system without APCu
* installed, then these null functions will step in and act like cache
* misses.
*/
private function apcuFetch($key, &$success = null)
{
return $this->useApcuCache ? apcu_fetch($key, $success) : false;
}

private function apcuStore($key, $var, $ttl = 0)
{
return $this->useApcuCache ? apcu_store($key, $var, $ttl) : false;
}
}
2 changes: 0 additions & 2 deletions lib/php/test/Fixtures/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.test
*/

namespace Test\Thrift\Fixtures;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Fixtures/TJSONProtocolFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.test
*/

namespace Test\Thrift\Fixtures;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Fixtures/TSimpleJSONProtocolFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.test
*/

namespace Test\Thrift\Fixtures;
Expand Down
5 changes: 0 additions & 5 deletions lib/php/test/Unit/Lib/ClassLoader/Fixtures/A/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/

namespace A;
Expand Down
5 changes: 0 additions & 5 deletions lib/php/test/Unit/Lib/ClassLoader/Fixtures/B/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/

namespace B;
Expand Down
5 changes: 0 additions & 5 deletions lib/php/test/Unit/Lib/ClassLoader/Fixtures/C/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/

namespace C;
Expand Down
5 changes: 0 additions & 5 deletions lib/php/test/Unit/Lib/ClassLoader/Fixtures/D/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/

namespace D;
Expand Down
5 changes: 0 additions & 5 deletions lib/php/test/Unit/Lib/ClassLoader/Fixtures/E/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/

namespace E;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TBinaryProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TCompactProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TFramedTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TJSONProtocolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TStringFuncFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Factory/TTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

namespace Test\Thrift\Unit\Lib\Factory;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TBufferedTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TCurlClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TFramedTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/THttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TMemoryBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TNullTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TPhpStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
2 changes: 0 additions & 2 deletions lib/php/test/Unit/Lib/Transport/TSSLSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.transport
*/

namespace Test\Thrift\Unit\Lib\Transport;
Expand Down
Loading

0 comments on commit eaf5e0f

Please sign in to comment.