node-eventstore-client/test/subscribeToStream_test.js

72 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2016-10-15 05:53:23 +00:00
const uuid = require('uuid');
const client = require('../src/client');
function delay(ms) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, ms);
})
}
function delayOnlyFirst(count, action) {
if (count === 0) return action();
return delay(200)
.then(function () {
action();
})
}
2016-10-15 05:53:23 +00:00
module.exports = {
'Test Subscribe To Stream Happy Path': function(test) {
const resolveLinkTos = false;
const numberOfPublishedEvents = 5;
test.expect(numberOfPublishedEvents + 6);
2016-10-15 05:53:23 +00:00
var publishedEvents = [];
for(var i=0;i<numberOfPublishedEvents;i++)
publishedEvents.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'));
var _doneCount = 0;
function done(err) {
test.ok(!err, err ? err.stack : '');
if (++_doneCount < 2) return;
test.done();
}
2016-10-15 05:53:23 +00:00
function testAllPublishedEventsAppeared() {
test.areEqual("receivedEvents.length", receivedEvents.length, numberOfPublishedEvents);
}
function testEventsAppearedInCorrectOrder() {
for (var j = 0; j < numberOfPublishedEvents; j++)
test.ok(receivedEvents[j].originalEvent.eventId === publishedEvents[j].eventId,
"receivedEvents[" + j + "] != publishedEvents[" + j + "]");
}
var receivedEvents = [];
function eventAppeared(subscription, event) {
delayOnlyFirst(receivedEvents.length, function () {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
});
2016-10-15 05:53:23 +00:00
}
function subscriptionDropped(subscription, reason, error) {
if (error) return done(error);
2016-10-15 05:53:23 +00:00
testAllPublishedEventsAppeared();
testEventsAppearedInCorrectOrder();
done();
2016-10-15 05:53:23 +00:00
}
var self = this;
this.conn.subscribeToStream(this.testStreamName, resolveLinkTos, eventAppeared, subscriptionDropped)
.then(function(subscription) {
test.areEqual("subscription.streamId", subscription.streamId, self.testStreamName);
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, false);
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, client.expectedVersion.emptyStream);
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
})
.then(function () {
done();
})
2016-10-15 05:53:23 +00:00
.catch(test.done)
}
};
require('./common/base_test').init(module.exports);