Skip to content

Commit

Permalink
test(grpc): multiple services case
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Nov 4, 2024
1 parent 4cc7129 commit efc672f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export namespace hero {
id?: number;
name?: string;
}

export interface HeroService2 {
findOne2(data: HeroById, metadata?: Metadata): Promise<Hero>;
}
export interface HeroService2Client {
findOne2(options?: IClientOptions): IClientUnaryService<HeroById, Hero>;
}
}

export namespace helloworld {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GrpcMethod, MSProviderType, Provider, Provide, Inject, Init } from '@midwayjs/core';
import { helloworld, hero } from '../interface';
import { Clients } from '../../../../../src';

@Provide()
@Provider(MSProviderType.GRPC, { package: 'hero' })
export class HeroService2 implements hero.HeroService2 {

@Inject()
grpcClients: Clients;

greeterService: helloworld.GreeterClient;

@Init()
async init() {
this.greeterService = this.grpcClients.getService<helloworld.GreeterClient>('helloworld.Greeter');
}

@GrpcMethod()
async findOne2(data) {
const result = await this.greeterService.sayHello().sendMessage({
name: 'harry'
});
return {
id: 1,
name: 'bbbb-' + result.message,
};
}
}
4 changes: 4 additions & 0 deletions packages/grpc/test/fixtures/proto/hero.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ service HeroService {
rpc FindOne (HeroById) returns (Hero) {}
}

service HeroService2 {
rpc FindOne2 (HeroById) returns (Hero) {}
}

message HeroById {
int32 id = 1;
}
Expand Down
28 changes: 20 additions & 8 deletions packages/grpc/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export namespace hero {
id?: number;
name?: string;
}
export interface HeroService2Client {
findOne2(options?: IClientOptions): IClientUnaryService<HeroById, Hero>;
}
}

export namespace helloworld {
Expand Down Expand Up @@ -61,7 +64,6 @@ export namespace math {
}
}


describe('/test/index.test.ts', function () {

it('run with empty config', async () => {
Expand Down Expand Up @@ -107,19 +109,29 @@ describe('/test/index.test.ts', function () {
});

it('should create multiple grpc service in one server', async () => {
const app = await createServer('base-app-multiple-service');
let app = await createServer('base-app-multiple-service');

const service = await createGRPCConsumer<hero.HeroServiceClient>({
const opts = {
package: 'hero',
protoPath: join(__dirname, 'fixtures/proto/hero.proto'),
url: 'localhost:6565'
});

const result = await service.findOne().sendMessage({
id: 123
});
}

const service = await createGRPCConsumer<hero.HeroServiceClient>({ ...opts, });
const result = await service.findOne().sendMessage({ id: 123 });
expect(result).toEqual({ id: 1, name: 'bbbb-Hello harry' })
// await closeApp(app);

app = await createServer('base-app-multiple-service');
const service2 = await createGRPCConsumer<hero.HeroService2Client>({ service: 'HeroService2', ...opts, });
const result2 = await service2.findOne2().sendMessage({ id: 123 });
expect(result2).toEqual({ id: 1, name: 'bbbb-Hello harry' })
// await closeApp(app);

app = await createServer('base-app-multiple-service');
const service3 = await createGRPCConsumer<hero.HeroService2Client>({ ...opts, service: 'hero.HeroService2' });
const result3 = await service3.findOne2().sendMessage({ id: 123 });
expect(result3).toEqual({ id: 1, name: 'bbbb-Hello harry' })
await closeApp(app);
});

Expand Down

0 comments on commit efc672f

Please sign in to comment.