-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Google Pub/Sub driver (#68)
- Loading branch information
Showing
4 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunner\Jobs\Queue; | ||
|
||
final class PubSubCreateInfo extends CreateInfo | ||
{ | ||
public const MAX_DELIVERY_ATTEMPTS_DEFAULT_VALUE = 10; | ||
|
||
/** | ||
* @param non-empty-string $name | ||
* @param non-empty-string $projectId | ||
* @param non-empty-string $topic | ||
* @param positive-int $priority | ||
* @param non-empty-string|null $deadLetterTopic | ||
* @param positive-int $maxDeliveryAttempts | ||
*/ | ||
public function __construct( | ||
string $name, | ||
public readonly string $projectId, | ||
public readonly string $topic, | ||
int $priority = self::PRIORITY_DEFAULT_VALUE, | ||
public readonly ?string $deadLetterTopic = null, | ||
public readonly int $maxDeliveryAttempts = self::MAX_DELIVERY_ATTEMPTS_DEFAULT_VALUE, | ||
) { | ||
parent::__construct(Driver::PubSub, $name, $priority); | ||
|
||
\assert($this->projectId !== '', 'Precondition [projectId !== ""] failed'); | ||
\assert($this->topic !== '', 'Precondition [topic !== ""] failed'); | ||
\assert($this->maxDeliveryAttempts >= 5, 'Precondition [maxDeliveryAttempts >= 5] failed'); | ||
if ($this->deadLetterTopic !== null) { | ||
\assert($this->deadLetterTopic !== '', 'Precondition [deadLetterTopic !== ""] failed'); | ||
} | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$result = \array_merge(parent::toArray(), [ | ||
'project_id' => $this->projectId, | ||
'topic' => $this->topic, | ||
]); | ||
|
||
if ($this->deadLetterTopic !== null && $this->deadLetterTopic !== '') { | ||
$result['dead_letter_topic'] = $this->deadLetterTopic; | ||
$result['max_delivery_attempts'] = $this->maxDeliveryAttempts; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunner\Jobs\Tests\Unit\Queue; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Spiral\RoadRunner\Jobs\Queue\Driver; | ||
use Spiral\RoadRunner\Jobs\Queue\PubSubCreateInfo; | ||
|
||
final class PubSubCreateInfoTest extends TestCase | ||
{ | ||
public function testCreatePubSubCreateInfo(): void | ||
{ | ||
$pubSubCreateInfo = new PubSubCreateInfo( | ||
name: 'test_name', | ||
projectId: 'test_project_id', | ||
topic: 'test_topic', | ||
priority: 3, | ||
deadLetterTopic: 'test_dead_letter_topic', | ||
maxDeliveryAttempts: 15, | ||
); | ||
|
||
$this->assertSame(Driver::PubSub, $pubSubCreateInfo->driver); | ||
$this->assertSame('test_name', $pubSubCreateInfo->name); | ||
$this->assertSame('test_project_id', $pubSubCreateInfo->projectId); | ||
$this->assertSame('test_topic', $pubSubCreateInfo->topic); | ||
$this->assertSame(3, $pubSubCreateInfo->priority); | ||
$this->assertSame('test_dead_letter_topic', $pubSubCreateInfo->deadLetterTopic); | ||
$this->assertSame(15, $pubSubCreateInfo->maxDeliveryAttempts); | ||
} | ||
|
||
public function testCreatePubSubCreateInfoOnlyRequiredData(): void | ||
{ | ||
$pubSubCreateInfo = new PubSubCreateInfo( | ||
name: 'test_name', | ||
projectId: 'test_project_id', | ||
topic: 'test_topic', | ||
); | ||
|
||
$this->assertSame(Driver::PubSub, $pubSubCreateInfo->driver); | ||
$this->assertSame('test_name', $pubSubCreateInfo->name); | ||
$this->assertSame('test_project_id', $pubSubCreateInfo->projectId); | ||
$this->assertSame('test_topic', $pubSubCreateInfo->topic); | ||
$this->assertSame(10, $pubSubCreateInfo->priority); | ||
$this->assertNull($pubSubCreateInfo->deadLetterTopic); | ||
$this->assertSame(10, $pubSubCreateInfo->maxDeliveryAttempts); | ||
} | ||
|
||
public function testToArray(): void | ||
{ | ||
$pubSubCreateInfo = new PubSubCreateInfo( | ||
name: 'test_name', | ||
projectId: 'test_project_id', | ||
topic: 'test_topic', | ||
priority: 3, | ||
deadLetterTopic: 'test_dead_letter_topic', | ||
maxDeliveryAttempts: 15, | ||
); | ||
|
||
$expectedArray = [ | ||
'name' => 'test_name', | ||
'driver' => Driver::PubSub->value, | ||
'priority' => 3, | ||
'project_id' => 'test_project_id', | ||
'topic' => 'test_topic', | ||
'dead_letter_topic' => 'test_dead_letter_topic', | ||
'max_delivery_attempts' => 15, | ||
]; | ||
|
||
$this->assertSame($expectedArray, $pubSubCreateInfo->toArray()); | ||
} | ||
|
||
public function testToArrayOnlyRequiredData(): void | ||
{ | ||
$pubSubCreateInfo = new PubSubCreateInfo( | ||
name: 'test_name', | ||
projectId: 'test_project_id', | ||
topic: 'test_topic', | ||
); | ||
|
||
$expectedArray = [ | ||
'name' => 'test_name', | ||
'driver' => Driver::PubSub->value, | ||
'priority' => 10, | ||
'project_id' => 'test_project_id', | ||
'topic' => 'test_topic', | ||
]; | ||
|
||
$this->assertSame($expectedArray, $pubSubCreateInfo->toArray()); | ||
} | ||
} |