Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Cache] Fix connecting to Redis via a socket file #45281

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

Merged
merged 1 commit into from
Feb 4, 2022
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
16 changes: 15 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
redis:
image: redis:6.0.0
ports:
- 6379:6379
- 16379:6379
redis-cluster:
image: grokzen/redis-cluster:5.0.4
ports:
Expand Down Expand Up @@ -67,6 +67,19 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Install system dependencies
run: |
echo "::group::apt-get update"
sudo apt-get update
echo "::endgroup::"

echo "::group::install tools & libraries"
sudo apt-get install redis-server
sudo -- sh -c 'echo unixsocket /var/run/redis/redis-server.sock >> /etc/redis/redis.conf'
sudo -- sh -c 'echo unixsocketperm 777 >> /etc/redis/redis.conf'
sudo service redis-server restart
echo "::endgroup::"

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -99,6 +112,7 @@ jobs:
- name: Run tests
run: ./phpunit --group integration -v
env:
REDIS_HOST: 'localhost:16379'
REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005'
REDIS_SENTINEL_HOSTS: 'localhost:26379'
REDIS_SENTINEL_SERVICE: redis_sentinel
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<env name="LDAP_HOST" value="localhost" />
<env name="LDAP_PORT" value="3389" />
<env name="REDIS_HOST" value="localhost" />
<env name="REDIS_SOCKET" value="/var/run/redis/redis-server.sock" />
<env name="MESSENGER_REDIS_DSN" value="redis://localhost/messages" />
<env name="MEMCACHED_HOST" value="localhost" />
<env name="MONGODB_HOST" value="localhost" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected static function createKernel(array $options = []): KernelInterface
private function skipIfRedisUnavailable()
{
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
cache.test_redis_connection:
public: false
class: Redis
calls:
- [connect, ['%env(REDIS_HOST)%']]
factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
arguments: ['redis://%env(REDIS_HOST)%']

cache.app:
parent: cache.adapter.redis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static function setUpBeforeClass(): void
throw new SkippedTestSuiteError('Extension redis required.');
}
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
throw new SkippedTestSuiteError($e->getMessage());
throw new SkippedTestSuiteError(getenv('REDIS_HOST').': '.$e->getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PredisAdapterTest extends AbstractRedisAdapterTest
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')], ['prefix' => 'prefix_']);
self::$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), ['prefix' => 'prefix_']);
}

public function testCreateConnection()
Expand All @@ -35,10 +35,11 @@ public function testCreateConnection()
$connection = $redis->getConnection();
$this->assertInstanceOf(StreamConnection::class, $connection);

$redisHost = explode(':', $redisHost);
$params = [
'scheme' => 'tcp',
'host' => $redisHost,
'port' => 6379,
'host' => $redisHost[0],
'port' => (int) ($redisHost[1] ?? 6379),
'persistent' => 0,
'timeout' => 3,
'read_write_timeout' => 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PredisClusterAdapterTest extends AbstractRedisAdapterTest
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]], ['prefix' => 'prefix_']);
self::$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), ['prefix' => 'prefix_']);
}

public static function tearDownAfterClass(): void
Expand Down
26 changes: 19 additions & 7 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,42 @@ public function createCachePool(int $defaultLifetime = 0, string $testMethod = n
return $adapter;
}

public function testCreateConnection()
public function testCreateHostConnection()
{
$redis = RedisAdapter::createConnection('redis:?host[h1]&host[h2]&host[/foo:]');
$this->assertInstanceOf(\RedisArray::class, $redis);
$this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts());
@$redis = null; // some versions of phpredis connect on destruct, let's silence the warning

$redisHost = getenv('REDIS_HOST');
$this->doTestCreateConnection(getenv('REDIS_HOST'));
}

public function testCreateSocketConnection()
{
if (!getenv('REDIS_SOCKET') || !file_exists(getenv('REDIS_SOCKET'))) {
$this->markTestSkipped('Redis socket not found');
}

$this->doTestCreateConnection(getenv('REDIS_SOCKET'));
}

$redis = RedisAdapter::createConnection('redis://'.$redisHost);
private function doTestCreateConnection(string $uri)
{
$redis = RedisAdapter::createConnection('redis://'.$uri);
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
$redis = RedisAdapter::createConnection('redis://'.$uri.'/2');
$this->assertSame(2, $redis->getDbNum());

$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['timeout' => 3]);
$redis = RedisAdapter::createConnection('redis://'.$uri, ['timeout' => 3]);
$this->assertEquals(3, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
$redis = RedisAdapter::createConnection('redis://'.$uri.'?timeout=4');
$this->assertEquals(4, $redis->getTimeout());

$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
$redis = RedisAdapter::createConnection('redis://'.$uri, ['read_timeout' => 5]);
$this->assertEquals(5, $redis->getReadTimeout());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function setUpBeforeClass(): void
throw new SkippedTestSuiteError('Extension redis required.');
}
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
throw new SkippedTestSuiteError($e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function createConnection($dsn, array $options = [])

$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) {
$host = $hosts[0]['host'] ?? $hosts[0]['path'];
$port = $hosts[0]['port'] ?? 6379;
$port = $hosts[0]['port'] ?? 0;

if (isset($hosts[0]['host']) && $tls) {
$host = 'tls://'.$host;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<php>
<ini name="error_reporting" value="-1" />
<env name="REDIS_HOST" value="localhost" />
<env name="REDIS_SOCKET" value="/var/run/redis/redis-server.sock" />
<env name="MEMCACHED_HOST" value="localhost" />
</php>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUp(): void
self::markTestSkipped('Extension redis required.');
}
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas
{
protected function createRedisClient(string $host): Client
{
return new Client([['host' => $host]]);
return new Client([array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class PredisSessionHandlerTest extends AbstractRedisSessionHandlerTestCase
{
protected function createRedisClient(string $host): Client
{
return new Client(['host' => $host]);
return new Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RedisSessionHandlerTest extends AbstractRedisSessionHandlerTestCase
protected function createRedisClient(string $host)
{
$client = new \Redis();
$client->connect($host);
$client->connect(...explode(':', $host));

return $client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function getClockDelay()
*/
public function getStore(): PersistingStoreInterface
{
$redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379');
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
try {
$redis->connect();
} catch (\Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PredisStoreTest extends AbstractRedisStoreTest
{
public static function setUpBeforeClass(): void
{
$redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379');
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
try {
$redis->connect();
} catch (\Exception $e) {
Expand All @@ -31,7 +31,7 @@ public static function setUpBeforeClass(): void

protected function getRedisConnection()
{
$redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379');
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
$redis->connect();

return $redis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function setUpBeforeClass(): void
throw new SkippedTestSuiteError('The RedisArray class is required.');
}
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
throw new SkippedTestSuiteError($e->getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RedisStoreTest extends AbstractRedisStoreTest
public static function setUpBeforeClass(): void
{
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
throw new SkippedTestSuiteError($e->getMessage());
}
Expand All @@ -35,7 +35,7 @@ public static function setUpBeforeClass(): void
protected function getRedisConnection()
{
$redis = new \Redis();
$redis->connect(getenv('REDIS_HOST'));
$redis->connect(...explode(':', getenv('REDIS_HOST')));

return $redis;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RetryTillSaveStoreTest extends AbstractStoreTest

public function getStore(): PersistingStoreInterface
{
$redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379');
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
try {
$redis->connect();
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testCreateTransport()
private function skipIfRedisUnavailable()
{
try {
(new \Redis())->connect(getenv('REDIS_HOST'));
(new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ public function testNotConnected()

public function testConnected()
{
$redisHost = getenv('REDIS_HOST');
$redisHost = explode(':', getenv('REDIS_HOST')) + [1 => 6379];
$redis = new \Redis();
try {
$redis->connect($redisHost);
$redis->connect(...$redisHost);
} catch (\Exception $e) {
self::markTestSkipped($e->getMessage());
}

$xCast = <<<EODUMP
Redis {%A
isConnected: true
host: "$redisHost"
port: 6379
host: "{$redisHost[0]}"
port: {$redisHost[1]}
auth: null
mode: ATOMIC
dbNum: 0
Expand Down