Skip to content
Snippets Groups Projects
Commit cc019af9 authored by Stanley Cheung's avatar Stanley Cheung
Browse files

add PHP timeout interop test

parent 0d6cf992
No related branches found
No related tags found
No related merge requests found
...@@ -43,9 +43,19 @@ abstract class AbstractCall { ...@@ -43,9 +43,19 @@ abstract class AbstractCall {
* Create a new Call wrapper object. * Create a new Call wrapper object.
* @param Channel $channel The channel to communicate on * @param Channel $channel The channel to communicate on
* @param string $method The method to call on the remote server * @param string $method The method to call on the remote server
* @param callback $deserialize A callback function to deserialize
* the response
* @param (optional) long $timeout Timeout in microseconds
*/ */
public function __construct(Channel $channel, $method, $deserialize) { public function __construct(Channel $channel, $method, $deserialize, $timeout = false) {
$this->call = new Call($channel, $method, Timeval::infFuture()); if ($timeout) {
$now = Timeval::now();
$delta = new Timeval($timeout);
$deadline = $now->add($delta);
} else {
$deadline = Timeval::infFuture();
}
$this->call = new Call($channel, $method, $deadline);
$this->deserialize = $deserialize; $this->deserialize = $deserialize;
$this->metadata = null; $this->metadata = null;
} }
......
...@@ -83,6 +83,21 @@ class BaseStub { ...@@ -83,6 +83,21 @@ class BaseStub {
return "https://" . $this->hostname . $service_name; return "https://" . $this->hostname . $service_name;
} }
/**
* extract $timeout from $metadata
* @param $metadata The metadata map
* @return list($metadata_copy, $timeout)
*/
private function _extract_timeout_from_metadata($metadata) {
$timeout = false;
$metadata_copy = $metadata;
if (isset($metadata['timeout'])) {
$timeout = $metadata['timeout'];
unset($metadata_copy['timeout']);
}
return array($metadata_copy, $timeout);
}
/* This class is intended to be subclassed by generated code, so all functions /* This class is intended to be subclassed by generated code, so all functions
begin with "_" to avoid name collisions. */ begin with "_" to avoid name collisions. */
...@@ -99,8 +114,8 @@ class BaseStub { ...@@ -99,8 +114,8 @@ class BaseStub {
$argument, $argument,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
$call = new UnaryCall($this->channel, $method, $deserialize); list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
$actual_metadata = $metadata; $call = new UnaryCall($this->channel, $method, $deserialize, $timeout);
$jwt_aud_uri = $this->_get_jwt_aud_uri($method); $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
...@@ -126,8 +141,8 @@ class BaseStub { ...@@ -126,8 +141,8 @@ class BaseStub {
$arguments, $arguments,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
$call = new ClientStreamingCall($this->channel, $method, $deserialize); list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
$actual_metadata = $metadata; $call = new ClientStreamingCall($this->channel, $method, $deserialize, $timeout);
$jwt_aud_uri = $this->_get_jwt_aud_uri($method); $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
...@@ -152,8 +167,8 @@ class BaseStub { ...@@ -152,8 +167,8 @@ class BaseStub {
$argument, $argument,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
$call = new ServerStreamingCall($this->channel, $method, $deserialize); list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
$actual_metadata = $metadata; $call = new ServerStreamingCall($this->channel, $method, $deserialize, $timeout);
$jwt_aud_uri = $this->_get_jwt_aud_uri($method); $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
...@@ -175,8 +190,8 @@ class BaseStub { ...@@ -175,8 +190,8 @@ class BaseStub {
public function _bidiRequest($method, public function _bidiRequest($method,
callable $deserialize, callable $deserialize,
$metadata = array()) { $metadata = array()) {
$call = new BidiStreamingCall($this->channel, $method, $deserialize); list($actual_metadata, $timeout) = $this->_extract_timeout_from_metadata($metadata);
$actual_metadata = $metadata; $call = new BidiStreamingCall($this->channel, $method, $deserialize, $timeout);
$jwt_aud_uri = $this->_get_jwt_aud_uri($method); $jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
......
...@@ -270,6 +270,24 @@ function cancelAfterFirstResponse($stub) { ...@@ -270,6 +270,24 @@ function cancelAfterFirstResponse($stub) {
'Call status was not CANCELLED'); 'Call status was not CANCELLED');
} }
function timeoutOnSleepingServer($stub) {
$call = $stub->FullDuplexCall(array('timeout' => 500000));
$request = new grpc\testing\StreamingOutputCallRequest();
$request->setResponseType(grpc\testing\PayloadType::COMPRESSABLE);
$response_parameters = new grpc\testing\ResponseParameters();
$response_parameters->setSize(8);
$request->addResponseParameters($response_parameters);
$payload = new grpc\testing\Payload();
$payload->setBody(str_repeat("\0", 9));
$request->setPayload($payload);
$call->write($request);
$response = $call->read();
hardAssert($call->getStatus()->code === Grpc\STATUS_DEADLINE_EXCEEDED,
'Call status was not DEADLINE_EXCEEDED');
}
$args = getopt('', array('server_host:', 'server_port:', 'test_case:', $args = getopt('', array('server_host:', 'server_port:', 'test_case:',
'server_host_override:', 'oauth_scope:', 'server_host_override:', 'oauth_scope:',
'default_service_account:')); 'default_service_account:'));
...@@ -341,6 +359,9 @@ switch ($args['test_case']) { ...@@ -341,6 +359,9 @@ switch ($args['test_case']) {
case 'cancel_after_first_response': case 'cancel_after_first_response':
cancelAfterFirstResponse($stub); cancelAfterFirstResponse($stub);
break; break;
case 'timeout_on_sleeping_server':
timeoutOnSleepingServer($stub);
break;
case 'service_account_creds': case 'service_account_creds':
serviceAccountCreds($stub, $args); serviceAccountCreds($stub, $args);
break; break;
......
...@@ -61,4 +61,26 @@ class TimevalTest extends PHPUnit_Framework_TestCase{ ...@@ -61,4 +61,26 @@ class TimevalTest extends PHPUnit_Framework_TestCase{
$this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now));
$this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); $this->assertLessThan(0, Grpc\Timeval::compare($now, $future));
} }
public function testNowAndAdd() {
$now = Grpc\Timeval::now();
$delta = new Grpc\Timeval(1000);
$deadline = $now->add($delta);
$this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now));
}
public function testNowAndSubtract() {
$now = Grpc\Timeval::now();
$delta = new Grpc\Timeval(1000);
$deadline = $now->subtract($delta);
$this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now));
}
public function testAddAndSubtract() {
$now = Grpc\Timeval::now();
$delta = new Grpc\Timeval(1000);
$deadline = $now->add($delta);
$back_to_now = $deadline->subtract($delta);
$this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment