wechaty-grpc
Version:
gRPC for Wechaty
90 lines • 3.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tstest_1 = require("tstest");
const util_1 = __importDefault(require("util"));
const mod_js_1 = require("../src/mod.js");
const puppet_server_impl_js_1 = require("./puppet-server-impl.js");
const SERVER_ENDPOINT = '127.0.0.1:8788';
const ID = 'test-id';
const ALIAS = 'test-alias';
const contactAlias = (call, callback) => {
const id = call.request.getId();
if (call.request.hasAlias()) {
/**
* Set alias, return void
*/
const alias = call.request.getAlias();
if (alias !== ALIAS) {
throw new Error(`alias argument value error: ${alias} not equal to ${ALIAS}`);
}
callback(null, new mod_js_1.puppet.ContactAliasResponse());
}
else {
/**
* Get alias, return alias
*/
const response = new mod_js_1.puppet.ContactAliasResponse();
response.setAlias(id + ALIAS);
callback(null, response);
}
};
(0, tstest_1.test)('use StringValue to support nullable values', async (t) => {
const puppetServerImplTest = {
...puppet_server_impl_js_1.puppetServerImpl,
contactAlias,
};
const server = new mod_js_1.grpc.Server();
server.addService(mod_js_1.puppet.PuppetService, puppetServerImplTest);
try {
// FIXME: Huan(202002) if the port has been used by another grpc server, this will still bind with succeed!
// The result will be one port binded by two grpc server, and they are all working well...
const port = await util_1.default.promisify(server.bindAsync.bind(server))(SERVER_ENDPOINT, mod_js_1.grpc.ServerCredentials.createInsecure());
// console.info('port:', port)
if (port <= 0) {
t.fail(`server bind to ${SERVER_ENDPOINT} failed, port get ${port}.`);
return;
}
}
catch (e) {
/**
* Run gRPC server failed
* https://medium.com/@yuanchaodu/run-grpc-server-failed-289172dbe6e
*
* No address added out of total 1 resolved
* The above error message means the port is in use.
*/
t.fail('server bindAsync fail.');
console.error(e);
}
server.start();
const client = new mod_js_1.puppet.PuppetClient(SERVER_ENDPOINT, mod_js_1.grpc.credentials.createInsecure());
const contactAliasPromise = util_1.default.promisify(client.contactAlias.bind(client));
/**
* Get alias
*/
{
const request = new mod_js_1.puppet.ContactAliasRequest();
request.setId(ID);
const response = await contactAliasPromise(request);
const alias = response.getAlias();
t.equal(alias, ID + ALIAS, 'should get the right alias value');
}
/**
* Set alias
*/
{
const request = new mod_js_1.puppet.ContactAliasRequest();
request.setId(ID);
request.setAlias(ALIAS);
const response = await contactAliasPromise(request);
const alias = response.getAlias();
t.notOk(alias, 'should return empty for after set a value');
}
await new Promise(resolve => server.tryShutdown(resolve));
setImmediate(() => server.forceShutdown());
});
//# sourceMappingURL=nullable.spec.js.map