@chatie/grpc
Version:
gRPC for Chatie
112 lines • 4.54 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 grpc_1 = __importDefault(require("grpc"));
const index_1 = require("../src/index");
const puppet_server_impl_1 = require("../tests/puppet-server-impl");
tstest_1.test('integration testing', (t) => __awaiter(void 0, void 0, void 0, function* () {
const ENDPOINT = 'localhost:18788';
const DING_DATA_LIST = ['data1', 'data2'];
const EVENT_DATA_LIST = [];
/**
* Create Server
*/
const testServer = getTestServer();
const server = new grpc_1.default.Server();
server.addService(index_1.PuppetService, testServer);
server.bind(ENDPOINT, grpc_1.default.ServerCredentials.createInsecure());
server.start();
/**
* Create Client
*/
const client = new index_1.PuppetClient(ENDPOINT, grpc_1.default.credentials.createInsecure());
/**
* gRPC: Stream
*/
const eventStream = client.event(new index_1.EventRequest());
const future = new Promise((resolve, reject) => {
eventStream
.on('data', (chunk) => {
const payload = chunk.getPayload();
EVENT_DATA_LIST.push(payload);
console.info('on(data)', payload);
if (EVENT_DATA_LIST.length === DING_DATA_LIST.length) {
resolve();
}
})
.on('error', reject);
});
/**
* gRPC: Ding
*/
for (const data of DING_DATA_LIST) {
const request = new index_1.DingRequest();
request.setData(data);
console.info('ding() for ', data);
yield util_1.default.promisify(client.ding.bind(client))(request);
}
/**
* Check Result
*/
yield future;
t.deepEqual(EVENT_DATA_LIST, DING_DATA_LIST, 'should get ding data back through event stream');
/**
* Close Client & Server
*/
eventStream.cancel();
yield new Promise(resolve => server.tryShutdown(resolve));
// server.forceShutdown()
}));
function getTestServer() {
let eventStream;
const dataQueue = [];
/**
* Implements the SayHello RPC method.
*/
const puppetTestServer = Object.assign(Object.assign({}, puppet_server_impl_1.puppetServerImpl), { ding: (call, callback) => {
const data = call.request.getData();
if (!eventStream) {
dataQueue.push(data);
}
else {
const eventResponse = new index_1.EventResponse();
eventResponse.setType(index_1.EventType.EVENT_TYPE_DONG);
eventResponse.setPayload(data);
eventStream.write(eventResponse);
}
callback(null, new index_1.DingResponse());
}, event: (streamingCall) => {
if (eventStream) {
throw new Error('eventStream can not be created again');
}
eventStream = streamingCall;
while (dataQueue.length > 0) {
const data = dataQueue.shift();
const eventResponse = new index_1.EventResponse();
eventResponse.setType(index_1.EventType.EVENT_TYPE_DONG);
eventResponse.setPayload(data);
eventStream.write(eventResponse);
}
/**
* Detect if Inexor Core is gone (GRPC disconnects)
* https://github.com/grpc/grpc/issues/8117#issuecomment-362198092
*/
eventStream.on('cancelled', () => console.info('eventStream.on(calcelled)'));
} });
return puppetTestServer;
}
//# sourceMappingURL=integration.spec.js.map
;