php: wrap getConnectivityState API

pull/2877/head
Stanley Cheung 10 years ago
parent 35ea361ff5
commit e63354a6e7
  1. 21
      src/php/ext/grpc/channel.c
  2. 12
      src/php/ext/grpc/php_grpc.c
  3. 8
      src/php/lib/Grpc/BaseStub.php
  4. 11
      src/php/tests/unit_tests/EndToEndTest.php

@ -204,6 +204,26 @@ PHP_METHOD(Channel, getTarget) {
RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1);
}
/**
* Get the connectivity state of the channel
* @param bool (optional) try to connect on the channel
* @return long The grpc connectivity state
*/
PHP_METHOD(Channel, getConnectivityState) {
wrapped_grpc_channel *channel =
(wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
bool try_to_connect;
/* "|b" == 1 optional bool */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) ==
FAILURE) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"getConnectivityState expects a bool", 1 TSRMLS_CC);
return;
}
RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped,
(int)try_to_connect));
}
/**
* Close the channel
*/
@ -219,6 +239,7 @@ PHP_METHOD(Channel, close) {
static zend_function_entry channel_methods[] = {
PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END};

@ -183,6 +183,18 @@ PHP_MINIT_FUNCTION(grpc) {
REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_CLOSE_ON_SERVER",
GRPC_OP_RECV_CLOSE_ON_SERVER, CONST_CS);
/* Register connectivity state constants */
REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_IDLE",
GRPC_CHANNEL_IDLE, CONST_CS);
REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_CONNECTING",
GRPC_CHANNEL_CONNECTING, CONST_CS);
REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_READY",
GRPC_CHANNEL_READY, CONST_CS);
REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_TRANSIENT_FAILURE",
GRPC_CHANNEL_TRANSIENT_FAILURE, CONST_CS);
REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_FATAL_FAILURE",
GRPC_CHANNEL_FATAL_FAILURE, CONST_CS);
grpc_init_call(TSRMLS_C);
grpc_init_channel(TSRMLS_C);
grpc_init_server(TSRMLS_C);

@ -74,6 +74,14 @@ class BaseStub {
return $this->channel->getTarget();
}
/**
* @param $try_to_connect bool
* @return int The grpc connectivity state
*/
public function getConnectivityState($try_to_connect = false) {
return $this->channel->getConnectivityState($try_to_connect);
}
/**
* Close the communication channel associated with this stub
*/

@ -153,4 +153,15 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{
public function testGetTarget() {
$this->assertTrue(is_string($this->channel->getTarget()));
}
/**
* @medium
*/
public function testGetConnectivityState() {
$old_state = Grpc\CHANNEL_IDLE;
$this->assertTrue($this->channel->getConnectivityState() == $old_state);
$this->assertTrue($this->channel->getConnectivityState(true) == $old_state);
usleep(500000);
$this->assertTrue($this->channel->getConnectivityState() != $old_state);
}
}

Loading…
Cancel
Save