@@ -19,6 +19,8 @@ import * as loader from '@grpc/proto-loader';
1919import * as assert2 from './assert2' ;
2020import * as path from 'path' ;
2121import * as grpc from '../src' ;
22+ import * as fsPromises from 'fs/promises' ;
23+ import * as os from 'os' ;
2224
2325import {
2426 GrpcObject ,
@@ -71,54 +73,77 @@ const serviceImpl = {
7173
7274export class TestServer {
7375 private server : grpc . Server ;
74- public port : number | null = null ;
76+ private target : string | null = null ;
7577 constructor ( public useTls : boolean , options ?: grpc . ServerOptions ) {
7678 this . server = new grpc . Server ( options ) ;
7779 this . server . addService ( echoService . service , serviceImpl ) ;
7880 }
79- start ( ) : Promise < void > {
80- let credentials : grpc . ServerCredentials ;
81+
82+ private getCredentials ( ) : grpc . ServerCredentials {
8183 if ( this . useTls ) {
82- credentials = grpc . ServerCredentials . createSsl ( null , [
84+ return grpc . ServerCredentials . createSsl ( null , [
8385 { private_key : key , cert_chain : cert } ,
8486 ] ) ;
8587 } else {
86- credentials = grpc . ServerCredentials . createInsecure ( ) ;
88+ return grpc . ServerCredentials . createInsecure ( ) ;
8789 }
90+ }
91+
92+ start ( ) : Promise < void > {
8893 return new Promise < void > ( ( resolve , reject ) => {
89- this . server . bindAsync ( 'localhost:0' , credentials , ( error , port ) => {
94+ this . server . bindAsync ( 'localhost:0' , this . getCredentials ( ) , ( error , port ) => {
9095 if ( error ) {
9196 reject ( error ) ;
9297 return ;
9398 }
94- this . port = port ;
99+ this . target = `localhost: ${ port } ` ;
95100 resolve ( ) ;
96101 } ) ;
97102 } ) ;
98103 }
99104
105+ startUds ( ) : Promise < void > {
106+ return fsPromises . mkdtemp ( path . join ( os . tmpdir ( ) , 'uds' ) ) . then ( dir => {
107+ return new Promise < void > ( ( resolve , reject ) => {
108+ const target = `unix://${ dir } /socket` ;
109+ this . server . bindAsync ( target , this . getCredentials ( ) , ( error , port ) => {
110+ if ( error ) {
111+ reject ( error ) ;
112+ return ;
113+ }
114+ this . target = target ;
115+ resolve ( ) ;
116+ } ) ;
117+ } ) ;
118+ } ) ;
119+ }
120+
100121 shutdown ( ) {
101122 this . server . forceShutdown ( ) ;
102123 }
124+
125+ getTarget ( ) {
126+ if ( this . target === null ) {
127+ throw new Error ( 'Server not yet started' ) ;
128+ }
129+ return this . target ;
130+ }
103131}
104132
105133export class TestClient {
106134 private client : ServiceClient ;
107- constructor ( port : number , useTls : boolean , options ?: grpc . ChannelOptions ) {
135+ constructor ( target : string , useTls : boolean , options ?: grpc . ChannelOptions ) {
108136 let credentials : grpc . ChannelCredentials ;
109137 if ( useTls ) {
110138 credentials = grpc . credentials . createSsl ( ca ) ;
111139 } else {
112140 credentials = grpc . credentials . createInsecure ( ) ;
113141 }
114- this . client = new echoService ( `localhost: ${ port } ` , credentials , options ) ;
142+ this . client = new echoService ( target , credentials , options ) ;
115143 }
116144
117145 static createFromServer ( server : TestServer , options ?: grpc . ChannelOptions ) {
118- if ( server . port === null ) {
119- throw new Error ( 'Cannot create client, server not started' ) ;
120- }
121- return new TestClient ( server . port , server . useTls , options ) ;
146+ return new TestClient ( server . getTarget ( ) , server . useTls , options ) ;
122147 }
123148
124149 waitForReady ( deadline : grpc . Deadline , callback : ( error ?: Error ) => void ) {
0 commit comments