2016-10-15 05:53:23 +00:00
|
|
|
const uuid = require('uuid');
|
2019-11-01 18:30:06 +00:00
|
|
|
const client = require('../lib/dist');
|
2017-10-17 22:55:07 +00:00
|
|
|
const Long = require('long');
|
2016-10-15 05:53:23 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
'Test Subscribe To Stream Happy Path': function(test) {
|
|
|
|
const resolveLinkTos = false;
|
|
|
|
const numberOfPublishedEvents = 5;
|
2017-01-29 02:04:58 +00:00
|
|
|
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'));
|
|
|
|
|
2017-01-29 02:04:58 +00:00
|
|
|
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) {
|
2020-09-16 15:23:46 +00:00
|
|
|
// Filter non-compliant events (only the one we've published)
|
|
|
|
let eventData;
|
|
|
|
try {
|
|
|
|
eventData = JSON.parse(event.event.data.toString());
|
|
|
|
} catch(e){}
|
|
|
|
if (eventData && eventData.a && eventData.b){
|
2017-07-17 00:11:54 +00:00
|
|
|
receivedEvents.push(event);
|
2020-09-16 15:23:46 +00:00
|
|
|
}
|
|
|
|
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
|
2016-10-15 05:53:23 +00:00
|
|
|
}
|
|
|
|
function subscriptionDropped(subscription, reason, error) {
|
2017-01-29 02:04:58 +00:00
|
|
|
if (error) return done(error);
|
2016-10-15 05:53:23 +00:00
|
|
|
testAllPublishedEventsAppeared();
|
|
|
|
testEventsAppearedInCorrectOrder();
|
2017-01-29 02:04:58 +00:00
|
|
|
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);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual("subscription.lastEventNumber", subscription.lastEventNumber, Long.fromNumber(client.expectedVersion.emptyStream));
|
2016-10-15 05:53:23 +00:00
|
|
|
|
|
|
|
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
|
|
|
|
})
|
2017-01-29 02:04:58 +00:00
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
})
|
2016-10-15 05:53:23 +00:00
|
|
|
.catch(test.done)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-01 18:30:06 +00:00
|
|
|
require('./common/base_test').init(module.exports);
|