Skip to content

Commit

Permalink
fix server
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 18, 2021
1 parent b34eda9 commit aa72d44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
19 changes: 11 additions & 8 deletions src/services/cogrpc/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const makeServiceImplementationSpy = mockSpy(
);
const mockServer = {
addService: mockSpy(jest.fn()),
bindAsync: mockSpy(jest.fn(), () => Promise.resolve()),
bindAsync: mockSpy(jest.fn(), (_netloc, _credentials, cb) => cb()),
start: mockSpy(jest.fn()),
};
jest.mock('@grpc/grpc-js', () => {
Expand Down Expand Up @@ -161,9 +161,8 @@ describe('runServer', () => {
expect.anything(),
expect.objectContaining({
statusMap: {
'': grpcHealthCheck.messages.HealthCheckResponse.ServingStatus.SERVING,
'relaynet.cogrpc.CargoRelay':
grpcHealthCheck.messages.HealthCheckResponse.ServingStatus.SERVING,
'': grpcHealthCheck.servingStatus.SERVING,
'relaynet.cogrpc.CargoRelay': grpcHealthCheck.servingStatus.SERVING,
},
}),
);
Expand All @@ -173,14 +172,18 @@ describe('runServer', () => {
await runServer();

expect(mockServer.bindAsync).toBeCalledTimes(1);
expect(mockServer.bindAsync).toBeCalledWith('0.0.0.0:8080', expect.anything());
expect(mockServer.bindAsync).toBeCalledWith(
'0.0.0.0:8080',
expect.anything(),
expect.anything(),
);
});

test('Failing to listen on specified port should result in error', async () => {
const bindError = new Error('Port is apparently taken')
mockServer.bindAsync.mockRejectedValue(bindError);
const bindError = new Error('Port is apparently taken');
mockServer.bindAsync.mockImplementation((_netloc, _credentials, cb) => cb(bindError));

await expect(() => runServer()).rejects.toEqual(bindError)
await expect(() => runServer()).rejects.toBe(bindError);
});

test('Server should use TLS with a self-issued certificate', async () => {
Expand Down
11 changes: 5 additions & 6 deletions src/services/cogrpc/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { KeyCertPair, Server, ServerCredentials } from '@grpc/grpc-js';
import { CargoRelayService } from '@relaycorp/cogrpc';
import { get as getEnvVar } from 'env-var';
import grpcHealthCheck from 'grpc-js-health-check';
import * as grpcHealthCheck from 'grpc-js-health-check';
import { Logger } from 'pino';
import * as selfsigned from 'selfsigned';
import { configureExitHandling } from '../../utilities/exitHandling';
Expand Down Expand Up @@ -46,15 +46,14 @@ export async function runServer(logger?: Logger): Promise<void> {
parcelStoreBucket,
publicAddress,
});
server.addService(CargoRelayService, serviceImplementation);
server.addService(CargoRelayService, serviceImplementation as any);

// TODO: Health checks should be probing backing services
const healthCheckService = new grpcHealthCheck.Implementation({
'': grpcHealthCheck.messages.HealthCheckResponse.ServingStatus.SERVING,
'relaynet.cogrpc.CargoRelay':
grpcHealthCheck.messages.HealthCheckResponse.ServingStatus.SERVING,
'': grpcHealthCheck.servingStatus.SERVING,
'relaynet.cogrpc.CargoRelay': grpcHealthCheck.servingStatus.SERVING,
});
server.addService(grpcHealthCheck.service, healthCheckService);
server.addService(grpcHealthCheck.service, healthCheckService as any);

const certificate = await selfIssueCertificate();
await new Promise((resolve, reject) => {
Expand Down
10 changes: 3 additions & 7 deletions src/types/grpc-js-health-check.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ declare module 'grpc-js-health-check' {
constructor(map: { readonly [key: string]: string });
}

export const messages: {
readonly HealthCheckResponse: {
readonly ServingStatus: {
readonly NOT_SERVING: any;
readonly SERVING: any;
};
};
export const servingStatus: {
readonly NOT_SERVING: any;
readonly SERVING: any;
};
}

0 comments on commit aa72d44

Please sign in to comment.