Skip to content
Merged
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
57 changes: 34 additions & 23 deletions interop/src/TestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Grpc\Testing\StreamingOutputCallResponse;
use Grpc\Testing\TestServiceClient;
use Grpc\Testing\UnimplementedServiceClient;
use PHPUnit\Framework\Assert;
use Thesis\Google\Rpc\Code;
use Thesis\Grpc\Client;
use Thesis\Grpc\ClientStream;
Expand Down Expand Up @@ -65,7 +64,7 @@ public function largeUnary(): void
{
$response = $this->test->unaryCall(new SimpleRequest(responseSize: 314_159, payload: self::payload(271_828)));

Assert::assertSame(314_159, self::size($response->payload));
self::assertSame(314_159, self::size($response->payload));
}

public function clientStreaming(): void
Expand All @@ -76,7 +75,7 @@ public function clientStreaming(): void
[27_182, 8, 1_828, 45_904],
));

Assert::assertSame(74_922, $stream->close()->aggregatedPayloadSize);
self::assertSame(74_922, $stream->close()->aggregatedPayloadSize);
}

public function serverStreaming(): void
Expand All @@ -87,7 +86,7 @@ public function serverStreaming(): void
responseParameters: array_map(static fn(int $size) => new ResponseParameters($size), $sizes),
));

Assert::assertSame($sizes, self::sizes($stream));
self::assertSame($sizes, self::sizes($stream));
}

public function pingPong(): void
Expand All @@ -100,36 +99,36 @@ public function pingPong(): void
payload: self::payload($requestSize),
));

Assert::assertSame($responseSize, self::size($stream->receive()->payload));
self::assertSame($responseSize, self::size($stream->receive()->payload));
}

$stream->close();

Assert::assertSame([], self::sizes($stream));
self::assertSame([], self::sizes($stream));
}

public function emptyStream(): void
{
$stream = $this->test->fullDuplexCall();
$stream->close();

Assert::assertSame([], self::sizes($stream));
self::assertSame([], self::sizes($stream));
}

public function statusCodeAndMessage(): void
{
$status = new EchoStatus(Code::UNKNOWN->value, self::STATUS_MESSAGE);

$error = self::error(fn() => $this->test->unaryCall(new SimpleRequest(responseStatus: $status)));
Assert::assertSame([Code::UNKNOWN, self::STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);
self::assertSame([Code::UNKNOWN, self::STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);

$error = self::error(function () use ($status): void {
$stream = $this->test->fullDuplexCall();
$stream->send(new StreamingOutputCallRequest(responseStatus: $status));
$stream->close();
self::sizes($stream);
});
Assert::assertSame([Code::UNKNOWN, self::STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);
self::assertSame([Code::UNKNOWN, self::STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);
}

public function specialStatusMessage(): void
Expand All @@ -138,21 +137,21 @@ public function specialStatusMessage(): void
responseStatus: new EchoStatus(Code::UNKNOWN->value, self::SPECIAL_STATUS_MESSAGE),
)));

Assert::assertSame([Code::UNKNOWN, self::SPECIAL_STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);
self::assertSame([Code::UNKNOWN, self::SPECIAL_STATUS_MESSAGE], [$error->statusCode, $error->statusMessage]);
}

public function unimplementedMethod(): void
{
$error = self::error(fn() => $this->test->unimplementedCall(new Empty_()));

Assert::assertSame(Code::UNIMPLEMENTED, $error->statusCode);
self::assertSame(Code::UNIMPLEMENTED, $error->statusCode);
}

public function unimplementedService(): void
{
$error = self::error(fn() => new UnimplementedServiceClient($this->client)->unimplementedCall(new Empty_()));

Assert::assertSame(Code::UNIMPLEMENTED, $error->statusCode);
self::assertSame(Code::UNIMPLEMENTED, $error->statusCode);
}

public function cancelAfterBegin(): void
Expand All @@ -161,7 +160,7 @@ public function cancelAfterBegin(): void
$stream = $this->test->streamingInputCall(cancellation: $cancellation->getCancellation());
$cancellation->cancel();

Assert::assertSame(Code::CANCELLED, self::error($stream->close(...))->statusCode);
self::assertSame(Code::CANCELLED, self::error($stream->close(...))->statusCode);
}

public function cancelAfterFirstResponse(): void
Expand All @@ -175,7 +174,7 @@ public function cancelAfterFirstResponse(): void
$stream->receive();
$cancellation->cancel();

Assert::assertSame(Code::CANCELLED, self::error($stream->receive(...))->statusCode);
self::assertSame(Code::CANCELLED, self::error($stream->receive(...))->statusCode);
}

public function timeoutOnSleepingServer(): void
Expand All @@ -193,7 +192,7 @@ public function timeoutOnSleepingServer(): void
$stream->receive();
});

Assert::assertSame(Code::DEADLINE_EXCEEDED, $error->statusCode);
self::assertSame(Code::DEADLINE_EXCEEDED, $error->statusCode);
}

public function customMetadata(): void
Expand All @@ -211,7 +210,7 @@ public function customMetadata(): void
$stream->send(new SimpleRequest(responseSize: 314_159, payload: self::payload(271_828)));
$stream->close();

Assert::assertSame(314_159, self::size($stream->receive()->payload));
self::assertSame(314_159, self::size($stream->receive()->payload));
self::assertEchoedMetadata($stream);

$stream = $this->client->createStream(
Expand All @@ -224,7 +223,7 @@ public function customMetadata(): void
));
$stream->close();

Assert::assertSame([314_159], self::sizes($stream));
self::assertSame([314_159], self::sizes($stream));
self::assertEchoedMetadata($stream);
}

Expand All @@ -248,7 +247,7 @@ private function soak(bool $newChannel): void
$responseSize = $this->flags->int('soak_response_size');
$deadline = hrtime(true) + $this->flags->int('soak_overall_timeout_seconds') * 1_000_000_000;

Assert::assertSame(0, $iterations % $threads, 'soak_iterations must be divisible by soak_num_threads.');
self::assertSame(0, $iterations % $threads, 'soak_iterations must be divisible by soak_num_threads.');

$results = await(array_map(fn(int $thread) => async(function () use (
$thread,
Expand Down Expand Up @@ -307,8 +306,13 @@ private function soak(bool $newChannel): void
return [$i, $failures];
}), range(0, $threads - 1)));

Assert::assertSame($iterations, array_sum(array_column($results, 0)), 'Soak test did not complete all iterations in time.');
Assert::assertLessThanOrEqual($this->flags->int('soak_max_failures'), array_sum(array_column($results, 1)), 'Soak test failures exceeded soak_max_failures.');
self::assertSame($iterations, array_sum(array_column($results, 0)), 'Soak test did not complete all iterations in time.');

$failures = array_sum(array_column($results, 1));

if ($failures > $this->flags->int('soak_max_failures')) {
throw new \UnexpectedValueException("Soak test failures {$failures} exceeded soak_max_failures {$this->flags->int('soak_max_failures')}.");
}
}

/**
Expand All @@ -322,16 +326,23 @@ private static function error(\Closure $call): InvokeError
return $e;
}

Assert::fail('The call was expected to fail with a status.');
throw new \UnexpectedValueException('The call was expected to fail with a status.');
}

private static function assertSame(mixed $expected, mixed $actual, string $message = 'Unexpected value.'): void
{
if ($expected !== $actual) {
throw new \UnexpectedValueException(\sprintf("%s\nExpected: %s\nActual: %s", $message, var_export($expected, true), var_export($actual, true)));
}
}

/**
* @param ClientStream<*, *> $stream
*/
private static function assertEchoedMetadata(ClientStream $stream): void
{
Assert::assertSame([self::INITIAL_METADATA_VALUE], $stream->headers()[self::INITIAL_METADATA_KEY]);
Assert::assertSame([self::TRAILING_METADATA_VALUE], $stream->trailers()[self::TRAILING_METADATA_KEY]);
self::assertSame([self::INITIAL_METADATA_VALUE], $stream->headers()[self::INITIAL_METADATA_KEY]);
self::assertSame([self::TRAILING_METADATA_VALUE], $stream->trailers()[self::TRAILING_METADATA_KEY]);
}

private static function payload(int $size): Payload
Expand Down
Loading