diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 8f5ec24776d745a7ae16f15e7e98cfbda295cb77..1d2e43957f1a8e62b05d8b985c70a608be2873c0 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -60,18 +60,18 @@ void free_wrapped_grpc_call(void *object TSRMLS_DC) { wrapped_grpc_call *call = (wrapped_grpc_call *)object; grpc_event *event; - if (call->queue != NULL) { - grpc_completion_queue_shutdown(call->queue); - event = grpc_completion_queue_next(call->queue, gpr_inf_future); - while (event != NULL) { - if (event->type == GRPC_QUEUE_SHUTDOWN) { - break; - } + if (call->owned && call->wrapped != NULL) { + if (call->queue != NULL) { + grpc_completion_queue_shutdown(call->queue); event = grpc_completion_queue_next(call->queue, gpr_inf_future); + while (event != NULL) { + if (event->type == GRPC_QUEUE_SHUTDOWN) { + break; + } + event = grpc_completion_queue_next(call->queue, gpr_inf_future); + } + grpc_completion_queue_destroy(call->queue); } - grpc_completion_queue_destroy(call->queue); - } - if (call->owned && call->wrapped != NULL) { grpc_call_destroy(call->wrapped); } efree(call); @@ -98,14 +98,15 @@ zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct should be destroyed at the end of the object's lifecycle */ -zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned) { +zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, + bool owned) { zval *call_object; MAKE_STD_ZVAL(call_object); object_init_ex(call_object, grpc_ce_call); wrapped_grpc_call *call = (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC); call->wrapped = wrapped; - call->queue = grpc_completion_queue_create(); + call->queue = queue; return call_object; } @@ -276,7 +277,7 @@ PHP_METHOD(Call, start_batch) { grpc_metadata_array recv_trailing_metadata; grpc_status_code status; char *status_details = NULL; - size_t status_details_capacity; + size_t status_details_capacity = 0; grpc_byte_buffer *message; int cancelled; grpc_call_error error; @@ -406,14 +407,15 @@ PHP_METHOD(Call, start_batch) { ops[op_num].op = (grpc_op_type)index; op_num++; } - error = grpc_call_start_batch(call->wrapped, ops, op_num, NULL); + error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped); if (error != GRPC_CALL_OK) { zend_throw_exception(spl_ce_LogicException, "start_batch was called incorrectly", (long)error TSRMLS_CC); goto cleanup; } - event = grpc_completion_queue_pluck(call->queue, NULL, gpr_inf_future); + event = grpc_completion_queue_pluck(call->queue, call->wrapped, + gpr_inf_future); if (event->data.op_complete != GRPC_OP_OK) { zend_throw_exception(spl_ce_LogicException, "The batch failed for some reason", @@ -449,7 +451,7 @@ PHP_METHOD(Call, start_batch) { add_property_zval(recv_status, "metadata", grpc_parse_metadata_array(&recv_trailing_metadata)); add_property_long(recv_status, "code", status); - add_property_string(recv_status, "details", status_details, false); + add_property_string(recv_status, "details", status_details, true); add_property_zval(result, "status", recv_status); break; case GRPC_OP_RECV_CLOSE_ON_SERVER: diff --git a/src/php/ext/grpc/call.h b/src/php/ext/grpc/call.h index 5ec9a9f8ea6d1b0b5a3ff0fd695c88a69732354f..4a32a5c7438d416f0d5dfd49f25c9d7da032d55d 100644 --- a/src/php/ext/grpc/call.h +++ b/src/php/ext/grpc/call.h @@ -61,7 +61,8 @@ typedef struct wrapped_grpc_call { void grpc_init_call(TSRMLS_D); /* Creates a Call object that wraps the given grpc_call struct */ -zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned); +zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, + bool owned); /* Creates and returns a PHP associative array of metadata from a C array of * call metadata */ diff --git a/src/php/ext/grpc/server.c b/src/php/ext/grpc/server.c index 6a3f27ae9bc468757696ea91da2c6bbbbfbf97f6..d1362c8b51a8bf580b9b4880f5d6fd04caf57e5b 100644 --- a/src/php/ext/grpc/server.c +++ b/src/php/ext/grpc/server.c @@ -181,7 +181,8 @@ PHP_METHOD(Server, request_call) { 1 TSRMLS_CC); goto cleanup; } - add_property_zval(result, "call", grpc_php_wrap_call(call, true)); + add_property_zval(result, "call", grpc_php_wrap_call(call, server->queue, + true)); add_property_string(result, "method", details.method, true); add_property_string(result, "host", details.host, true); add_property_zval(result, "absolute_deadline", diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index af7c09ccaf4a4ac03f6710978e171b1136328d43..3e165b7213dbc98e1b63441a5c2fa225ad5363c3 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -42,8 +42,6 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ public function tearDown() { unset($this->channel); unset($this->server); - unset($this->client_queue); - unset($this->server_queue); } public function testSimpleRequestBody() { @@ -63,6 +61,7 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $event = $this->server->request_call(); $this->assertSame('dummy_method', $event->method); + $this->assertSame([], $event->metadata); $server_call = $event->call; $event = $server_call->start_batch([ @@ -81,8 +80,8 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $event = $call->start_batch([ Grpc\OP_RECV_INITIAL_METADATA => true, - Grpc\OP_RECV_STATUS_ON_CLIENT => true, - ]); + Grpc\OP_RECV_STATUS_ON_CLIENT => true + ]); $this->assertSame([], $event->metadata); $status = $event->status; @@ -134,7 +133,7 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $this->assertTrue($event->send_status); $this->assertTrue($event->send_message); $this->assertFalse($event->cancelled); - $this->assertSame($req_text, $event->read); + $this->assertSame($req_text, $event->message); $event = $call->start_batch([ Grpc\OP_RECV_INITIAL_METADATA => true, @@ -143,7 +142,7 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ ]); $this->assertSame([], $event->metadata); - $this->assertSame($reply_text, $event->read); + $this->assertSame($reply_text, $event->message); $status = $event->status; $this->assertSame([], $status->metadata); $this->assertSame(Grpc\STATUS_OK, $status->code);