Skip to content

Commit 7b2a25c

Browse files
committed
Refactor readline
1 parent e5ae947 commit 7b2a25c

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/TcpClient.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Clue\Redis\Protocol\Factory as ProtocolFactory;
66
use Clue\Redis\Protocol\Parser\ParserInterface;
77
use Knuckles\Faktory\Problems\CouldntConnect;
8+
use Knuckles\Faktory\Problems\ProtocolError;
89
use Knuckles\Faktory\Problems\UnexpectedResponse;
910
use Knuckles\Faktory\Utils\Json;
1011
use Psr\Log\LoggerAwareInterface;
@@ -75,7 +76,7 @@ protected function handshake()
7576
{
7677
$this->readHi();
7778
$this->sendHello();
78-
self::checkOk($this->readLine(), operation: "Handshake");
79+
$this->readLine(operation: "Handshake");
7980
}
8081

8182
protected function readHi()
@@ -102,13 +103,10 @@ protected function sendHello()
102103
$this->send("HELLO", $workerInfo);
103104
}
104105

105-
/**
106-
* Send a command and raise an error if the response is not OK.
107-
*/
108-
public function operation($command, string ...$args): void
106+
public function sendAndRead($command, string ...$args): mixed
109107
{
110108
$this->send($command, ...$args);
111-
self::checkOk($this->readLine(), operation: $command);
109+
return $this->readLine(operation: $command);
112110
}
113111

114112
public function send($command, string ...$args): void
@@ -122,7 +120,7 @@ public function send($command, string ...$args): void
122120
fwrite($this->connection, $message);
123121
}
124122

125-
public function readLine(?int $skipLines = 0): mixed
123+
public function readLine(?int $skipLines = 0, $operation = "Operation"): mixed
126124
{
127125
if ($this->state == State::Disconnected) {
128126
$this->connect();
@@ -139,16 +137,11 @@ public function readLine(?int $skipLines = 0): mixed
139137
$messages = $this->responseParser->pushIncoming($line);
140138
if (empty($messages)) return null;
141139

142-
return $messages[0]?->getValueNative();
143-
}
140+
$response = $messages[0]?->getValueNative();
141+
if (is_string($response) && str_starts_with($response, "ERR"))
142+
throw UnexpectedResponse::from($operation, $response);
144143

145-
private static function checkOk(mixed $result, $operation = "Operation")
146-
{
147-
if ($result !== "OK") {
148-
throw UnexpectedResponse::from($operation, $result);
149-
}
150-
151-
return true;
144+
return $response;
152145
}
153146

154147
public function setLogger(LoggerInterface $logger): void

tests/client_test.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Knuckles\Faktory\Client;
4+
use Knuckles\Faktory\TcpClient;
5+
6+
function client($mockTcpClient) {
7+
$client = new Client();
8+
$tcpClient = new \ReflectionProperty($client, 'tcpClient');
9+
$tcpClient->setValue($mockTcpClient);
10+
return $client;
11+
}
12+
13+
it('can push and retrieve jobs', function () {
14+
$mockTcpClient = Mockery::mock(TcpClient::class);
15+
$mockTcpClient->shouldReceive('send', 'PUSH');
16+
17+
$client = client($mockTcpClient);
18+
$client->push([
19+
"jid" => "123861239abnadsa",
20+
"jobtype" => "SomeJobClass",
21+
"args" => [1, 2, true, "hello"],
22+
]);
23+
expect();
24+
})->skip();

tests/tcp_client_test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@
7070
$tcpClient->connect();
7171
});
7272

73-
test('->send and ->readLine() do not raise an error when the response is not OK', function () {
73+
test('->readLine() raises an error when the response is an ERR', function () {
7474
$tcpClient = tcpClient();
75-
$tcpClient->send("PUSH", "Something");
76-
expect($tcpClient->readLine())->toStartWith("ERR");
75+
$tcpClient->send("PUSH", "Invalid payload");
76+
expect(fn() => $tcpClient->readLine())->toThrow(UnexpectedResponse::class);
7777
});
7878

79-
test('->operation() raises an error when the response is not OK', function () {
79+
test('->sendAndRead() raises an error when the response is not OK', function () {
8080
expect(
81-
fn() => tcpClient()->operation("PUSH", "Anything")
81+
fn() => tcpClient()->sendAndRead("PUSH", "Invalid data")
8282
)->toThrow(UnexpectedResponse::class);
8383
});
8484

0 commit comments

Comments
 (0)