Skip to content

Commit e0ac517

Browse files
refactor and test
1 parent 68431b0 commit e0ac517

File tree

2 files changed

+85
-20
lines changed

2 files changed

+85
-20
lines changed

packages/common/src/grpc-service.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ function GrpcService(config, options) {
159159
this.grpcCredentials = grpc.credentials.createInsecure();
160160
}
161161

162-
this.grpcMetadata = config.grpcMetadata;
162+
this.grpcMetadata = null;
163+
164+
if (config.grpcMetadata) {
165+
this.grpcMetadata = new grpc.Metadata();
166+
167+
for (var prop in config.grpcMetadata) {
168+
if (config.grpcMetadata.hasOwnProperty(prop)) {
169+
this.grpcMetadata.add(prop, config.grpcMetadata[prop]);
170+
}
171+
}
172+
}
173+
163174
this.maxRetries = options.maxRetries;
164175

165176
var apiVersion = config.apiVersion;
@@ -230,14 +241,7 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
230241

231242
var service = this.getService_(protoOpts);
232243

233-
var metadata = null;
234-
if (this.grpcMetadata) {
235-
metadata = new grpc.Metadata();
236-
237-
for (var prop in this.grpcMetadata) {
238-
metadata.add(prop, this.grpcMetadata[prop]);
239-
}
240-
}
244+
var metadata = this.grpcMetadata;
241245

242246
var grpcOpts = {};
243247
if (is.number(protoOpts.timeout)) {
@@ -341,7 +345,7 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
341345
shouldRetryFn: GrpcService.shouldRetryRequest_,
342346

343347
request: function() {
344-
return service[protoOpts.method](reqOpts, grpcOpts)
348+
return service[protoOpts.method](reqOpts, self.grpcMetadata, grpcOpts)
345349
.on('metadata', function() {
346350
// retry-request requires a server response before it starts emitting
347351
// data. The closest mechanism grpc provides is a metadata event, but
@@ -663,12 +667,12 @@ GrpcService.prototype.getGrpcCredentials_ = function(callback) {
663667
return;
664668
}
665669

666-
var grpcCredentials = grpc.credentials.combineChannelCredentials(
670+
var credentials = grpc.credentials.combineChannelCredentials(
667671
grpc.credentials.createSsl(),
668672
grpc.credentials.createFromGoogleCredential(authClient)
669673
);
670674

671-
callback(null, grpcCredentials);
675+
callback(null, credentials);
672676
});
673677
};
674678

packages/common/test/grpc-service.js

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ function fakeRetryRequest() {
4343
return (retryRequestOverride || retryRequest).apply(null, arguments);
4444
}
4545

46+
var grpcMetadataOverride;
4647
var grpcLoadOverride;
4748
var fakeGrpc = {
49+
Metadata: function() {
50+
return new (grpcMetadataOverride || grpc.Metadata)();
51+
},
4852
load: function() {
4953
return (grpcLoadOverride || grpc.load).apply(null, arguments);
5054
},
@@ -84,7 +88,10 @@ describe('GrpcService', function() {
8488
var CONFIG = {
8589
proto: {},
8690
service: 'Service',
87-
apiVersion: 'v1'
91+
apiVersion: 'v1',
92+
grpcMetadata: {
93+
property: 'value'
94+
}
8895
};
8996

9097
var OPTIONS = {
@@ -111,6 +118,7 @@ describe('GrpcService', function() {
111118
});
112119

113120
beforeEach(function() {
121+
grpcMetadataOverride = null;
114122
retryRequestOverride = null;
115123

116124
googleProtoFilesOverride = function() {
@@ -232,6 +240,30 @@ describe('GrpcService', function() {
232240
assert.strictEqual(calledWith[1], OPTIONS);
233241
});
234242

243+
it('should default grpcMetadata to null', function() {
244+
var config = extend({}, CONFIG);
245+
delete config.grpcMetadata;
246+
247+
var grpcService = new GrpcService(config, OPTIONS);
248+
assert.strictEqual(grpcService.grpcMetadata, null);
249+
});
250+
251+
it('should create and localize grpcMetadata', function() {
252+
var fakeGrpcMetadata = {
253+
add: function(prop, value) {
254+
assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]);
255+
assert.strictEqual(value, CONFIG.grpcMetadata[prop]);
256+
}
257+
};
258+
259+
grpcMetadataOverride = function() {
260+
return fakeGrpcMetadata;
261+
};
262+
263+
var grpcService = new GrpcService(CONFIG, OPTIONS);
264+
assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
265+
});
266+
235267
it('should localize maxRetries', function() {
236268
assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries);
237269
});
@@ -738,7 +770,7 @@ describe('GrpcService', function() {
738770
grpcService.protos.Service = {
739771
service: function() {
740772
return {
741-
method: function(reqOpts, grpcOpts, callback) {
773+
method: function(reqOpts, metadata, grpcOpts, callback) {
742774
callback(grpcError500);
743775
}
744776
};
@@ -763,7 +795,7 @@ describe('GrpcService', function() {
763795
grpcService.protos.Service = {
764796
service: function() {
765797
return {
766-
method: function(reqOpts, grpcOpts, callback) {
798+
method: function(reqOpts, metadata, grpcOpts, callback) {
767799
callback(grpcError500);
768800
}
769801
};
@@ -787,7 +819,7 @@ describe('GrpcService', function() {
787819
grpcService.protos.Service = {
788820
service: function() {
789821
return {
790-
method: function(reqOpts, grpcOpts, callback) {
822+
method: function(reqOpts, metadata, grpcOpts, callback) {
791823
callback(unknownError, null);
792824
}
793825
};
@@ -821,6 +853,21 @@ describe('GrpcService', function() {
821853
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
822854
});
823855

856+
it('should pass the grpc metadata with the request', function(done) {
857+
grpcService.protos.Service = {
858+
service: function() {
859+
return {
860+
method: function(reqOpts, metadata) {
861+
assert.strictEqual(metadata, grpcService.grpcMetadata);
862+
done();
863+
}
864+
};
865+
}
866+
};
867+
868+
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
869+
});
870+
824871
it('should set a deadline if a timeout is provided', function(done) {
825872
var expectedDeadlineRange = [
826873
Date.now() + PROTO_OPTS.timeout - 250,
@@ -830,7 +877,7 @@ describe('GrpcService', function() {
830877
grpcService.protos.Service = {
831878
service: function() {
832879
return {
833-
method: function(reqOpts, grpcOpts) {
880+
method: function(reqOpts, metadata, grpcOpts) {
834881
assert(is.date(grpcOpts.deadline));
835882

836883
assert(grpcOpts.deadline.getTime() > expectedDeadlineRange[0]);
@@ -874,7 +921,7 @@ describe('GrpcService', function() {
874921
grpcService.protos.Service = {
875922
service: function() {
876923
return {
877-
method: function(reqOpts, grpcOpts, callback) {
924+
method: function(reqOpts, metadata, grpcOpts, callback) {
878925
callback(grpcError);
879926
}
880927
};
@@ -896,7 +943,7 @@ describe('GrpcService', function() {
896943
grpcService.protos.Service = {
897944
service: function() {
898945
return {
899-
method: function(reqOpts, grpcOpts, callback) {
946+
method: function(reqOpts, metadata, grpcOpts, callback) {
900947
callback(null, RESPONSE);
901948
}
902949
};
@@ -976,7 +1023,7 @@ describe('GrpcService', function() {
9761023
return fakeDeadline;
9771024
};
9781025

979-
ProtoService.prototype.method = function(reqOpts, grpcOpts) {
1026+
ProtoService.prototype.method = function(reqOpts, metadata, grpcOpts) {
9801027
assert.strictEqual(grpcOpts.deadline, fakeDeadline);
9811028

9821029
GrpcService.createDeadline_ = createDeadline;
@@ -992,6 +1039,20 @@ describe('GrpcService', function() {
9921039
grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
9931040
});
9941041

1042+
it('should pass the grpc metadata with the request', function(done) {
1043+
ProtoService.prototype.method = function(reqOpts, metadata) {
1044+
assert.strictEqual(metadata, grpcService.grpcMetadata);
1045+
setImmediate(done);
1046+
return through.obj();
1047+
};
1048+
1049+
retryRequestOverride = function(_, retryOpts) {
1050+
return retryOpts.request();
1051+
};
1052+
1053+
grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
1054+
});
1055+
9951056
describe('getting gRPC credentials', function() {
9961057
beforeEach(function() {
9971058
delete grpcService.grpcCredentials;

0 commit comments

Comments
 (0)