php: wrap watchConnectivityState api

pull/2877/head
Stanley Cheung 10 years ago
parent e63354a6e7
commit a63fdd0126
  1. 41
      src/php/ext/grpc/channel.c
  2. 19
      src/php/tests/unit_tests/EndToEndTest.php

@ -51,8 +51,10 @@
#include <grpc/support/log.h>
#include <grpc/grpc_security.h>
#include "server.h"
#include "completion_queue.h"
#include "credentials.h"
#include "server.h"
#include "timeval.h"
zend_class_entry *grpc_ce_channel;
@ -224,6 +226,42 @@ PHP_METHOD(Channel, getConnectivityState) {
(int)try_to_connect));
}
/**
* Watch the connectivity state of the channel until it changed
* @param long The previous connectivity state of the channel
* @param Timeval The deadline this function should wait until
*/
PHP_METHOD(Channel, watchConnectivityState) {
wrapped_grpc_channel *channel =
(wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC);
long last_state;
zval *deadline_obj;
/* "lO" == 1 long 1 object */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
&last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"watchConnectivityState expects 1 long 1 timeval",
1 TSRMLS_CC);
return;
}
wrapped_grpc_timeval *deadline =
(wrapped_grpc_timeval *)zend_object_store_get_object(
deadline_obj TSRMLS_CC);
grpc_channel_watch_connectivity_state(
channel->wrapped, (grpc_connectivity_state)last_state,
deadline->wrapped, completion_queue, NULL);
grpc_event event = grpc_completion_queue_pluck(
completion_queue, NULL,
gpr_inf_future(GPR_CLOCK_REALTIME));
if (!event.success) {
zend_throw_exception(spl_ce_LogicException,
"watchConnectivityState failed",
1 TSRMLS_CC);
}
return;
}
/**
* Close the channel
*/
@ -240,6 +278,7 @@ 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, watchConnectivityState, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END};

@ -154,14 +154,17 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{
$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);
$this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE);
}
public function testWatchConnectivityState() {
$old_state = $this->channel->getConnectivityState(true);
$deadline = microtime(true) * 1000000 + 500000;
$this->channel->watchConnectivityState(
$old_state,
new Grpc\Timeval($deadline));
$new_state = $this->channel->getConnectivityState();
$this->assertTrue($old_state != $new_state);
}
}

Loading…
Cancel
Save