Skip to content
Snippets Groups Projects
Commit 45ec8df9 authored by Luhur Abdi Rizal's avatar Luhur Abdi Rizal
Browse files

Issue #3432426 by el7cosmos: Add support for Relay

parent 26c4469a
No related branches found
No related tags found
1 merge request!8Add support for Relay
Pipeline #126321 passed
......@@ -109,7 +109,9 @@ phpunit:
extends: .phpunit-base
parallel:
matrix:
- REDIS_INTERFACE: [PhpRedis, Predis]
- REDIS_INTERFACE: [PhpRedis, Predis, Relay]
variables:
PHP_INI_DIR: /usr/local/etc/php
services:
- !reference [.with-database]
- name: redis:alpine
......@@ -120,6 +122,12 @@ phpunit:
pecl install redis && docker-php-ext-enable redis
echo -e "\e[0Ksection_end:`date +%s`:my_first_section\r\e[0K"
fi
- |
if [ "$REDIS_INTERFACE" == "Relay" ]; then
echo -e "\e[0Ksection_start:`date +%s`:my_first_section[collapsed=true]\r\e[0KInstall PHP Extension"
curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s relay
echo -e "\e[0Ksection_end:`date +%s`:my_first_section\r\e[0K"
fi
- cp $CI_PROJECT_DIR/$_WEB_ROOT/modules/custom/$CI_PROJECT_NAME/tests/fixtures/settings.testing.php $CI_PROJECT_DIR/$_WEB_ROOT/sites/default/settings.testing.php
- echo "SetEnv REDIS_INTERFACE $REDIS_INTERFACE" >> /etc/apache2/apache2.conf
- echo "SetEnv REDIS_HOST redis" >> /etc/apache2/apache2.conf
......@@ -14,6 +14,9 @@ abstract class BatchStorageBase implements BatchStorageInterface {
use RedisPrefixTrait;
/**
* Constructs a new BatchStorageBase object.
*/
public function __construct(
protected readonly mixed $client,
protected readonly CsrfTokenGenerator $csrfToken,
......
......@@ -24,6 +24,7 @@ final class BatchStorageFactory {
return match ($clientName) {
'PhpRedis' => new PhpRedisBatchStorage($clientFactory::getClient(), $csrfToken, $serializer, $session),
'Predis' => new PredisBatchStorage($clientFactory::getClient(), $csrfToken, $serializer, $session),
'Relay' => new RelayBatchStorage($clientFactory::getClient(), $csrfToken, $serializer, $session),
default => throw new \Exception('Not implemented for ' . $clientName),
};
}
......
<?php
namespace Drupal\redis_batch\Batch;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Access\CsrfTokenGenerator;
use Relay\Relay;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Relay batch storage backend.
*
* @property \Relay\Relay $client
*/
class RelayBatchStorage extends PhpRedisBatchStorage {
/**
* {@inheritdoc}
*/
public function __construct(mixed $client, CsrfTokenGenerator $csrfToken, SerializationInterface $serializer, SessionInterface $session) {
parent::__construct($client, $csrfToken, $serializer, $session);
$this->client->addIgnorePatterns('drupal:batch:*');
}
public function doCreate(string $key, string $token, string $batch): void {
$pipe = $this->client->multi();
if (!$pipe instanceof Relay) {
trigger_error('Unable to start pipeline to write batch', E_USER_WARNING);
return;
}
$pipe->hMSet($key, [
'token' => $token,
'batch' => $batch,
]);
$pipe->expire($key, self::TTL);
$pipe->exec();
}
}
......@@ -4,7 +4,9 @@ namespace Drupal\redis_batch\Batch;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\redis\Client\Predis;
use Drupal\redis\ClientFactory;
use Drupal\redis\ClientInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\TestTools\Random;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
......@@ -17,30 +19,14 @@ class BatchStorageFactoryTest extends UnitTestCase {
/**
* @dataProvider getProvider
*/
public function testGet(string $clientName, ?string $expectedClass): void {
public function testGet(ClientInterface $client, ?string $expectedClass): void {
ClientFactory::setClient($client);
if (!$expectedClass) {
$this->expectExceptionMessage('Not implemented for ' . $clientName);
$this->expectExceptionMessage('Not implemented for ' . ClientFactory::getClientName());
}
$factory = new class() extends ClientFactory {
/**
* @var string
*/
public static string $clientName;
/**
* {@inheritdoc}
*/
public static function getClientName(): string {
return self::$clientName;
}
};
$factory::$clientName = $clientName;
$storage = BatchStorageFactory::get(
$factory,
new ClientFactory(),
$this->createMock(CsrfTokenGenerator::class),
$this->createMock(SerializationInterface::class),
$this->createMock(SessionInterface::class),
......@@ -52,10 +38,32 @@ class BatchStorageFactoryTest extends UnitTestCase {
/**
* @return \Generator
*/
public static function getProvider(): \Generator {
yield ['PhpRedis', PhpRedisBatchStorage::class];
yield ['Predis', PredisBatchStorage::class];
yield [Random::machineName(), NULL];
public function getProvider(): \Generator {
$phpRedis = $this->createMock(ClientInterface::class);
$phpRedis->method('getName')->willReturn('PhpRedis');
yield 'PhpRedis' => [$phpRedis, PhpRedisBatchStorage::class];
yield 'Predis' => [new Predis(), PredisBatchStorage::class];
$relay = $this->createMock(ClientInterface::class);
$relay->method('getName')->willReturn('Relay');
$relay->method('getClient')->willReturn(new class() {
public function addIgnorePatterns(string ...$pattern): void {}
});
yield 'Relay' => [$relay, RelayBatchStorage::class];
$random = $this->createMock(ClientInterface::class);
$random->method('getName')->willReturn(Random::machineName());
yield 'Random' => [$random, NULL];
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void {
ClientFactory::reset();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment