Skip to content

Commit

Permalink
small rafactors
Browse files Browse the repository at this point in the history
  • Loading branch information
samhannan committed Apr 5, 2024
1 parent c2943f1 commit 80cc814
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 51 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,3 @@ parameters:
message: "#^Method AgentSoftware\\\\LaravelRawSqsConnector\\\\RawSqsQueue\\:\\:receiveMessage\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/RawSqsQueue.php

-
message: "#^Parameter \\#2 \\$maxAttempts of static method Illuminate\\\\Support\\\\Facades\\\\RateLimiter\\:\\:attempt\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/RawSqsQueue.php
15 changes: 11 additions & 4 deletions src/RawSqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function pop($queue = null): SqsJob|Job|null

protected function receiveMessage(string $queue): Result|array|null
{
if ($this->rateLimit === null) {
if ($this->getRateLimit() === null) {
return $this->querySqs($queue);
}

Expand Down Expand Up @@ -76,11 +76,11 @@ protected function log(string $text, array $context = []): void
protected function hasRemainingAttempts(string $key): mixed
{
/** @var int $limit */
$limit = $this->rateLimit;
$limit = $this->getRateLimit();

return RateLimiter::attempt(
$key,
$this->rateLimit,
$limit,
fn () => true,
);
}
Expand Down Expand Up @@ -129,7 +129,6 @@ public function later($delay, $job, $data = '', $queue = null)
throw new InvalidPayloadException('later is not permitted for raw-sqs connector');
}


/**
* @return string
*/
Expand All @@ -138,6 +137,14 @@ public function getJobClass(): string
return $this->jobClass;
}

/**
* @return int|null
*/
public function getRateLimit(): ?int
{
return $this->rateLimit;
}

/**
* @param string $jobClass
* @return $this
Expand Down
36 changes: 36 additions & 0 deletions tests/RawSqsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@ public function testConnectShouldReturnRawSqsQueue(): void
$this->assertInstanceOf(RawSqsQueue::class, $rawSqsQueue);
}

public function testCanSpecifyAnIntegerRateLimit(): void
{
$rawSqsConnector = new RawSqsConnector();

$config = [
'key' => 'key',
'secret' => 'secret',
'region' => 'eu-west-2',
'queue' => 'raw-sqs',
'job_class' => TestJobClass::class,
'rate_limit' => 1
];

$rawSqsQueue = $rawSqsConnector->connect($config);

$this->assertEquals(1, $rawSqsQueue->getRateLimit());
}

public function testCanSpecifyACallableRateLimit(): void
{
$rawSqsConnector = new RawSqsConnector();

$config = [
'key' => 'key',
'secret' => 'secret',
'region' => 'eu-west-2',
'queue' => 'raw-sqs',
'job_class' => TestJobClass::class,
'rate_limit' => fn () => 1
];

$rawSqsQueue = $rawSqsConnector->connect($config);

$this->assertEquals(1, $rawSqsQueue->getRateLimit());
}

public function testShouldThrowInvalidArgumentExceptionIfClassDoesNotExist(): void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down
42 changes: 0 additions & 42 deletions tests/RawSqsQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,48 +202,6 @@ public function testWillReturnMessageIfRateLimitEnabled(): void
$this->expectNotToPerformAssertions();
}

public function testWillReturnMessageIfRateLimitEnabledUsingAClosure(): void
{
$firstName = 'Primitive';
$lastName = 'Sense';

$sqsReturnMessage = [
'Body' => json_encode([
'first_name' => $firstName,
'last_name' => $lastName
])
];

$sqsClientMock = Mockery::mock(SqsClient::class);
$sqsClientMock->shouldReceive('receiveMessage')
->andReturn([
'Messages' => [
$sqsReturnMessage
]
]);

$rawSqsQueue = Mockery::mock(RawSqsQueue::class, [
$sqsClientMock,
'default',
'prefix'
])
->shouldAllowMockingProtectedMethods()
->makePartial();

$rawSqsQueue
->shouldReceive('hasRemainingAttempts')
->andReturn(true);

$container = Mockery::mock(Container::class);
$rawSqsQueue->setContainer($container);
$rawSqsQueue->setJobClass(TestJobClass::class);
$rawSqsQueue->setRateLimit(fn () => 1);

$rawSqsQueue->pop();

$this->expectNotToPerformAssertions();
}

public function testWillNotReturnMessageIfRateLimitEnabledButNoAttemptsLeft(): void
{
$sqsClientMock = Mockery::mock(SqsClient::class);
Expand Down

0 comments on commit 80cc814

Please sign in to comment.