UNPKG

realm-object-server-enterprise

Version:

Realm Object Server Enterprise

237 lines 12.9 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); require("mocha"); const consul = require("consul"); const events_1 = require("events"); const chai_1 = require("chai"); const chai = require("chai"); const chaiAsPromised = require("chai-as-promised"); chai.use(chaiAsPromised); const sinon = require("sinon"); sinon.assert.expose(chai.assert, { prefix: "" }); const realm_object_server_1 = require("realm-object-server"); const ConsulDiscovery_1 = require("./ConsulDiscovery"); const tmp = require("tmp"); let DummyService = class DummyService { constructor() { this._tags = ["dummy"]; } }; DummyService = __decorate([ realm_object_server_1.ServiceName("dummy") ], DummyService); describe("ConsulDiscovery", () => { let config = {}; beforeEach(function () { this.tmpDir = tmp.dirSync(); this.server = new realm_object_server_1.Server(); this.discovery = new ConsulDiscovery_1.ConsulDiscovery(Object.assign({ logger: new realm_object_server_1.MuteLogger() }, config)); this.service = new DummyService(); }); describe("registerService", () => { describe("when consul is functioning", () => { it("should register a service", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.resolves(); yield chai_1.assert.isFulfilled(this.discovery.registerService(this.service, "127.0.0.1", 9080)); }); }); }); describe("when consul is not functioning", () => { it("should throw an error", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.rejects(new Error("connect EHOSTDOWN")); yield chai_1.assert.isRejected(this.discovery.registerService(this.service, "127.0.0.1", 9080), /^connect EHOSTDOWN/); }); }); }); describe("when advertising overrides are specified in configuration", () => { before(() => { config = { advertiseAddress: '10.0.130.2', advertisePortMap: { 9080: 32758 } }; }); after(() => { config = {}; }); it("should register a service with the overrides", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.resolves(); yield chai_1.assert.isFulfilled(this.discovery.registerService(this.service, "127.0.0.1", 9080)); chai_1.assert.equal(registerStub.args[0][0].address, '10.0.130.2'); chai_1.assert.equal(registerStub.args[0][0].port, 32758); }); }); }); describe("when advertising overrides are specified in environment variables", () => { before(() => { process.env.CONSUL_ADVERTISE_ADDRESS = '10.0.130.2'; process.env.CONSUL_ADVERTISE_PORT_9080 = '32758'; }); after(() => { delete process.env.CONSUL_ADVERTISE_ADDRESS; delete process.env.CONSUL_ADVERTISE_PORT_9080; }); it("should register a service with the overrides", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.resolves(); yield chai_1.assert.isFulfilled(this.discovery.registerService(this.service, "127.0.0.1", 9080)); chai_1.assert.equal(registerStub.args[0][0].address, '10.0.130.2'); chai_1.assert.equal(registerStub.args[0][0].port, 32758); }); }); }); }); describe("deregisterService", () => { describe("when consul is functioning", () => { it("should deregister a registered service", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.resolves(); yield this.discovery.registerService(this.service, "127.0.0.1", 9080); const deregisterStub = sinon.stub(this.discovery.consul.agent.service, "deregister"); deregisterStub.resolves(); yield chai_1.assert.isFulfilled(this.discovery.deregisterService(this.service)); sinon.assert.calledOnce(deregisterStub); }); }); it("should not deregister a service that is not registered", function () { return __awaiter(this, void 0, void 0, function* () { const deregisterStub = sinon.stub(this.discovery.consul.agent.service, "deregister"); deregisterStub.resolves(); yield chai_1.assert.isFulfilled(this.discovery.deregisterService(this.service)); sinon.assert.notCalled(deregisterStub); }); }); }); describe("when consul is not functioning", () => { it("should not throw an error", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.agent.service, "register"); registerStub.resolves(); yield this.discovery.registerService(this.service, "127.0.0.1", 9080); const deregisterStub = sinon.stub(this.discovery.consul.agent.service, "deregister"); deregisterStub.rejects(new Error("some consul error")); yield chai_1.assert.isFulfilled(this.discovery.deregisterService(this.service)); }); }); }); }); describe("findAll", () => { describe("when consul is functioning", () => { it("should find a registered service without a tag", function () { return __awaiter(this, void 0, void 0, function* () { const stub = sinon.stub(this.discovery.consul.catalog.service, "nodes"); stub.resolves([{ ServiceAddress: "127.0.0.1", ServiceName: "DummyService", ServicePort: 9080, ServiceTags: ["dummy"], }]); const r = yield this.discovery.findAll("DummyService"); chai_1.assert.equal(r[0].address, "127.0.0.1"); chai_1.assert.equal(r[0].name, "DummyService"); chai_1.assert.equal(r[0].port, 9080); chai_1.assert.deepEqual(r[0].tags, ["dummy"]); }); }); it("should return an empty array when looking for nonexistant tag", function () { return __awaiter(this, void 0, void 0, function* () { const stub = sinon.stub(this.discovery.consul.catalog.service, "nodes"); stub.resolves([{ ServiceAddress: "127.0.0.1", ServiceName: "DummyService", ServicePort: 9080, ServiceTags: ["dummy", "slave"], }]); const r = yield this.discovery.findAll("DummyService", ["dummy", "master"]); chai_1.assert.deepEqual(r, []); }); }); it("should return a service when looking for an existant tag", function () { return __awaiter(this, void 0, void 0, function* () { const stub = sinon.stub(this.discovery.consul.catalog.service, "nodes"); stub.resolves([{ ServiceAddress: "127.0.0.1", ServiceName: "DummyService", ServicePort: 9080, ServiceTags: ["dummy", "master"], }]); const r = yield this.discovery.findAll("DummyService", ["dummy", "master"]); chai_1.assert.equal(r[0].address, "127.0.0.1"); chai_1.assert.equal(r[0].name, "DummyService"); chai_1.assert.equal(r[0].port, 9080); chai_1.assert.deepEqual(r[0].tags, ["dummy", "master"]); }); }); }); describe("when consul not is functioning", () => { it("should reject", function () { return __awaiter(this, void 0, void 0, function* () { const registerStub = sinon.stub(this.discovery.consul.catalog.service, "nodes"); registerStub.rejects(new Error("connect EHOSTDOWN")); yield chai_1.assert.isRejected(this.discovery.find("DummyService")); }); }); }); }); describe("watchService", () => { beforeEach(function () { this.consulWatchMethodStub = sinon.stub(this.discovery.consul, "watch").callsFake(function (opts) { const consulWatchInstance = sinon.createStubInstance(consul.Watch); consulWatchInstance.consul = this; events_1.EventEmitter.call(consulWatchInstance); for (const key of Reflect.ownKeys(consulWatchInstance)) { if (key in events_1.EventEmitter.prototype && typeof consulWatchInstance[key] === 'function') { consulWatchInstance[key].callThrough(); } } return consulWatchInstance; }); }); context("consul watch lifetime", () => { it("should start a watch only after a listener has been added and end it when it has been removed", function () { const watch = this.discovery.watchService("DummyService"); chai_1.assert(this.consulWatchMethodStub.notCalled); watch.on('available', () => { }); chai_1.assert(this.consulWatchMethodStub.calledOnce); watch.removeAllListeners(); chai_1.assert(this.consulWatchMethodStub.returnValues[0].end.calledOnce); }); }); describe("when consul is not functioning", () => { it("should emit error", function () { return __awaiter(this, void 0, void 0, function* () { const watch = this.discovery.watchService("DummyService"); const promise = new Promise((resolve, reject) => { watch.once("available", resolve); watch.once("error", reject); }); chai_1.assert(this.consulWatchMethodStub.calledOnce); this.consulWatchMethodStub.returnValues[0].emit('error', new Error("connect EHOSTDOWN")); yield chai_1.assert.isRejected(promise, /connect EHOSTDOWN/); }); }); }); }); }); //# sourceMappingURL=ConsulDiscovery.spec.js.map