Skip to content
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
22 changes: 19 additions & 3 deletions src/AsyncResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,34 @@

/**
* Wait until task is ready, and return its final result.
*
* @param float|list<float> $interval Either a single interval in which to
* try fetching the result, or a list of intervals which will be used in
* specified order - the last interval is then tried repeatedly.
*/
public function get(?float $timeout = 10, float $interval = 0.4): mixed {
public function get(
?float $timeout = 10,
float|array $interval = 0.4,
): mixed {

$startTime = Functions::monotonicTime();
$timeoutTime = $timeout !== null
? $startTime + $timeout
: null;

$uInterval = (int) ($interval * 1_000_000);
$waits = is_array($interval) ? $interval : [$interval];

while (!$this->isReady()) {

usleep($uInterval);
// For intervals specified as [0.1, 0.6, 10] we'll first wait
// 0.1s, then 0.6s, then 10s repeatedly, until the task is ready (or
// we time out).
if ($waits) {
$wait = array_shift($waits);
}

$uWait = (int) ($wait * 1_000_000);

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.0, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 1, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (latest, 2, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, 2, PredisRedisDriver, PredisRedisDriver)

Variable $wait might not be defined.

Check failure on line 134 in src/AsyncResult.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, 1, PhpAmqpLibAmqpDriver, PredisRedisDriver)

Variable $wait might not be defined.
usleep($uWait);

if (
$timeoutTime !== null
Expand Down
6 changes: 5 additions & 1 deletion src/Serializers/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

class JsonSerializer implements ISerializer {

private const JSON_ENCODE_FLAGS
= JSON_THROW_ON_ERROR
| JSON_INVALID_UTF8_SUBSTITUTE;

public function getContentType(): string {
return "application/json";
}

public function encode(mixed $input): string {
return json_encode($input, JSON_UNESCAPED_SLASHES);
return json_encode($input, self::JSON_ENCODE_FLAGS);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/suite/JsonSerializer.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Tester\Assert;

use Smuuf\CeleryForPhp\Serializers\JsonSerializer;

require __DIR__ . '/../bootstrap.php';

$s = new JsonSerializer();

//
// Serializing JSOn with broken unicode doesn't fail completely.
// Broken unicode will be substituted with "\ufffd" (REPLACEMENT CHARACTER).
//

$brokenUnicode = "\xC3Hello";
Assert::same('"\ufffdHello"', $s->encode("\xC3Hello"));

//
// JSON encoded crashes if the encoding is not successful, instead of returning
// false.
//

$unEncodable = [STDERR]; // File descriptor cannot be JSON-encoded.
Assert::exception(fn() => $s->encode($unEncodable), \JsonException::class);
Loading