@axelspringer/graphql-google-pubsub
Version:
A graphql-subscriptions PubSub Engine using Google PubSub
467 lines • 21.8 kB
JavaScript
;
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = __importDefault(require("chai"));
var chai_as_promised_1 = __importDefault(require("chai-as-promised"));
var simple_mock_1 = require("simple-mock");
var iterall_1 = require("iterall");
var index_1 = require("../index");
var pubsub_1 = require("@google-cloud/pubsub");
chai_1.default.use(chai_as_promised_1.default);
var expect = chai_1.default.expect;
function getMockedGooglePubSub(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.topic2SubName, topic2SubName = _c === void 0 ? undefined : _c, _d = _b.commonMessageHandler, commonMessageHandler = _d === void 0 ? undefined : _d;
var listener;
var ackSpy = simple_mock_1.spy(function () { });
var removeListenerSpy = simple_mock_1.spy(function (event, cb) {
if (event === 'message') {
listener = null;
}
});
var addListenerSpy = simple_mock_1.spy(function (event, cb) {
if (event === 'message') {
listener = cb;
}
});
var subscriptionMock = {
exists: simple_mock_1.spy(function (subName) { return Promise.resolve(true); }),
on: addListenerSpy,
removeListener: removeListenerSpy
};
var topicMock = {
publish: simple_mock_1.spy(function (data, attributes) { return listener && listener({ ack: ackSpy, data: data, attributes: attributes }); }),
createSubscription: simple_mock_1.spy(function (subName) { return Promise.resolve([subscriptionMock]); })
};
var mockGooglePubSubClient = new pubsub_1.PubSub();
simple_mock_1.mock(mockGooglePubSubClient, 'topic', function () { return topicMock; });
simple_mock_1.mock(mockGooglePubSubClient, 'subscription', function () { return subscriptionMock; });
var pubSub = new index_1.GooglePubSub(undefined, topic2SubName, commonMessageHandler, mockGooglePubSubClient);
return { pubSub: pubSub, addListenerSpy: addListenerSpy, removeListenerSpy: removeListenerSpy, topicMock: topicMock };
}
var asyncMessageHandler = function () { return new Promise(function (resolve) { return setTimeout(resolve, 0); }); };
var asyncSubscribe = asyncMessageHandler;
describe('GooglePubSub', function () {
afterEach(function () { simple_mock_1.restore(); });
it('can subscribe to specific topic and called when a message is published on it', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
pubSub
.subscribe('Posts', function (message) {
try {
expect(message.data.toString()).to.equals('test');
done();
}
catch (e) {
done(e);
}
})
.then(function (subId) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
expect(subId).to.be.a('number');
pubSub.publish('Posts', 'test');
return [4, asyncMessageHandler()];
case 1:
_a.sent();
pubSub.unsubscribe(subId);
return [2];
}
});
}); });
});
it('can unsubscribe from specific topic', function (done) {
var _a = getMockedGooglePubSub(), pubSub = _a.pubSub, removeListenerSpy = _a.removeListenerSpy;
pubSub
.subscribe('Posts', function () { return null; })
.then(function (subId) {
pubSub.unsubscribe(subId);
try {
expect(removeListenerSpy.callCount).to.equals(2);
expect(removeListenerSpy.calls[0].args[0]).to.equals('message');
expect(removeListenerSpy.calls[1].args[0]).to.equals('error');
done();
}
catch (e) {
done(e);
}
});
});
it('cleans up correctly the memory when unsubscribing', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
Promise.all([pubSub.subscribe('Posts', function () { return null; }), pubSub.subscribe('Posts', function () { return null; })]).then(function (_a) {
var subId = _a[0], secondSubId = _a[1];
try {
expect(pubSub.clientId2GoogleSubNameAndClientCallback[subId]).not.to.be.an('undefined');
pubSub.unsubscribe(subId);
expect(pubSub.clientId2GoogleSubNameAndClientCallback[subId]).to.be.an('undefined');
expect(function () { return pubSub.unsubscribe(subId); }).to.throw("There is no subscription of id \"" + subId + "\"");
pubSub.unsubscribe(secondSubId);
done();
}
catch (e) {
done(e);
}
});
});
it("will not unsubscribe from the topic if there is another subscriber on it's subscriber list", function (done) {
var _a = getMockedGooglePubSub(), pubSub = _a.pubSub, removeListenerSpy = _a.removeListenerSpy;
var subscriptionPromises = [
pubSub.subscribe('Posts', function () {
done('Not supposed to be triggered');
}),
pubSub.subscribe('Posts', function (message) {
try {
expect(message.data.toString()).to.equals('test');
done();
}
catch (e) {
done(e);
}
})
];
Promise.all(subscriptionPromises).then(function (subIds) { return __awaiter(_this, void 0, void 0, function () {
var e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
expect(subIds.length).to.equals(2);
pubSub.unsubscribe(subIds[0]);
expect(removeListenerSpy.callCount).to.equals(0);
pubSub.publish('Posts', 'test');
return [4, asyncMessageHandler()];
case 1:
_a.sent();
pubSub.unsubscribe(subIds[1]);
expect(removeListenerSpy.callCount).to.equals(2);
expect(removeListenerSpy.calls[0].args[0]).to.equals('message');
expect(removeListenerSpy.calls[1].args[0]).to.equals('error');
return [3, 3];
case 2:
e_1 = _a.sent();
done(e_1);
return [3, 3];
case 3: return [2];
}
});
}); });
});
it('will subscribe to topic only once', function (done) {
var _a = getMockedGooglePubSub(), pubSub = _a.pubSub, addListenerSpy = _a.addListenerSpy;
var onMessage = function () { return null; };
var subscriptionPromises = [pubSub.subscribe('Posts', onMessage), pubSub.subscribe('Posts', onMessage)];
Promise.all(subscriptionPromises).then(function (subIds) {
try {
expect(subIds.length).to.equals(2);
expect(addListenerSpy.callCount).to.equals(2);
expect(addListenerSpy.calls[0].args[0]).to.equals('message');
expect(addListenerSpy.calls[1].args[0]).to.equals('error');
pubSub.unsubscribe(subIds[0]);
pubSub.unsubscribe(subIds[1]);
done();
}
catch (e) {
done(e);
}
});
});
it('can have multiple subscribers and all will be called when a message is published to this topic', function (done) {
var _a = getMockedGooglePubSub(), pubSub = _a.pubSub, addListenerSpy = _a.addListenerSpy, removeListenerSpy = _a.removeListenerSpy;
var onMessageSpy = simple_mock_1.spy(function () { return null; });
var subscriptionPromises = [
pubSub.subscribe('Posts', onMessageSpy),
pubSub.subscribe('Posts', onMessageSpy)
];
Promise.all(subscriptionPromises).then(function (subIds) { return __awaiter(_this, void 0, void 0, function () {
var e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
expect(subIds.length).to.equals(2);
pubSub.publish('Posts', 'test');
return [4, asyncMessageHandler()];
case 1:
_a.sent();
expect(onMessageSpy.callCount).to.equals(2);
onMessageSpy.calls.forEach(function (call) {
expect(call.args[0].data.toString()).to.equals('test');
});
pubSub.unsubscribe(subIds[0]);
pubSub.unsubscribe(subIds[1]);
done();
return [3, 3];
case 2:
e_2 = _a.sent();
done(e_2);
return [3, 3];
case 3: return [2];
}
});
}); });
});
it('can publish objects as well', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
pubSub
.subscribe('Posts', function (message) {
try {
expect(JSON.parse(message.data.toString())).to.have.property('comment', 'This is amazing');
done();
}
catch (e) {
done(e);
}
})
.then(function (subId) { return __awaiter(_this, void 0, void 0, function () {
var e_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
pubSub.publish('Posts', { comment: 'This is amazing' });
return [4, asyncMessageHandler()];
case 1:
_a.sent();
pubSub.unsubscribe(subId);
return [3, 3];
case 2:
e_3 = _a.sent();
done(e_3);
return [3, 3];
case 3: return [2];
}
});
}); });
});
it('can use custom message handler', function (done) {
var dateReviver = function (key, value) {
var isISO8601Z = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
if (typeof value === 'string' && isISO8601Z.test(value)) {
var tempDateNumber = Date.parse(value);
if (!isNaN(tempDateNumber)) {
return new Date(tempDateNumber);
}
}
return value;
};
function commonMessageHandler(message) {
var parsedMessage;
try {
parsedMessage = JSON.parse(message.data.toString(), dateReviver);
}
catch (e) {
parsedMessage = message;
}
return parsedMessage;
}
var pubSub = getMockedGooglePubSub({ commonMessageHandler: commonMessageHandler }).pubSub;
var validTime = new Date();
var invalidTime = '2018-13-01T12:00:00Z';
pubSub
.subscribe('Times', function (message) {
try {
expect(message).to.have.property('invalidTime', invalidTime);
expect(message).to.have.property('validTime');
expect(message.validTime.getTime()).to.equals(validTime.getTime());
done();
}
catch (e) {
done(e);
}
})
.then(function (subId) {
try {
pubSub.publish('Times', { validTime: validTime, invalidTime: invalidTime });
asyncMessageHandler().then(function () { return pubSub.unsubscribe(subId); });
}
catch (e) {
done(e);
}
});
});
it('throws if you try to unsubscribe with an unknown id', function () {
var pubSub = getMockedGooglePubSub().pubSub;
return expect(function () { return pubSub.unsubscribe(123); }).to.throw('There is no subscription of id "123"');
});
it('can use a transform function to convert the topic name given into more explicit subscription name', function (done) {
var topic2SubName = function (topicName, _a) {
var subscriptionSufix = _a.subscriptionSufix;
return topicName + "-" + subscriptionSufix;
};
var pubSub = getMockedGooglePubSub({ topic2SubName: topic2SubName }).pubSub;
var validateMessage = function (message) {
try {
expect(message.data.toString()).to.equals('test');
done();
}
catch (e) {
done(e);
}
};
pubSub
.subscribe('comments', validateMessage, { subscriptionSufix: 'graphql-google-pubsub-subscription' })
.then(function (subId) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
pubSub.publish('comments', 'test');
return [4, asyncMessageHandler()];
case 1:
_a.sent();
pubSub.unsubscribe(subId);
return [2];
}
});
}); });
});
it('subscribe passes through subscription options to google pub sub subscribe', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, pubSub, topicMock, subOpts, subId, firstCall;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = getMockedGooglePubSub(), pubSub = _a.pubSub, topicMock = _a.topicMock;
subOpts = {
messageRetentionDuration: { seconds: 1337 },
};
return [4, pubSub.subscribe('options', function () { }, subOpts)];
case 1:
subId = _b.sent();
pubSub.unsubscribe(subId);
firstCall = topicMock.createSubscription.calls[0];
expect(firstCall.args[0]).to.equal('options-subscription');
expect(firstCall.args[1]).to.deep.equal(subOpts);
return [2];
}
});
}); });
});
describe('PubSubAsyncIterator', function () {
afterEach(function () { simple_mock_1.restore(); });
it('should expose valid asyncIterator for a specific event', function () {
var pubSub = getMockedGooglePubSub().pubSub;
var eventName = 'test';
var iterator = pubSub.asyncIterator(eventName);
expect(iterator).to.exist;
expect(iterall_1.isAsyncIterable(iterator)).to.be.true;
});
it('should trigger event on asyncIterator when published', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
var eventName = 'test';
var iterator = pubSub.asyncIterator(eventName);
iterator.next().then(function (result) {
expect(result).to.exist;
expect(result.value).to.exist;
expect(result.done).to.exist;
done();
});
asyncSubscribe().then(function () { return pubSub.publish(eventName, { test: true }); });
});
it('should not trigger event on asyncIterator when publishing other event', function () {
var pubSub = getMockedGooglePubSub().pubSub;
var eventName = 'test2';
var iterator = pubSub.asyncIterator('test');
var triggerSpy = simple_mock_1.spy(function () { return undefined; });
iterator.next().then(triggerSpy);
pubSub.publish(eventName, { test: true });
expect(triggerSpy.callCount).to.equal(0);
});
it('register to multiple events', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
var eventName = 'test2';
var iterator = pubSub.asyncIterator(['test', 'test2']);
var triggerSpy = simple_mock_1.spy(function () { return undefined; });
iterator.next().then(function () {
triggerSpy();
expect(triggerSpy.callCount).to.be.gte(1);
done();
});
asyncSubscribe().then(function () { return pubSub.publish(eventName, { test: true }); });
});
it('should not trigger event on asyncIterator already returned', function (done) {
var pubSub = getMockedGooglePubSub().pubSub;
var eventName = 'test';
var iterator = pubSub.asyncIterator(eventName);
iterator
.next()
.then(function (result) {
expect(result).to.exist;
expect(result.value).to.exist;
expect(JSON.parse(result.value.data.toString()).test).to.equal('word');
expect(result.done).to.be.false;
})
.then(function () {
return iterator.next().then(function (result) {
expect(result).to.exist;
expect(result.value).not.to.exist;
expect(result.done).to.be.true;
done();
});
});
asyncSubscribe()
.then(function () { return pubSub.publish(eventName, { test: 'word' }); })
.then(asyncMessageHandler)
.then(function () { return iterator.return(); })
.then(function () { return pubSub.publish(eventName, { test: true }); });
});
it('passes through subscription options to google pub sub subscribe', function () { return __awaiter(_this, void 0, void 0, function () {
var _a, pubSub, topicMock, subOpts, iterator, firstCall;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = getMockedGooglePubSub(), pubSub = _a.pubSub, topicMock = _a.topicMock;
subOpts = {
messageRetentionDuration: { seconds: 42 },
};
return [4, pubSub.asyncIterator('iter-options', subOpts)];
case 1:
iterator = _b.sent();
return [4, iterator.return()];
case 2:
_b.sent();
firstCall = topicMock.createSubscription.calls[0];
expect(firstCall.args[0]).to.equal('iter-options-subscription');
expect(firstCall.args[1]).to.deep.equal(subOpts);
return [2];
}
});
}); });
});
//# sourceMappingURL=tests.js.map