Skip to content

Commit

Permalink
feat: support static-genereated client service
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed Aug 16, 2019
1 parent ae623c9 commit b7788cc
Show file tree
Hide file tree
Showing 14 changed files with 699 additions and 86 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__tests__/fixtures
111 changes: 111 additions & 0 deletions __tests__/client-static.test.ts
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');
});
});
});
19 changes: 11 additions & 8 deletions __tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import startGreeterServer from './fixtures/helloworld/greeter_server';
import { register, createGrpcProxy } from '../src';
import { hosts, parseKV } from './helper/etcd';
import sleep from './helper/sleep';
import { destroyGlobalPool } from '../src/client/globalClientPool';

describe('grpc proxy', () => {
describe('client dynamic - grpc proxy', () => {
let pkgDef: GrpcObject;

beforeAll(() => {
Expand All @@ -24,11 +25,11 @@ describe('grpc proxy', () => {
process.env.ETCD_NAMESPACE = 'test-services:';
});

afterAll(() => {
afterAll(async () => {
delete process.env.ETCD_NAMESPACE;
await destroyGlobalPool();
});

let proxy: GrpcObject;
let revokers: Server[];
beforeAll(async () => {
revokers = await Promise.all(
Expand All @@ -48,18 +49,20 @@ describe('grpc proxy', () => {
return server;
})
);
});

afterAll(() => {
revokers.map(item => item.forceShutdown());
});

let proxy: GrpcObject;
beforeAll(async () => {
proxy = await createGrpcProxy({
etcdHosts: hosts,
target: pkgDef.helloworld,
parseKV,
});
});

afterAll(() => {
revokers.map(item => item.forceShutdown());
});

it('round=robin', async () => {
const first = proxy.Greeter.getChannel().getTarget();

Expand Down
62 changes: 62 additions & 0 deletions __tests__/fixtures/helloworld/static_codegen/helloworld_grpc_pb.js
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 __tests__/fixtures/helloworld/static_codegen/helloworld_pb.d.ts
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;
};
}
Loading

0 comments on commit b7788cc

Please sign in to comment.