From f04aabc0f36e0446b9f5efb89b6d8396c83b17ab Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Wed, 5 Jun 2019 15:06:44 -0700 Subject: [PATCH 1/3] PHP: update generated code tests --- examples/node/dynamic_codegen/math_server.js | 127 ++++++++++++++++++ src/php/README.md | 4 +- .../AbstractGeneratedCodeTest.php | 2 + 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 examples/node/dynamic_codegen/math_server.js diff --git a/examples/node/dynamic_codegen/math_server.js b/examples/node/dynamic_codegen/math_server.js new file mode 100644 index 00000000000..d4eb310fc88 --- /dev/null +++ b/examples/node/dynamic_codegen/math_server.js @@ -0,0 +1,127 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +var PROTO_PATH = __dirname + '/../../../src/proto/math/math.proto'; + +var grpc = require('grpc'); +var protoLoader = require('@grpc/proto-loader'); +var packageDefinition = protoLoader.loadSync( + PROTO_PATH, + {keepCase: true, + longs: String, + enums: String, + defaults: true, + oneofs: true + }); +var math_proto = grpc.loadPackageDefinition(packageDefinition).math; + +/** + * Implements the Div RPC method. + */ +function Div(call, callback) { + var divisor = call.request.divisor; + var dividend = call.request.dividend; + if (divisor == 0) { + callback({ + code: grpc.status.INVALID_ARGUMENT, + details: 'Cannot divide by zero' + }); + } else { + setTimeout(function () { + callback(null, { + quotient: Math.floor(dividend / divisor), + remainder: dividend % divisor + }); + }, 1); // 1 millisecond, to make sure 1 microsecond timeout from test + // will hit. TODO: Consider fixing this. + } +} + +/** + * Implements the Fib RPC method. + */ +function Fib(stream) { + var previous = 0, current = 1; + for (var i = 0; i < stream.request.limit; i++) { + stream.write({ + num: current + }); + var temp = current; + current += previous; + previous = temp; + } + stream.end(); +} + +/** + * Implements the Sum RPC method. + */ +function Sum(call, callback) { + var sum = 0; + call.on('data', function(data) { + sum += parseInt(data.num); + }); + call.on('end', function() { + callback(null, { + num: sum + }); + }); +} + +/** + * Implements the DivMany RPC method. + */ +function DivMany(stream) { + stream.on('data', function(div_args) { + var divisor = div_args.divisor; + var dividend = div_args.dividend; + if (divisor == 0) { + stream.emit('error', { + code: grpc.status.INVALID_ARGUMENT, + details: 'Cannot divide by zero' + }); + } else { + stream.write({ + quotient: Math.floor(dividend / divisor), + remainder: dividend % divisor + }); + } + }); + stream.on('end', function() { + stream.end(); + }); +} + + +/** + * Starts an RPC server that receives requests for the Math service at the + * sample server port + */ +function main() { + var server = new grpc.Server(); + server.addService(math_proto.Math.service, { + Div: Div, + Fib: Fib, + Sum: Sum, + DivMany: DivMany, + }); + server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()); + server.start(); +} + +main(); diff --git a/src/php/README.md b/src/php/README.md index 5e9fa387636..69f4bbf42c2 100644 --- a/src/php/README.md +++ b/src/php/README.md @@ -294,9 +294,9 @@ Run a local server serving the math services. Please see [Node][] for how to run an example server. ```sh -$ cd grpc +$ cd grpc/examples/node $ npm install -$ node src/node/test/math/math_server.js +$ node dynamic_codegen/math_server.js ``` ### Run test client diff --git a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php index c050a26cbf5..8b655a7d83f 100644 --- a/src/php/tests/generated_code/AbstractGeneratedCodeTest.php +++ b/src/php/tests/generated_code/AbstractGeneratedCodeTest.php @@ -81,6 +81,8 @@ abstract class AbstractGeneratedCodeTest extends PHPUnit_Framework_TestCase public function testTimeout() { $div_arg = new Math\DivArgs(); + $div_arg->setDividend(7); + $div_arg->setDivisor(4); $call = self::$client->Div($div_arg, [], ['timeout' => 1]); list($response, $status) = $call->wait(); $this->assertSame(\Grpc\STATUS_DEADLINE_EXCEEDED, $status->code); From d30d538dbab5fd5f19edad59dd6282e515bb85a3 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Thu, 6 Jun 2019 13:56:46 -0700 Subject: [PATCH 2/3] Move math_server.js out of examples/ directory --- src/php/README.md | 4 ++-- .../php/tests/generated_code}/math_server.js | 2 +- src/php/tests/generated_code/package.json | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) rename {examples/node/dynamic_codegen => src/php/tests/generated_code}/math_server.js (97%) create mode 100644 src/php/tests/generated_code/package.json diff --git a/src/php/README.md b/src/php/README.md index 69f4bbf42c2..dde6152cfce 100644 --- a/src/php/README.md +++ b/src/php/README.md @@ -294,9 +294,9 @@ Run a local server serving the math services. Please see [Node][] for how to run an example server. ```sh -$ cd grpc/examples/node +$ cd grpc/src/php/tests/generated_code $ npm install -$ node dynamic_codegen/math_server.js +$ node math_server.js ``` ### Run test client diff --git a/examples/node/dynamic_codegen/math_server.js b/src/php/tests/generated_code/math_server.js similarity index 97% rename from examples/node/dynamic_codegen/math_server.js rename to src/php/tests/generated_code/math_server.js index d4eb310fc88..1492c936e13 100644 --- a/examples/node/dynamic_codegen/math_server.js +++ b/src/php/tests/generated_code/math_server.js @@ -16,7 +16,7 @@ * */ -var PROTO_PATH = __dirname + '/../../../src/proto/math/math.proto'; +var PROTO_PATH = __dirname + '/../../../proto/math/math.proto'; var grpc = require('grpc'); var protoLoader = require('@grpc/proto-loader'); diff --git a/src/php/tests/generated_code/package.json b/src/php/tests/generated_code/package.json new file mode 100644 index 00000000000..318ef4fd01f --- /dev/null +++ b/src/php/tests/generated_code/package.json @@ -0,0 +1,11 @@ +{ + "name": "grpc-examples", + "version": "0.1.0", + "dependencies": { + "@grpc/proto-loader": "^0.1.0", + "async": "^1.5.2", + "grpc": "^1.11.0", + "lodash": "^4.6.1", + "minimist": "^1.2.0" + } +} From 3b61d3efa3f3d3b4b4e9f4feed45a3e859fc827a Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Thu, 6 Jun 2019 14:05:08 -0700 Subject: [PATCH 3/3] Further simplify package.json --- src/php/tests/generated_code/package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/php/tests/generated_code/package.json b/src/php/tests/generated_code/package.json index 318ef4fd01f..5cc28be35d2 100644 --- a/src/php/tests/generated_code/package.json +++ b/src/php/tests/generated_code/package.json @@ -3,9 +3,6 @@ "version": "0.1.0", "dependencies": { "@grpc/proto-loader": "^0.1.0", - "async": "^1.5.2", - "grpc": "^1.11.0", - "lodash": "^4.6.1", - "minimist": "^1.2.0" + "grpc": "^1.11.0" } }