Skip to content

Commit

Permalink
fix: clients clear test
Browse files Browse the repository at this point in the history
  • Loading branch information
chakhsu committed Jan 17, 2024
1 parent 5f41f18 commit 32d0ebe
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/client/clientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export class ClientFactory {

let cacheAddr: string = addr
if (addr === 'undefined:undefined') {
cacheAddr = this._clientAddrMap.get(name) || addr
cacheAddr = this._clientAddrMap.get(name) || ''
}
if (cacheAddr === '') {
throw new Error(`client factory create() [${name}]: address] not found.`)
}

const client = this.createReal(name, cacheAddr, credentials, channelOptions)
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export type { HandleServerStreamingCall, ServerWritableStream } from './server/s
export type { HandleBidiStreamingCall, ServerDuplexStream } from './server/bidiStreamingCallProxy'
export type { ReflectionServerOptions } from './server/serverReflection'

// export grpc-js
export type { Metadata, StatusObject } from '@grpc/grpc-js'

// export middleware
export type { ClientContext } from './client/clientContext'
export type { ServerContext } from './server/serverContext'
export type { Next } from './utils/compose'
export type { Next, MiddlewareFunction } from './utils/compose'

// export grpc-js
export type { Metadata, StatusObject } from '@grpc/grpc-js'
71 changes: 71 additions & 0 deletions test/unaryCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,75 @@ describe('gRPC Unary Call', () => {

await server.shutdown()
})

test('Should getReal() from client to server', async () => {
await loader.init()

// server
const server = await loader.initServer()
server.add('helloworld.Greeter', new Greeter())
await server.listen(addr)

// client
const clients = await loader.initClients({
services: { 'helloworld.Greeter': addr }
})
const greeterClient = clients.getReal('helloworld.Greeter')

const meta = loader.makeMetadata({
'x-test-name': 'grpcity'
})

const { status, metadata, peer, response } = (await new Promise((resolve, reject) => {
const result: any = {}
const call = greeterClient.sayGreet({ name: 'grpcity' }, meta, (err: Error, response: any) => {
if (err) {
reject(err)
} else {
result.response = response
}
})
result.peer = call.getPeer()
call.on('metadata', (metadata: Metadata) => {
result.metadata = metadata
})
call.on('status', (status: StatusObject) => {
result.status = status
resolve(result)
})
})) as any

expect(typeof response).toBe('object')
expect(response.message).toBe('hello, grpcity')
expect(status.code).toBe(0)
expect(metadata.get('x-test-name')[0]).toBe('grpcity')
expect(peer.includes(addr.host + ':' + addr.port)).toBeTruthy

await server.shutdown()
})

test('Should clear clients from client to server', async () => {
await loader.init()

// server
const server = await loader.initServer()
server.add('helloworld.Greeter', new Greeter())
await server.listen(addr)

// client
const clients = await loader.initClients({
services: { 'helloworld.Greeter': addr }
})

// clear
clients.clear()

try {
clients.get('helloworld.Greeter')
} catch (err: any) {
expect(/address/i.test(err.message)).toBeTruthy
}

await server.shutdown()
})
})

0 comments on commit 32d0ebe

Please sign in to comment.