wechaty-grpc
Version:
gRPC for Wechaty
70 lines • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tstest_1 = require("tstest");
const util_1 = require("util");
const mod_js_1 = require("../src/mod.js");
(0, tstest_1.test)('HealthCheck protocol buffer', async (t) => {
const request = new mod_js_1.google.HealthCheckRequest();
const response = new mod_js_1.google.HealthCheckResponse();
response.setStatus(mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING);
await t.ok(request && response, 'should export HealCheck protobuf');
});
(0, tstest_1.test)('health check smoke testing', async (t) => {
const ENDPOINT = 'localhost:18788';
/**
* Create Server
*/
const testServer = getTestServer();
const server = new mod_js_1.grpc.Server();
server.addService(mod_js_1.google.HealthService, testServer);
await (0, util_1.promisify)(server.bindAsync
.bind(server))(ENDPOINT, mod_js_1.grpc.ServerCredentials.createInsecure());
server.start();
/**
* Create Client
*/
const client = new mod_js_1.google.HealthClient(ENDPOINT, mod_js_1.grpc.credentials.createInsecure());
const request = new mod_js_1.google.HealthCheckRequest();
const response = await (0, util_1.promisify)(client.check
.bind(client))(request);
t.equal(response.getStatus(), mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING, 'should return SERVING_STATUS_SERVING for check method');
/**
* gRPC: Stream
*/
const eventStream = client.watch(request);
let counter = 0;
const future = new Promise((resolve, reject) => {
eventStream
.on('data', (response) => {
t.equal(response.getStatus(), mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING, `should return SERVING_STATUS_SERVING for watch method with counter #${counter}`);
counter++;
})
.on('close', resolve)
.on('error', reject);
});
await future;
t.equal(counter, 2, 'should return 2 responses');
await new Promise(resolve => server.tryShutdown(resolve));
// server.forceShutdown()
});
function getTestServer() {
const puppetTestServer = {
check: (_call, callback) => {
const response = new mod_js_1.google.HealthCheckResponse();
response.setStatus(mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING);
callback(null, response);
},
watch: (call) => {
const response1 = new mod_js_1.google.HealthCheckResponse();
response1.setStatus(mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING);
call.write(response1);
const response2 = new mod_js_1.google.HealthCheckResponse();
response2.setStatus(mod_js_1.google.HealthCheckResponse.ServingStatus.SERVING);
call.write(response2);
call.end();
},
};
return puppetTestServer;
}
//# sourceMappingURL=health-check.spec.js.map