1+ /*
2+ * Copyright 2019 gRPC authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *
16+ */
17+
18+ const options = {
19+ keepCase : true ,
20+ longs : String ,
21+ enums : String ,
22+ defaults : true ,
23+ oneofs : true
24+ } ;
25+ const path = require ( 'path' ) ;
26+ const fs = require ( 'fs' ) ;
27+ const assert = require ( 'assert' ) ;
28+ const _ = require ( 'lodash' ) ;
29+ const anyGrpc = require ( '../any_grpc' ) ;
30+ const clientGrpc = anyGrpc . client ;
31+ const serverGrpc = anyGrpc . server ;
32+ const protoLoader = require ( '../../packages/proto-loader' , options ) ;
33+ const testServiceDef = protoLoader . loadSync ( __dirname + '/../proto/test_service.proto' ) ;
34+ const TestService = serverGrpc . loadPackageDefinition ( testServiceDef ) . TestService . service ;
35+ const TestServiceClient = clientGrpc . loadPackageDefinition ( testServiceDef ) . TestService ;
36+
37+ const clientCreds = clientGrpc . credentials . createInsecure ( ) ;
38+ const serverCreds = serverGrpc . ServerCredentials . createInsecure ( ) ;
39+
40+ const serviceImpl = {
41+ unary : function ( call , cb ) {
42+ cb ( null , { } ) ;
43+ } ,
44+ clientStream : function ( stream , cb ) {
45+ stream . on ( 'data' , function ( data ) { } ) ;
46+ stream . on ( 'end' , function ( ) {
47+ cb ( null , { } ) ;
48+ } ) ;
49+ } ,
50+ serverStream : function ( stream ) {
51+ stream . end ( ) ;
52+ } ,
53+ bidiStream : function ( stream ) {
54+ stream . on ( 'data' , function ( data ) { } ) ;
55+ stream . on ( 'end' , function ( ) {
56+ stream . end ( ) ;
57+ } ) ;
58+ }
59+ } ;
60+
61+ describe ( 'Reconnection' , function ( ) {
62+ let server1 ;
63+ let server2 ;
64+ let port ;
65+ before ( function ( ) {
66+ server1 = new serverGrpc . Server ( ) ;
67+ server1 . addService ( TestService , serviceImpl ) ;
68+ server2 = new serverGrpc . Server ( ) ;
69+ server2 . addService ( TestService , serviceImpl ) ;
70+ port = server1 . bind ( 'localhost:0' , serverCreds ) ;
71+ server1 . start ( ) ;
72+ client = new TestServiceClient ( `localhost:${ port } ` , clientCreds ) ;
73+ } ) ;
74+ after ( function ( ) {
75+ server1 . forceShutdown ( ) ;
76+ server2 . forceShutdown ( ) ;
77+ } ) ;
78+ it ( 'Should end with either OK or UNAVAILABLE when querying a server that is shutting down' , function ( done ) {
79+ client . unary ( { } , ( err , data ) => {
80+ assert . ifError ( err ) ;
81+ server1 . tryShutdown ( ( ) => {
82+ server2 . bind ( `localhost:${ port } ` , serverCreds ) ;
83+ server2 . start ( ) ;
84+ client . unary ( { } , ( err , data ) => {
85+ assert . ifError ( err ) ;
86+ clearInterval ( callInterval ) ;
87+ done ( ) ;
88+ } ) ;
89+ } ) ;
90+ let callInterval = setInterval ( ( ) => {
91+ client . unary ( { } , ( err , data ) => {
92+ if ( err ) {
93+ assert . strictEqual ( err . code , clientGrpc . status . UNAVAILABLE ) ;
94+ }
95+ } ) ;
96+ } , 0 ) ;
97+ } ) ;
98+ } ) ;
99+ } ) ;
0 commit comments