-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support static-genereated client service
- Loading branch information
1 parent
ae623c9
commit b7788cc
Showing
14 changed files
with
699 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__tests__/fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// @ts-ignore | ||
import startGreeterServer from './fixtures/helloworld/greeter_server'; | ||
// @ts-ignore | ||
import { GreeterClient } from './fixtures/helloworld/static_codegen/helloworld_grpc_pb'; | ||
import createClientPool from '../src/client/createClientPool'; | ||
import { hosts, parseKV } from './helper/etcd'; | ||
import { | ||
HelloRequest, | ||
HelloReply, | ||
} from './fixtures/helloworld/static_codegen/helloworld_pb'; | ||
import { Server } from 'grpc'; | ||
import { register } from '../src'; | ||
import sleep from './helper/sleep'; | ||
import { destroyGlobalPool } from '../src/client/globalClientPool'; | ||
|
||
describe('client static - grpc client pool', () => { | ||
describe('round:robin', () => { | ||
beforeAll(() => { | ||
process.env.ETCD_NAMESPACE = 'test-services:'; | ||
}); | ||
afterAll(() => { | ||
delete process.env.ETCD_NAMESPACE; | ||
}); | ||
|
||
let revokers: Server[]; | ||
beforeAll(async () => { | ||
revokers = await Promise.all( | ||
[50051, 50052].map(async port => { | ||
// const port = Math.floor(Math.random() * 1e4); | ||
// start server | ||
const server = startGreeterServer(port) as Server; | ||
|
||
await register({ | ||
server, | ||
etcdKV: { | ||
key: `test-services:helloworld.Greeter:localhost:${port}`, | ||
}, | ||
etcdHosts: hosts, | ||
}); | ||
|
||
return server; | ||
}) | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
revokers.map(item => item.forceShutdown()); | ||
await destroyGlobalPool(); | ||
}); | ||
|
||
it('round=robin', async () => { | ||
const pool = await createClientPool({ | ||
Client: GreeterClient, | ||
parseKV, | ||
etcdHosts: hosts, | ||
}); | ||
const first = pool | ||
.get() | ||
.getChannel() | ||
.getTarget(); | ||
|
||
const second = pool | ||
.get() | ||
.getChannel() | ||
.getTarget(); | ||
|
||
expect(second).not.toEqual(first); | ||
const third = pool | ||
.get() | ||
.getChannel() | ||
.getTarget(); | ||
expect(third).toEqual(first); | ||
|
||
// revoke | ||
const revoker = revokers.shift(); | ||
revoker && revoker.forceShutdown(); | ||
await sleep(200); | ||
const fourth = pool | ||
.get() | ||
.getChannel() | ||
.getTarget(); | ||
const fifth = pool | ||
.get() | ||
.getChannel() | ||
.getTarget(); | ||
|
||
expect(fourth).toEqual(fifth); | ||
expect(fourth).not.toBeUndefined(); | ||
}); | ||
|
||
it('sayHello', async () => { | ||
const pool = await createClientPool({ | ||
Client: GreeterClient, | ||
parseKV, | ||
etcdHosts: hosts, | ||
}); | ||
const result = (await new Promise((resolve, reject) => { | ||
const req = new HelloRequest(); | ||
req.setName('edvard'); | ||
pool | ||
.get() | ||
// @ts-ignore | ||
.sayHello(req, (err: Error | null, response: HelloReply) => { | ||
if (err) return reject(err); | ||
resolve(response); | ||
}); | ||
})) as HelloReply; | ||
expect(result.getMessage()).toEqual('Hello edvard'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
__tests__/fixtures/helloworld/static_codegen/helloworld_grpc_pb.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// GENERATED CODE -- DO NOT EDIT! | ||
|
||
// Original file comments: | ||
// Copyright 2015 gRPC authors. | ||
// | ||
// 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 grpc = require('grpc'); | ||
var helloworld_pb = require('./helloworld_pb.js'); | ||
|
||
function serialize_helloworld_HelloReply(arg) { | ||
if (!(arg instanceof helloworld_pb.HelloReply)) { | ||
throw new Error('Expected argument of type helloworld.HelloReply'); | ||
} | ||
return Buffer.from(arg.serializeBinary()); | ||
} | ||
|
||
function deserialize_helloworld_HelloReply(buffer_arg) { | ||
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg)); | ||
} | ||
|
||
function serialize_helloworld_HelloRequest(arg) { | ||
if (!(arg instanceof helloworld_pb.HelloRequest)) { | ||
throw new Error('Expected argument of type helloworld.HelloRequest'); | ||
} | ||
return Buffer.from(arg.serializeBinary()); | ||
} | ||
|
||
function deserialize_helloworld_HelloRequest(buffer_arg) { | ||
return helloworld_pb.HelloRequest.deserializeBinary( | ||
new Uint8Array(buffer_arg) | ||
); | ||
} | ||
|
||
// The greeting service definition. | ||
var GreeterService = (exports.GreeterService = { | ||
// Sends a greeting | ||
sayHello: { | ||
path: '/helloworld.Greeter/SayHello', | ||
requestStream: false, | ||
responseStream: false, | ||
requestType: helloworld_pb.HelloRequest, | ||
responseType: helloworld_pb.HelloReply, | ||
requestSerialize: serialize_helloworld_HelloRequest, | ||
requestDeserialize: deserialize_helloworld_HelloRequest, | ||
responseSerialize: serialize_helloworld_HelloReply, | ||
responseDeserialize: deserialize_helloworld_HelloReply, | ||
}, | ||
}); | ||
|
||
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService); |
66 changes: 66 additions & 0 deletions
66
__tests__/fixtures/helloworld/static_codegen/helloworld_pb.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// package: helloworld | ||
// file: helloworld.proto | ||
|
||
import * as jspb from 'google-protobuf'; | ||
|
||
export class HelloRequest extends jspb.Message { | ||
getName(): string; | ||
setName(value: string): void; | ||
|
||
serializeBinary(): Uint8Array; | ||
toObject(includeInstance?: boolean): HelloRequest.AsObject; | ||
static toObject( | ||
includeInstance: boolean, | ||
msg: HelloRequest | ||
): HelloRequest.AsObject; | ||
static extensions: { [key: number]: jspb.ExtensionFieldInfo<jspb.Message> }; | ||
static extensionsBinary: { | ||
[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>; | ||
}; | ||
static serializeBinaryToWriter( | ||
message: HelloRequest, | ||
writer: jspb.BinaryWriter | ||
): void; | ||
static deserializeBinary(bytes: Uint8Array): HelloRequest; | ||
static deserializeBinaryFromReader( | ||
message: HelloRequest, | ||
reader: jspb.BinaryReader | ||
): HelloRequest; | ||
} | ||
|
||
export namespace HelloRequest { | ||
export type AsObject = { | ||
name: string; | ||
}; | ||
} | ||
|
||
export class HelloReply extends jspb.Message { | ||
getMessage(): string; | ||
setMessage(value: string): void; | ||
|
||
serializeBinary(): Uint8Array; | ||
toObject(includeInstance?: boolean): HelloReply.AsObject; | ||
static toObject( | ||
includeInstance: boolean, | ||
msg: HelloReply | ||
): HelloReply.AsObject; | ||
static extensions: { [key: number]: jspb.ExtensionFieldInfo<jspb.Message> }; | ||
static extensionsBinary: { | ||
[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>; | ||
}; | ||
static serializeBinaryToWriter( | ||
message: HelloReply, | ||
writer: jspb.BinaryWriter | ||
): void; | ||
static deserializeBinary(bytes: Uint8Array): HelloReply; | ||
static deserializeBinaryFromReader( | ||
message: HelloReply, | ||
reader: jspb.BinaryReader | ||
): HelloReply; | ||
} | ||
|
||
export namespace HelloReply { | ||
export type AsObject = { | ||
message: string; | ||
}; | ||
} |
Oops, something went wrong.