Skip to content

Commit

Permalink
Add additional grpcnode.ts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esilkensen committed Aug 6, 2019
1 parent ee6a3da commit a6fdf5d
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import * as proto_othercom_external_child_message_pb from "../../proto/othercom/
import * as google_protobuf_empty_pb from "google-protobuf/google/protobuf/empty_pb";
import * as grpc from "grpc";

interface MethodDefinition<RequestType, ResponseType> extends grpc.MethodDefinition<RequestType, ResponseType> {
requestType: { new(): RequestType };
responseType: { new(): ResponseType };
}

interface ISimpleServiceService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
doUnary: MethodDefinition<proto_examplecom_simple_service_pb.UnaryRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doServerStream: MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doClientStream: MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, google_protobuf_empty_pb.Empty>;
doBidiStream: MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
delete: MethodDefinition<proto_examplecom_simple_service_pb.UnaryRequest, proto_examplecom_simple_service_pb.UnaryResponse>;
doUnary: grpc.MethodDefinition<proto_examplecom_simple_service_pb.UnaryRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doServerStream: grpc.MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doClientStream: grpc.MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, google_protobuf_empty_pb.Empty>;
doBidiStream: grpc.MethodDefinition<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
delete: grpc.MethodDefinition<proto_examplecom_simple_service_pb.UnaryRequest, proto_examplecom_simple_service_pb.UnaryResponse>;
}

export const SimpleServiceService: ISimpleServiceService;
Expand Down
9 changes: 2 additions & 7 deletions examples/generated-grpc-node/proto/orphan_grpc_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
import * as proto_orphan_pb from "../proto/orphan_pb";
import * as grpc from "grpc";

interface MethodDefinition<RequestType, ResponseType> extends grpc.MethodDefinition<RequestType, ResponseType> {
requestType: { new(): RequestType };
responseType: { new(): ResponseType };
}

interface IOrphanServiceService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
doUnary: MethodDefinition<proto_orphan_pb.OrphanUnaryRequest, proto_orphan_pb.OrphanMessage>;
doStream: MethodDefinition<proto_orphan_pb.OrphanStreamRequest, proto_orphan_pb.OrphanMessage>;
doUnary: grpc.MethodDefinition<proto_orphan_pb.OrphanUnaryRequest, proto_orphan_pb.OrphanMessage>;
doStream: grpc.MethodDefinition<proto_orphan_pb.OrphanStreamRequest, proto_orphan_pb.OrphanMessage>;
}

export const OrphanServiceService: IOrphanServiceService;
Expand Down
57 changes: 27 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"mocha": "^5.2.0",
"mocha-spec-json-output-reporter": "^1.1.6",
"source-map-support": "^0.4.18",
"ts-node": "^5.0.1",
"ts-node": "^8.3.0",
"tsickle": "0.33.1",
"tslint": "^5.9.1",
"tsutils": "2.27.2",
Expand Down
9 changes: 1 addition & 8 deletions src/service/grpcnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ function generateTypeScriptDefinition(fileDescriptor: FileDescriptorProto, expor
printer.printLn(`import * as ${importDescriptor.namespace} from "${importDescriptor.path}";`);
});
printer.printLn(`import * as grpc from "grpc";`);
printer.printEmptyLn();

// Custom MethodDefinition.
printer.printLn("interface MethodDefinition<RequestType, ResponseType> extends grpc.MethodDefinition<RequestType, ResponseType> {");
printer.printIndentedLn("requestType: { new(): RequestType };");
printer.printIndentedLn("responseType: { new(): ResponseType };");
printer.printLn("}");

// Services.
serviceDescriptor.services
Expand All @@ -59,7 +52,7 @@ function printService(printer: Printer, service: RPCDescriptor) {
printer.printLn(`interface I${serviceName} extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {`);
service.methods
.forEach(method => {
const methodType = `MethodDefinition<${method.requestType}, ${method.responseType}>`;
const methodType = `grpc.MethodDefinition<${method.requestType}, ${method.responseType}>`;
printer.printIndentedLn(`${method.nameAsCamelCase}: ${methodType};`);
});
printer.printLn("}");
Expand Down
52 changes: 41 additions & 11 deletions test/integration/service/grpcnode.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { resolve } from "path";
import { readFileSync, existsSync } from "fs";
import { assert } from "chai";

import { ExternalChildMessage } from "../../../examples/generated-grpc-node/proto/othercom/external_child_message_pb";
import { SimpleServiceService } from "../../../examples/generated-grpc-node/proto/examplecom/simple_service_grpc_pb";
import { StreamRequest, UnaryRequest } from "../../../examples/generated-grpc-node/proto/examplecom/simple_service_pb";
import { SimpleServiceService, SimpleServiceClient } from "../../../examples/generated-grpc-node/proto/examplecom/simple_service_grpc_pb";
import { StreamRequest, UnaryRequest, UnaryResponse } from "../../../examples/generated-grpc-node/proto/examplecom/simple_service_pb";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";

describe("service/grpc-node", () => {
describe("generated service definitions", () => {

it("should be exported", () => {
assert.isDefined(SimpleServiceService);
});
Expand All @@ -16,32 +17,61 @@ describe("service/grpc-node", () => {
assert.strictEqual(SimpleServiceService.doUnary.path, "/examplecom.SimpleService/DoUnary");
assert.strictEqual(SimpleServiceService.doUnary.requestStream, false);
assert.strictEqual(SimpleServiceService.doUnary.responseStream, false);
assert.strictEqual(SimpleServiceService.doUnary.requestType, UnaryRequest);
assert.strictEqual(SimpleServiceService.doUnary.responseType, ExternalChildMessage);
assert.strictEqual((SimpleServiceService.doUnary as any).requestType, UnaryRequest);
assert.strictEqual((SimpleServiceService.doUnary as any).responseType, ExternalChildMessage);
});

it("should contain the expected DoServerStream method", () => {
assert.strictEqual(SimpleServiceService.doServerStream.path, "/examplecom.SimpleService/DoServerStream");
assert.strictEqual(SimpleServiceService.doServerStream.requestStream, false);
assert.strictEqual(SimpleServiceService.doServerStream.responseStream, true);
assert.strictEqual(SimpleServiceService.doServerStream.requestType, StreamRequest);
assert.strictEqual(SimpleServiceService.doServerStream.responseType, ExternalChildMessage);
assert.strictEqual((SimpleServiceService.doServerStream as any).requestType, StreamRequest);
assert.strictEqual((SimpleServiceService.doServerStream as any).responseType, ExternalChildMessage);
});

it("should contain the expected DoClientStream method", () => {
assert.strictEqual(SimpleServiceService.doClientStream.path, "/examplecom.SimpleService/DoClientStream");
assert.strictEqual(SimpleServiceService.doClientStream.requestStream, true);
assert.strictEqual(SimpleServiceService.doClientStream.responseStream, false);
assert.strictEqual(SimpleServiceService.doClientStream.requestType, StreamRequest);
assert.strictEqual(SimpleServiceService.doClientStream.responseType, Empty);
assert.strictEqual((SimpleServiceService.doClientStream as any).requestType, StreamRequest);
assert.strictEqual((SimpleServiceService.doClientStream as any).responseType, Empty);
});

it("should contain the expected DoBidiStream method", () => {
assert.strictEqual(SimpleServiceService.doBidiStream.path, "/examplecom.SimpleService/DoBidiStream");
assert.strictEqual(SimpleServiceService.doBidiStream.requestStream, true);
assert.strictEqual(SimpleServiceService.doBidiStream.responseStream, true);
assert.strictEqual(SimpleServiceService.doBidiStream.requestType, StreamRequest);
assert.strictEqual(SimpleServiceService.doBidiStream.responseType, ExternalChildMessage);
assert.strictEqual((SimpleServiceService.doBidiStream as any).requestType, StreamRequest);
assert.strictEqual((SimpleServiceService.doBidiStream as any).responseType, ExternalChildMessage);
});

it("should contain the expected Delete method", () => {
assert.strictEqual(SimpleServiceService.delete.path, "/examplecom.SimpleService/Delete");
assert.strictEqual(SimpleServiceService.delete.requestStream, false);
assert.strictEqual(SimpleServiceService.delete.responseStream, false);
assert.strictEqual((SimpleServiceService.delete as any).requestType, UnaryRequest);
assert.strictEqual((SimpleServiceService.delete as any).responseType, UnaryResponse);
});
});

it("should not output imports for namespaces that are not used in the service definition", () => {
const generatedService = readFileSync(resolve(__dirname, "../../../examples/generated-grpc-node/proto/examplecom/simple_service_grpc_pb.d.ts"), "utf8");
assert.notInclude(generatedService, "google-protobuf/google/protobuf/timestamp_pb");

const generatedProto = readFileSync(resolve(__dirname, "../../../examples/generated-grpc-node/proto/examplecom/simple_service_grpc_pb.js"), "utf8");
assert.include(generatedProto, "google-protobuf/google/protobuf/timestamp_pb");
});

it("should generate service definition files for protos that have no service definitions", () => {
assert.isTrue(existsSync(resolve(__dirname, "../../../examples/generated-grpc-node/proto/examplecom/empty_message_no_service_grpc_pb.d.ts")));
assert.isTrue(existsSync(resolve(__dirname, "../../../examples/generated-grpc-node/proto/examplecom/empty_message_no_service_grpc_pb.js")));
});

describe("generated client definition", () => {
it("should be exported", () => {
assert.isDefined(SimpleServiceClient);
});

// TODO: more tests
});
});
4 changes: 2 additions & 2 deletions test/integration/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ describe("service/grpc-web", () => {
it("should invoke onStatus before onEnd if the server ends the stream", (done) => {
let onStatusInvoked = false;

makeClient(new StubTransportBuilder().withMessages([ payload ]))
makeClient(new StubTransportBuilder().withMessages([payload]))
.doBidiStream()
.on("end", () => {
assert.ok(onStatusInvoked, "onStatus callback should be invoked before onEnd");
Expand All @@ -549,7 +549,7 @@ describe("service/grpc-web", () => {
it("should invoke onEnd with the same status as onStatus if server ends the stream", (done) => {
let statusFromOnStatus: Status;

makeClient(new StubTransportBuilder().withMessages([ payload ]))
makeClient(new StubTransportBuilder().withMessages([payload]))
.doBidiStream()
.on("end", (endStatus) => {
assert.deepEqual(endStatus, statusFromOnStatus);
Expand Down
2 changes: 1 addition & 1 deletion test/mocha-run-suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi
mocha \
--reporter mocha-spec-json-output-reporter \
--reporter-options "fileName=./test/mocha-report.json" \
--require ts-node/register/type-check \
--require ts-node/register \
--require source-map-support/register \
${MOCHA_DEBUG} \
"${TEST_SUITE}"
Expand Down
51 changes: 32 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"

arg@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.1.tgz#485f8e7c390ce4c5f78257dbea80d4be11feda4c"
integrity sha512-SlmP3fEA88MBv0PypnXZ8ZfJhwmDeIE3SP71j37AiXQBXYosPV0x6uISAaHYSlSVhmHOVkomen0tbGk6Anlebw==

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
Expand All @@ -218,10 +223,6 @@ arraybuffer.slice@~0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/arraybuffer.slice/-/arraybuffer.slice-0.0.7.tgz#3bbc4275dd584cc1b10809b89d4e8b63a69e7675"

arrify@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"

ascli@~1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/ascli/-/ascli-1.0.1.tgz#bcfa5974a62f18e81cabaeb49732ab4a88f906bc"
Expand Down Expand Up @@ -665,10 +666,15 @@ di@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/di/-/di-0.0.1.tgz#806649326ceaa7caa3306d75d985ea2748ba913c"

[email protected], diff@^3.1.0, diff@^3.2.0:
[email protected], diff@^3.2.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"

diff@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
integrity sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==

dom-serialize@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/dom-serialize/-/dom-serialize-2.2.1.tgz#562ae8999f44be5ea3076f5419dcd59eb43ac95b"
Expand Down Expand Up @@ -2057,7 +2063,7 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

[email protected], source-map-support@^0.5.3:
[email protected]:
version "0.5.9"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.9.tgz#41bc953b2534267ea2d605bccfa7bfa3111ced5f"
dependencies:
Expand All @@ -2070,6 +2076,14 @@ source-map-support@^0.4.18:
dependencies:
source-map "^0.5.6"

source-map-support@^0.5.6:
version "0.5.13"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
integrity sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map-url@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3"
Expand Down Expand Up @@ -2225,18 +2239,16 @@ to-regex@^3.0.1, to-regex@^3.0.2:
regex-not "^1.0.2"
safe-regex "^1.1.0"

ts-node@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-5.0.1.tgz#78e5d1cb3f704de1b641e43b76be2d4094f06f81"
ts-node@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.3.0.tgz#e4059618411371924a1fb5f3b125915f324efb57"
integrity sha512-dyNS/RqyVTDcmNM4NIBAeDMpsAdaQ+ojdf0GOLqE6nwJOgzEkdRNzJywhDfwnuvB10oa6NLVG1rUJQCpRN7qoQ==
dependencies:
arrify "^1.0.0"
chalk "^2.3.0"
diff "^3.1.0"
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
minimist "^1.2.0"
mkdirp "^0.5.1"
source-map-support "^0.5.3"
yn "^2.0.0"
source-map-support "^0.5.6"
yn "^3.0.0"

[email protected]:
version "0.33.1"
Expand Down Expand Up @@ -2445,6 +2457,7 @@ [email protected]:
version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"

yn@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"
yn@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 comments on commit a6fdf5d

Please sign in to comment.