Skip to content

Commit ea26100

Browse files
committed
GRPC server tests updated:
- Added package recursive search test for support of proto-namespaces - Updated tests which were testing single level proto structure
1 parent 8d41eda commit ea26100

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

packages/microservices/server/server-grpc.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
7676
* @param grpcPkg
7777
*/
7878
public getServiceNames(grpcPkg: any): {name: string, service: any}[] {
79-
const keys = Object.keys(grpcPkg);
8079
// Define accumulator to collect all of the services available to load
8180
const services: {name: string, service: any}[] = [];
8281
// Initiate recursive services collector starting with empty name
@@ -232,8 +231,12 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
232231
nameExtended = name + '.' + key;
233232
// Take nested object
234233
const nested = grpcDefinition[key];
235-
// Check if it's in depth with service definition
236-
if (nested.hasOwnProperty('service')) {
234+
// Check if it's in depth with service definition available
235+
const requirement1 = typeof nested.service !== 'undefined';
236+
// Check if first requirement passable and service isn't a boolean value
237+
const requirement2 = requirement1 ? nested.service !== false : false;
238+
// Check if both requirements satisfied
239+
if (requirement1 && requirement2) {
237240
// Add new service object to accumulator
238241
accumulator.push({
239242
name: nameExtended,

packages/microservices/test/server/server-grpc.spec.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ describe('ServerGrpc', () => {
5353
it('should call "addService"', async () => {
5454
const serviceNames = ['test', 'test2'];
5555
sinon.stub(server, 'lookupPackage').callsFake(() => ({
56-
test: true,
57-
test2: true,
56+
test: {service: true},
57+
test2: {service: true}
5858
}));
5959
sinon.stub(server, 'getServiceNames').callsFake(() => serviceNames);
6060

@@ -73,7 +73,16 @@ describe('ServerGrpc', () => {
7373
key2: { service: true },
7474
key3: { service: false },
7575
};
76-
const expected = ['key', 'key2'];
76+
const expected = [
77+
{
78+
name: 'key',
79+
service: {service: true}
80+
},
81+
{
82+
name: 'key2',
83+
service: {service: true}
84+
}
85+
];
7786
expect(server.getServiceNames(obj)).to.be.eql(expected);
7887
});
7988
});
@@ -225,4 +234,57 @@ describe('ServerGrpc', () => {
225234
expect(server.deserialize(content)).to.equal(content);
226235
});
227236
});
237+
238+
describe('proto interfaces parser should account for package namespaces', () => {
239+
it('should parse multi-level proto package tree"', () => {
240+
const grpcPkg = {
241+
A: {
242+
C: {
243+
E: {
244+
service: {
245+
serviceName: {}
246+
}
247+
}
248+
}
249+
},
250+
B: {
251+
D: {
252+
service: {
253+
serviceName: {}
254+
}
255+
}
256+
}
257+
};
258+
const svcs = server.getServiceNames(grpcPkg);
259+
expect(svcs.length).to
260+
.be.equal(
261+
2,
262+
'Amount of services collected from namespace should be equal 2'
263+
);
264+
expect(svcs[0].name).to.be.equal('A.C.E');
265+
expect(svcs[1].name).to.be.equal('B.D');
266+
});
267+
it('should parse single level proto package tree"', () => {
268+
const grpcPkg = {
269+
A: {
270+
service: {
271+
serviceName: {}
272+
}
273+
},
274+
B: {
275+
service: {
276+
serviceName: {}
277+
}
278+
}
279+
};
280+
const services = server.getServiceNames(grpcPkg);
281+
expect(services.length).to
282+
.be.equal(
283+
2,
284+
'Amount of services collected from namespace should be equal 2'
285+
);
286+
expect(services[0].name).to.be.equal('A');
287+
expect(services[1].name).to.be.equal('B');
288+
});
289+
});
228290
});

0 commit comments

Comments
 (0)