This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
/
service.js
2101 lines (1693 loc) 路 58 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.
*/
'use strict';
var assert = require('assert');
var duplexify = require('duplexify');
var extend = require('extend');
var grpc = require('grpc');
var is = require('is');
var proxyquire = require('proxyquire');
var retryRequest = require('retry-request');
var sinon = require('sinon').sandbox.create();
var through = require('through2');
var util = require('@google-cloud/common').util;
var fakeUtil = extend({}, util);
function FakeService() {
this.calledWith_ = arguments;
}
var retryRequestOverride;
function fakeRetryRequest() {
return (retryRequestOverride || retryRequest).apply(null, arguments);
}
var GrpcMetadataOverride;
var grpcLoadOverride;
var fakeGrpc = {
Metadata: function() {
if (GrpcMetadataOverride) {
return new GrpcMetadataOverride();
}
return new grpc.Metadata();
},
load: function() {
return (grpcLoadOverride || grpc.load).apply(null, arguments);
},
credentials: {
combineChannelCredentials: function() {
return {
name: 'combineChannelCredentials',
args: arguments,
};
},
createSsl: function() {
return {
name: 'createSsl',
args: arguments,
};
},
createFromGoogleCredential: function() {
return {
name: 'createFromGoogleCredential',
args: arguments,
};
},
createInsecure: function() {
return {
name: 'createInsecure',
args: arguments,
};
},
},
};
describe('GrpcService', function() {
var GrpcServiceCached;
var GrpcService;
var grpcService;
var ObjectToStructConverter;
var ROOT_DIR = '/root/dir';
var PROTO_FILE_PATH = 'filepath.proto';
var SERVICE_PATH = 'service.path';
var CONFIG = {
proto: {},
protosDir: ROOT_DIR,
protoServices: {
Service: {
path: PROTO_FILE_PATH,
service: SERVICE_PATH,
},
},
packageJson: {
name: '@google-cloud/service',
version: '0.2.0',
},
grpcMetadata: {
property: 'value',
},
};
var OPTIONS = {
maxRetries: 3,
};
var EXPECTED_API_CLIENT_HEADER = [
'gl-node/' + process.versions.node,
'gccl/' + CONFIG.packageJson.version,
'grpc/' + require('grpc/package.json').version,
].join(' ');
var MOCK_GRPC_API = {
google: {
Service: {
[SERVICE_PATH]: {},
},
},
};
before(function() {
GrpcService = proxyquire('../src/service.js', {
'@google-cloud/common': {
Service: FakeService,
util: fakeUtil,
},
grpc: fakeGrpc,
'retry-request': fakeRetryRequest,
});
GrpcServiceCached = extend(true, {}, GrpcService);
ObjectToStructConverter = GrpcService.ObjectToStructConverter;
});
beforeEach(function() {
GrpcMetadataOverride = null;
retryRequestOverride = null;
grpcLoadOverride = function() {
return MOCK_GRPC_API;
};
extend(fakeUtil, util);
extend(GrpcService, GrpcServiceCached);
grpcService = new GrpcService(CONFIG, OPTIONS);
});
afterEach(function() {
grpcLoadOverride = null;
sinon.restore();
});
describe('grpc error to http error map', function() {
it('should export grpc error map', function() {
assert.deepEqual(GrpcService.GRPC_ERROR_CODE_TO_HTTP, {
0: {
code: 200,
message: 'OK',
},
1: {
code: 499,
message: 'Client Closed Request',
},
2: {
code: 500,
message: 'Internal Server Error',
},
3: {
code: 400,
message: 'Bad Request',
},
4: {
code: 504,
message: 'Gateway Timeout',
},
5: {
code: 404,
message: 'Not Found',
},
6: {
code: 409,
message: 'Conflict',
},
7: {
code: 403,
message: 'Forbidden',
},
8: {
code: 429,
message: 'Too Many Requests',
},
9: {
code: 412,
message: 'Precondition Failed',
},
10: {
code: 409,
message: 'Conflict',
},
11: {
code: 400,
message: 'Bad Request',
},
12: {
code: 501,
message: 'Not Implemented',
},
13: {
code: 500,
message: 'Internal Server Error',
},
14: {
code: 503,
message: 'Service Unavailable',
},
15: {
code: 500,
message: 'Internal Server Error',
},
16: {
code: 401,
message: 'Unauthorized',
},
});
});
});
describe('grpc service options', function() {
it('should define the correct default options', function() {
assert.deepEqual(GrpcService.GRPC_SERVICE_OPTIONS, {
'grpc.max_send_message_length': -1,
'grpc.max_receive_message_length': -1,
'grpc.initial_reconnect_backoff_ms': 5000,
});
});
});
describe('instantiation', function() {
it('should inherit from Service', function() {
assert(grpcService instanceof FakeService);
var calledWith = grpcService.calledWith_;
assert.strictEqual(calledWith[0], CONFIG);
assert.strictEqual(calledWith[1], OPTIONS);
});
it('should set insecure credentials if using customEndpoint', function() {
var config = extend({}, CONFIG, {customEndpoint: true});
var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure');
});
it('should default grpcMetadata to empty metadata', function() {
var fakeGrpcMetadata = {
'x-goog-api-client': EXPECTED_API_CLIENT_HEADER,
};
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, val) {
this[prop] = val;
};
var config = extend({}, CONFIG);
delete config.grpcMetadata;
var grpcService = new GrpcService(config, OPTIONS);
assert.deepEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
});
it('should create and localize grpcMetadata', function() {
var fakeGrpcMetadata = extend(
{
'x-goog-api-client': EXPECTED_API_CLIENT_HEADER,
},
CONFIG.grpcMetadata
);
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, val) {
this[prop] = val;
};
var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.deepEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
});
it('should localize maxRetries', function() {
assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries);
});
it('should set the correct user-agent', function() {
var userAgent = 'user-agent/0.0.0';
fakeUtil.getUserAgentFromPackageJson = function(packageJson) {
assert.strictEqual(packageJson, CONFIG.packageJson);
return userAgent;
};
var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.strictEqual(grpcService.userAgent, userAgent);
});
it('should localize the service', function() {
assert.strictEqual(grpcService.service, CONFIG.service);
});
it('should localize an empty Map of services', function() {
assert(grpcService.activeServiceMap_ instanceof Map);
assert.strictEqual(grpcService.activeServiceMap_.size, 0);
});
it('should call grpc.load correctly', function() {
grpcLoadOverride = function(opts, format, grpcOpts) {
assert.strictEqual(opts.root, ROOT_DIR);
assert.strictEqual(opts.file, PROTO_FILE_PATH);
assert.strictEqual(format, 'proto');
assert.deepEqual(grpcOpts, {
binaryAsBase64: true,
convertFieldsToCamelCase: true,
});
return MOCK_GRPC_API;
};
var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.strictEqual(
grpcService.protos[CONFIG.service],
MOCK_GRPC_API.google[CONFIG.service]
);
});
it('should allow proto file paths to be given', function() {
grpcLoadOverride = function(opts) {
assert.strictEqual(opts.root, ROOT_DIR);
assert.strictEqual(opts.file, '../file/path.proto');
return MOCK_GRPC_API;
};
var config = extend(true, {}, CONFIG, {
protoServices: {
Service: '../file/path.proto',
},
});
var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.protos.Service, MOCK_GRPC_API.google);
});
it('should store the baseUrl properly', function() {
var fakeBaseUrl = 'a.googleapis.com';
grpcLoadOverride = function() {
return MOCK_GRPC_API;
};
var config = extend(true, {}, CONFIG, {
protoServices: {
CustomServiceName: {
path: '../file/path.proto',
baseUrl: fakeBaseUrl,
},
},
});
var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(
grpcService.protos.CustomServiceName.baseUrl,
fakeBaseUrl
);
});
it('should not run in the gcloud sandbox environment', function() {
global.GCLOUD_SANDBOX_ENV = {};
var grpcService = new GrpcService();
assert.strictEqual(grpcService, global.GCLOUD_SANDBOX_ENV);
delete global.GCLOUD_SANDBOX_ENV;
});
});
describe('decodeValue_', function() {
it('should decode a struct value', function() {
var structValue = {
kind: 'structValue',
structValue: {},
};
var decodedValue = {};
GrpcService.structToObj_ = function() {
return decodedValue;
};
assert.strictEqual(GrpcService.decodeValue_(structValue), decodedValue);
});
it('should decode a null value', function() {
var nullValue = {
kind: 'nullValue',
};
var decodedValue = null;
assert.strictEqual(GrpcService.decodeValue_(nullValue), decodedValue);
});
it('should decode a list value', function() {
var listValue = {
kind: 'listValue',
listValue: {
values: [
{
kind: 'nullValue',
},
],
},
};
assert.deepEqual(GrpcService.decodeValue_(listValue), [null]);
});
it('should return the raw value', function() {
var numberValue = {
kind: 'numberValue',
numberValue: 8,
};
assert.strictEqual(GrpcService.decodeValue_(numberValue), 8);
});
});
describe('objToStruct_', function() {
it('should convert the object using ObjectToStructConverter', function() {
var options = {};
var obj = {};
var convertedObject = {};
GrpcService.ObjectToStructConverter = function(options_) {
assert.strictEqual(options_, options);
return {
convert: function(obj_) {
assert.strictEqual(obj_, obj);
return convertedObject;
},
};
};
assert.strictEqual(
GrpcService.objToStruct_(obj, options),
convertedObject
);
});
});
describe('structToObj_', function() {
it('should convert a struct to an object', function() {
var inputValue = {};
var decodedValue = {};
var struct = {
fields: {
a: inputValue,
},
};
GrpcService.decodeValue_ = function(value) {
assert.strictEqual(value, inputValue);
return decodedValue;
};
assert.deepEqual(GrpcService.structToObj_(struct), {
a: decodedValue,
});
});
});
describe('request', function() {
var PROTO_OPTS = {service: 'service', method: 'method', timeout: 3000};
var REQ_OPTS = {reqOpts: true};
var GRPC_CREDENTIALS = {};
function ProtoService() {}
ProtoService.prototype.method = function() {};
beforeEach(function() {
grpcService.grpcCredentials = GRPC_CREDENTIALS;
grpcService.getService_ = function() {
return ProtoService;
};
});
it('should not run in the gcloud sandbox environment', function() {
global.GCLOUD_SANDBOX_ENV = true;
assert.strictEqual(grpcService.request(), global.GCLOUD_SANDBOX_ENV);
delete global.GCLOUD_SANDBOX_ENV;
});
it('should access the specified service proto object', function(done) {
retryRequestOverride = util.noop;
grpcService.getService_ = function(protoOpts) {
assert.strictEqual(protoOpts, PROTO_OPTS);
setImmediate(done);
return ProtoService;
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
it('should use and return retry-request', function() {
var retryRequestInstance = {};
retryRequestOverride = function() {
return retryRequestInstance;
};
var request = grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
assert.strictEqual(request, retryRequestInstance);
});
describe('getting gRPC credentials', function() {
beforeEach(function() {
delete grpcService.grpcCredentials;
});
describe('getting credentials error', function() {
var error = new Error('Error.');
beforeEach(function() {
grpcService.getGrpcCredentials_ = function(callback) {
callback(error);
};
});
it('should execute callback with error', function(done) {
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err) {
assert.strictEqual(err, error);
done();
});
});
});
describe('getting credentials success', function() {
var authClient = {};
beforeEach(function() {
grpcService.getGrpcCredentials_ = function(callback) {
callback(null, authClient);
};
});
it('should make the gRPC request again', function(done) {
grpcService.getService_ = function() {
assert.strictEqual(grpcService.grpcCredentials, authClient);
setImmediate(done);
return new ProtoService();
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
});
});
describe('retry strategy', function() {
var retryRequestReqOpts;
var retryRequestOptions;
var retryRequestCallback;
beforeEach(function() {
retryRequestOverride = function(reqOpts, options, callback) {
retryRequestReqOpts = reqOpts;
retryRequestOptions = options;
retryRequestCallback = callback;
};
});
it('should use retry-request', function(done) {
var error = {};
var response = {};
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err, resp) {
assert.strictEqual(err, error);
assert.strictEqual(resp, response);
done();
});
assert.strictEqual(retryRequestReqOpts, null);
assert.strictEqual(retryRequestOptions.retries, grpcService.maxRetries);
assert.strictEqual(retryRequestOptions.currentRetryAttempt, 0);
retryRequestCallback(error, response);
});
it('should retry on 429, 500, 502, and 503', function() {
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
var shouldRetryFn = retryRequestOptions.shouldRetryFn;
var retryErrors = [{code: 429}, {code: 500}, {code: 502}, {code: 503}];
var nonRetryErrors = [
{code: 200},
{code: 401},
{code: 404},
{code: 409},
{code: 412},
];
assert.strictEqual(retryErrors.every(shouldRetryFn), true);
assert.strictEqual(nonRetryErrors.every(shouldRetryFn), false);
});
it('should treat a retriable error as an HTTP response', function(done) {
var grpcError500 = {code: 2};
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts, callback) {
callback(grpcError500);
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
function onResponse(err, resp) {
assert.strictEqual(err, null);
assert.deepEqual(resp, GrpcService.GRPC_ERROR_CODE_TO_HTTP[2]);
done();
}
retryRequestOptions.request({}, onResponse);
});
it('should return grpc request', function() {
var grpcRequest = {};
grpcService.getService_ = function() {
return {
method: function() {
return grpcRequest;
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
var request = retryRequestOptions.request();
assert.strictEqual(request, grpcRequest);
});
it('should exec callback with response error as error', function(done) {
var grpcError500 = {code: 2};
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts, callback) {
callback(grpcError500);
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err, resp) {
assert.deepEqual(err, GrpcService.GRPC_ERROR_CODE_TO_HTTP[2]);
assert.strictEqual(resp, null);
done();
});
// When the gRPC error is passed to "onResponse", it will just invoke
// the callback passed to retry-request. We will check if the grpc Error
retryRequestOptions.request({}, retryRequestCallback);
});
it('should exec callback with unknown error', function(done) {
var unknownError = {a: 'a'};
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts, callback) {
callback(unknownError, null);
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err, resp) {
assert.strictEqual(err, unknownError);
assert.strictEqual(resp, null);
done();
});
// When the gRPC error is passed to "onResponse", it will just invoke
// the callback passed to retry-request. We will check if the grpc Error
retryRequestOptions.request({}, retryRequestCallback);
});
});
describe('request option decoration', function() {
describe('decoration success', function() {
it('should decorate the request', function(done) {
var decoratedRequest = {};
grpcService.decorateRequest_ = function(reqOpts) {
assert.deepEqual(reqOpts, REQ_OPTS);
return decoratedRequest;
};
grpcService.getService_ = function() {
return {
method: function(reqOpts) {
assert.strictEqual(reqOpts, decoratedRequest);
done();
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
});
describe('decoration error', function() {
var error = new Error('Error.');
it('should return a thrown error to the callback', function(done) {
grpcService.decorateRequest_ = function() {
throw error;
};
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err) {
assert.strictEqual(err, error);
done();
});
});
});
});
describe('retry request', function() {
it('should make the correct request on the service', function(done) {
grpcService.getService_ = function() {
return {
method: function(reqOpts) {
assert.deepEqual(reqOpts, REQ_OPTS);
done();
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
it('should pass the grpc metadata with the request', function(done) {
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata) {
assert.strictEqual(metadata, grpcService.grpcMetadata);
done();
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
it('should set a deadline if a timeout is provided', function(done) {
var expectedDeadlineRange = [
Date.now() + PROTO_OPTS.timeout - 250,
Date.now() + PROTO_OPTS.timeout + 250,
];
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts) {
assert(is.date(grpcOpts.deadline));
assert(grpcOpts.deadline.getTime() > expectedDeadlineRange[0]);
assert(grpcOpts.deadline.getTime() < expectedDeadlineRange[1]);
done();
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
describe('request response error', function() {
it('should look up the http status from the code', function() {
/*jshint loopfunc:true */
for (var grpcErrorCode in GrpcService.GRPC_ERROR_CODE_TO_HTTP) {
var grpcError = {code: grpcErrorCode};
var httpError = GrpcService.GRPC_ERROR_CODE_TO_HTTP[grpcErrorCode];
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts, callback) {
callback(grpcError);
},
};
};
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err) {
assert.strictEqual(err.code, httpError.code);
});
}
/*jshint loopfunc:false */
});
});
describe('request response success', function() {
var RESPONSE = {};
beforeEach(function() {
grpcService.getService_ = function() {
return {
method: function(reqOpts, metadata, grpcOpts, callback) {
callback(null, RESPONSE);
},
};
};
});
it('should execute callback with response', function(done) {
grpcService.request(PROTO_OPTS, REQ_OPTS, function(err, resp) {
assert.ifError(err);
assert.strictEqual(resp, RESPONSE);
done();
});
});
});
});
});
describe('requestStream', function() {
var PROTO_OPTS;
var REQ_OPTS = {};
var GRPC_CREDENTIALS = {};
var fakeStream;
function ProtoService() {}
beforeEach(function() {
PROTO_OPTS = {service: 'service', method: 'method', timeout: 3000};
ProtoService.prototype.method = function() {};
grpcService.grpcCredentials = GRPC_CREDENTIALS;
grpcService.baseUrl = 'http://base-url';
grpcService.proto = {};
grpcService.proto.service = ProtoService;
grpcService.getService_ = function() {
return new ProtoService();
};
fakeStream = through.obj();
retryRequestOverride = function() {
return fakeStream;
};
});
afterEach(function() {
retryRequestOverride = null;
});
it('should not run in the gcloud sandbox environment', function() {
delete grpcService.grpcCredentials;
grpcService.getGrpcCredentials_ = function() {
throw new Error('Should not be called.');
};
global.GCLOUD_SANDBOX_ENV = true;
grpcService.requestStream();
delete global.GCLOUD_SANDBOX_ENV;
});
describe('getting gRPC credentials', function() {
beforeEach(function() {
delete grpcService.grpcCredentials;
});
describe('credentials error', function() {
var error = new Error('err');
beforeEach(function() {
grpcService.getGrpcCredentials_ = function(callback) {
callback(error);
};
});
it('should execute callback with error', function(done) {
grpcService
.requestStream(PROTO_OPTS, REQ_OPTS)
.on('error', function(err) {
assert.strictEqual(err, error);
done();
});
});
});
describe('credentials success', function() {
var authClient = {};
beforeEach(function() {
grpcService.getGrpcCredentials_ = function(callback) {
callback(null, authClient);
};
});
it('should make the gRPC request again', function(done) {
grpcService.getService_ = function() {
assert.strictEqual(grpcService.grpcCredentials, authClient);
setImmediate(done);
return new ProtoService();
};
grpcService.requestStream(PROTO_OPTS, REQ_OPTS).on('error', done);
});
});
});
it('should get the proto service', function(done) {
grpcService.getService_ = function(protoOpts) {
assert.strictEqual(protoOpts, PROTO_OPTS);
setImmediate(done);
return new ProtoService();
};
grpcService.requestStream(PROTO_OPTS, REQ_OPTS, assert.ifError);
});
it('should set the deadline', function(done) {
var createDeadline = GrpcService.createDeadline_;
var fakeDeadline = createDeadline(PROTO_OPTS.timeout);
GrpcService.createDeadline_ = function(timeout) {
assert.strictEqual(timeout, PROTO_OPTS.timeout);
return fakeDeadline;
};
ProtoService.prototype.method = function(reqOpts, metadata, grpcOpts) {
assert.strictEqual(grpcOpts.deadline, fakeDeadline);
GrpcService.createDeadline_ = createDeadline;
setImmediate(done);
return through.obj();
};
retryRequestOverride = function(_, retryOpts) {
return retryOpts.request();
};
grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
});
it('should pass the grpc metadata with the request', function(done) {
ProtoService.prototype.method = function(reqOpts, metadata) {
assert.strictEqual(metadata, grpcService.grpcMetadata);
setImmediate(done);
return through.obj();
};
retryRequestOverride = function(_, retryOpts) {
return retryOpts.request();
};
grpcService.requestStream(PROTO_OPTS, REQ_OPTS);
});
describe('request option decoration', function() {
beforeEach(function() {
ProtoService.prototype.method = function() {