Skip to content

Commit

Permalink
chore: add typesecurity for grpc health indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Sep 14, 2023
1 parent c6d8f7c commit 44971f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 15 additions & 11 deletions lib/health-indicator/microservice/grpc.health.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GRPCHealthIndicator } from './grpc.health';
import { checkPackages } from '../../utils/checkPackage.util';
import { Transport } from '@nestjs/microservices';
import { GrpcOptions, Transport } from '@nestjs/microservices';
import { UnhealthyResponseCodeError, TimeoutError } from '../../errors';
import { HealthCheckError } from '../../health-check/health-check.error';
jest.mock('../../utils/checkPackage.util');
Expand Down Expand Up @@ -41,21 +41,21 @@ describe('GRPCHealthIndicator', () => {
});
describe('checkService', () => {
it('should return a healthy result', async () => {
const result = await grpc.checkService('grpc', 'test');
const result = await grpc.checkService<GrpcOptions>('grpc', 'test');
expect(result).toEqual({
grpc: { servingStatus: 'SERVING', status: 'up', statusCode: 1 },
});
});

it('should correctly call the ClientProxyFactory with default', async () => {
await grpc.checkService('grpc', 'test');
await grpc.checkService<GrpcOptions>('grpc', 'test');
expect(clientProxyFactoryMock.create.mock.calls[0][0]).toEqual({
options: { package: 'grpc.health.v1', protoPath: expect.anything() },
transport: Transport.GRPC,
});
});
it('should correctly all the ClientProxyFactory with custom options', async () => {
await grpc.checkService('grpc', 'test', {
await grpc.checkService<GrpcOptions>('grpc', 'test', {
protoPath: 'test.proto',
timeout: 100,
package: 'grpc.health.v2',
Expand All @@ -70,14 +70,14 @@ describe('GRPCHealthIndicator', () => {
toPromise: (): any => Promise.resolve({ status: 0 }),
}));
try {
await grpc.checkService('grpc', 'test');
await grpc.checkService<GrpcOptions>('grpc', 'test');
} catch (err) {
expect(err instanceof UnhealthyResponseCodeError).toBeTruthy();
}
});
it('should throw an error when the timeout runs out', async () => {
try {
await grpc.checkService('grpc', 'test', { timeout: 0 });
await grpc.checkService<GrpcOptions>('grpc', 'test', { timeout: 0 });
} catch (err) {
expect(err instanceof TimeoutError).toBeTruthy();
}
Expand All @@ -87,12 +87,16 @@ describe('GRPCHealthIndicator', () => {
.fn()
.mockImplementation((): any => ({ status: 1 }));

await grpc.checkService('grpc', 'test', { healthServiceCheck });
await grpc.checkService<GrpcOptions>('grpc', 'test', {
healthServiceCheck,
});

expect(healthServiceCheck.mock.calls.length).toBe(1);
});
it('should use the custom healthServiceName', async () => {
await grpc.checkService('grpc', 'test', { healthServiceName: 'health2' });
await grpc.checkService<GrpcOptions>('grpc', 'test', {
healthServiceName: 'health2',
});
expect(grpcClientMock.getService.mock.calls[0][0]).toBe('health2');
});
it('should throw TypeError further in client.getService', async () => {
Expand All @@ -101,7 +105,7 @@ describe('GRPCHealthIndicator', () => {
throw error;
});
try {
await grpc.checkService('grpc', 'test');
await grpc.checkService<GrpcOptions>('grpc', 'test');
} catch (err) {
expect(err).toEqual(error);
}
Expand All @@ -112,14 +116,14 @@ describe('GRPCHealthIndicator', () => {
throw error;
});
try {
await grpc.checkService('grpc', 'test');
await grpc.checkService<GrpcOptions>('grpc', 'test');
} catch (err) {
expect(err instanceof HealthCheckError).toBeTruthy();
}
});
it('should throw HealthCheckError if the grpc check function fails', async () => {
try {
await grpc.checkService('grpc', 'test', {
await grpc.checkService<GrpcOptions>('grpc', 'test', {
healthServiceCheck: () => {
throw new Error('test');
},
Expand Down
6 changes: 3 additions & 3 deletions lib/health-indicator/microservice/grpc.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ export class GRPCHealthIndicator extends HealthIndicator {
* @param {CheckGRPCOptions} [options] Configuration for the request
*
* @example
* grpc.checkService('hero_service', 'hero.health.v1')
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1')
*
* @example
* // Change the timeout
* grpc.checkService('hero_service', 'hero.health.v1', { timeout: 300 })
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1', { timeout: 300 })
*
* @example
* // You can customize the health check
* // by giving these options. Nonetheless it is still seen
* // as best practice to implement the recommended GRPC specs
* grpc.checkService('hero_service', 'hero.health.v1', {
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1', {
* timeout: 500,
* package: 'grpc.health.v2',
* protoPath: join(__dirname, './protos/my-custom-health.v1'),
Expand Down

0 comments on commit 44971f1

Please sign in to comment.