Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap onStatus to invoke BEFORE onEnd rather than after #159

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

18 changes: 9 additions & 9 deletions examples/generated/proto/examplecom/simple_service_pb_service.js

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

6 changes: 3 additions & 3 deletions examples/generated/proto/orphan_pb_service.d.ts

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

6 changes: 3 additions & 3 deletions examples/generated/proto/orphan_pb_service.js

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

24 changes: 12 additions & 12 deletions src/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,22 @@ function generateTypescriptDefinition(fileDescriptor: FileDescriptorProto, expor
printer.printLn(`interface ResponseStream<T> {`);
printer.printIndentedLn(`cancel(): void;`);
printer.printIndentedLn(`on(type: 'data', handler: (message: T) => void): ResponseStream<T>;`);
printer.printIndentedLn(`on(type: 'end', handler: () => void): ResponseStream<T>;`);
printer.printIndentedLn(`on(type: 'end', handler: (status?: Status) => void): ResponseStream<T>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): ResponseStream<T>;`);
printer.printLn(`}`);
printer.printLn(`interface RequestStream<T> {`);
printer.printIndentedLn(`write(message: T): RequestStream<T>;`);
printer.printIndentedLn(`end(): void;`);
printer.printIndentedLn(`cancel(): void;`);
printer.printIndentedLn(`on(type: 'end', handler: () => void): RequestStream<T>;`);
printer.printIndentedLn(`on(type: 'end', handler: (status?: Status) => void): RequestStream<T>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): RequestStream<T>;`);
printer.printLn(`}`);
printer.printLn(`interface BidirectionalStream<ReqT, ResT> {`);
printer.printIndentedLn(`write(message: ReqT): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`end(): void;`);
printer.printIndentedLn(`cancel(): void;`);
printer.printIndentedLn(`on(type: 'data', handler: (message: ResT) => void): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`on(type: 'end', handler: () => void): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`on(type: 'end', handler: (status?: Status) => void): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): BidirectionalStream<ReqT, ResT>;`);
printer.printLn(`}`);
printer.printEmptyLn();
Expand Down Expand Up @@ -375,10 +375,10 @@ function printServerStreamStubMethod(printer: CodePrinter, method: RPCMethodDesc
.dedent().printLn(`});`)
.dedent().printLn(`},`)
.printLn(`onEnd: function (status, statusMessage, trailers) {`)
.indent().printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler();`)
.indent().printLn(`listeners.status.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners.status.forEach(function (handler) {`)
.printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners = null;`)
Expand Down Expand Up @@ -410,10 +410,10 @@ function printClientStreamStubMethod(printer: CodePrinter, method: RPCMethodDesc
.printLn(`transport: this.options.transport`)
.dedent().printLn(`});`)
.printLn(`client.onEnd(function (status, statusMessage, trailers) {`)
.indent().printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler();`)
.indent().printLn(`listeners.status.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners.status.forEach(function (handler) {`)
.printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners = null;`)
Expand Down Expand Up @@ -455,10 +455,10 @@ function printBidirectionalStubMethod(printer: CodePrinter, method: RPCMethodDes
.printLn(`transport: this.options.transport`)
.dedent().printLn(`});`)
.printLn(`client.onEnd(function (status, statusMessage, trailers) {`)
.indent().printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler();`)
.indent().printLn(`listeners.status.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners.status.forEach(function (handler) {`)
.printLn(`listeners.end.forEach(function (handler) {`)
.indent().printLn(`handler({ code: status, details: statusMessage, metadata: trailers });`)
.dedent().printLn(`});`)
.printLn(`listeners = null;`)
Expand Down
99 changes: 76 additions & 23 deletions test/integration/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createContext, runInContext } from "vm";

import { frameRequest, StubTransportBuilder } from "../../helpers/fakeGrpcTransport";
import { ExternalChildMessage } from "../../../examples/generated/proto/othercom/external_child_message_pb";
import { SimpleService, SimpleServiceClient } from "../../../examples/generated/proto/examplecom/simple_service_pb_service";
import { SimpleService, SimpleServiceClient, Status } from "../../../examples/generated/proto/examplecom/simple_service_pb_service";
import { StreamRequest, UnaryRequest } from "../../../examples/generated/proto/examplecom/simple_service_pb";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";

Expand Down Expand Up @@ -230,17 +230,30 @@ describe("service/grpc-web", () => {
});
});

it("should invoke onEnd before onStatus", (done) => {
it("should invoke onStatus before onEnd", (done) => {
const [payload] = makePayloads("some value");
let onEndInvoked = false;
let onStatusInvoked = false;

makeClient(new StubTransportBuilder().withMessages([payload]))
.doServerStream(new StreamRequest())
.on("end", () => { onEndInvoked = true; })
.on("status", () => {
assert.ok(onEndInvoked, "onEnd callback should be invoked before onStatus");
.on("end", () => {
assert.ok(onStatusInvoked, "onStatus callback should be invoked before onEnd");
done();
});
})
.on("status", () => { onStatusInvoked = true; });
});

it("should invoke onEnd with the same status as onStatus", (done) => {
const [payload] = makePayloads("some value");
let statusFromOnStatus: Status;

makeClient(new StubTransportBuilder().withMessages([payload]))
.doServerStream(new StreamRequest())
.on("end", (endStatus) => {
assert.deepEqual(endStatus, statusFromOnStatus);
done();
})
.on("status", (status) => { statusFromOnStatus = status; });
});

it("should handle an error returned ahead of any data by the endpoint", (done) => {
Expand Down Expand Up @@ -362,16 +375,30 @@ describe("service/grpc-web", () => {
.end();
});

it("should invoke onEnd before onStatus", (done) => {
let onEndInvoked = false;
it("should invoke onStatus before onEnd", (done) => {
let onStatusInvoked = false;

makeClient(new StubTransportBuilder())
.doClientStream()
.on("end", () => {
assert.ok(onStatusInvoked, "onStatus callback should be invoked before onEnd");
done();
})
.on("status", () => { onStatusInvoked = true; })
.write(payload)
.end();
});

it("should invoke onEnd with the same status as onStatus", (done) => {
let statusFromOnStatus: Status;

makeClient(new StubTransportBuilder())
.doClientStream()
.on("end", () => { onEndInvoked = true; })
.on("status", () => {
assert.ok(onEndInvoked, "onEnd callback should be invoked before onStatus");
.on("end", (endStatus) => {
assert.deepEqual(endStatus, statusFromOnStatus);
done();
})
.on("status", (status) => { statusFromOnStatus = status; })
.write(payload)
.end();
});
Expand Down Expand Up @@ -467,16 +494,30 @@ describe("service/grpc-web", () => {
.end();
});

it("should invoke onEnd before onStatus if the client ends the stream", (done) => {
let onEndInvoked = false;
it("should invoke onStatus before onEnd if the client ends the stream", (done) => {
let onStatusInvoked = false;

makeClient(new StubTransportBuilder())
.doBidiStream()
.on("end", () => { onEndInvoked = true; })
.on("status", () => {
assert.ok(onEndInvoked, "onEnd callback should be invoked before onStatus");
.on("end", () => {
assert.ok(onStatusInvoked, "onStatus callback should be invoked before onEnd");
done();
})
.on("status", () => { onStatusInvoked = true; })
.write(payload)
.end();
});

it("should invoke onEnd with the same status as onStatus if client ends the stream", (done) => {
let statusFromOnStatus: Status;

makeClient(new StubTransportBuilder())
.doBidiStream()
.on("end", (endStatus) => {
assert.deepEqual(endStatus, statusFromOnStatus);
done();
})
.on("status", (status) => { statusFromOnStatus = status; })
.write(payload)
.end();
});
Expand All @@ -493,16 +534,28 @@ describe("service/grpc-web", () => {
.end();
});

it("should invoke onEnd before onStatus if the server ends the stream", (done) => {
let onEndInvoked = false;
it("should invoke onStatus before onEnd if the server ends the stream", (done) => {
let onStatusInvoked = false;

makeClient(new StubTransportBuilder().withMessages([ payload ]))
.doBidiStream()
.on("end", () => { onEndInvoked = true; })
.on("status", () => {
assert.ok(onEndInvoked, "onEnd callback should be invoked before onStatus");
.on("end", () => {
assert.ok(onStatusInvoked, "onStatus callback should be invoked before onEnd");
done();
});
})
.on("status", () => { onStatusInvoked = true; });
});

it("should invoke onEnd with the same status as onStatus if server ends the stream", (done) => {
let statusFromOnStatus: Status;

makeClient(new StubTransportBuilder().withMessages([ payload ]))
.doBidiStream()
.on("end", (endStatus) => {
assert.deepEqual(endStatus, statusFromOnStatus);
done();
})
.on("status", (status) => { statusFromOnStatus = status; });
});

it("should handle an error returned ahead of any data by the server", (done) => {
Expand Down