Skip to content

Commit

Permalink
grpc: listen for metadata event instead of status (#1444)
Browse files Browse the repository at this point in the history
grpc: listen for metadata event instead of status
  • Loading branch information
callmehiphop authored and stephenplusplus committed Jul 22, 2016
1 parent 86e5fd8 commit 9cdcef5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/common/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,16 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {

request: function() {
return service[protoOpts.method](reqOpts, grpcOpts)
.on('status', function(status) {
var grcpStatus = GrpcService.decorateStatus_(status);

this.emit('response', grcpStatus || status);
.on('metadata', function() {
// retry-request requires a server response before it starts emitting
// data. The closest mechanism grpc provides is a metadata event, but
// this does not provide any kind of response status. So we're faking
// it here with code `0` which translates to HTTP 200.
//
// https://github.com/GoogleCloudPlatform/gcloud-node/pull/1444#discussion_r71812636
var grcpStatus = GrpcService.decorateStatus_({ code: 0 });

this.emit('response', grcpStatus);
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions test/common/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,7 @@ describe('GrpcService', function() {
);
});

it('should emit the status as a response event', function(done) {
var grpcError500 = { code: 2 };
it('should emit the metadata event as a response event', function(done) {
var fakeStream = through.obj();

ProtoService.prototype.method = function() {
Expand All @@ -1075,13 +1074,14 @@ describe('GrpcService', function() {
};

fakeStream
.on('error', done)
.on('response', function(resp) {
assert.deepEqual(resp, GrpcService.GRPC_ERROR_CODE_TO_HTTP[2]);
assert.deepEqual(resp, GrpcService.GRPC_ERROR_CODE_TO_HTTP[0]);
done();
});

grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
fakeStream.emit('status', grpcError500);
fakeStream.emit('metadata');
});

it('should emit the response error', function(done) {
Expand Down

0 comments on commit 9cdcef5

Please sign in to comment.