2016-10-15 05:53:23 +00:00
|
|
|
var util = require('util');
|
|
|
|
var uuid = require('uuid');
|
2019-11-01 18:30:06 +00:00
|
|
|
var client = require('../lib/dist');
|
2016-10-15 05:53:23 +00:00
|
|
|
const allCredentials = new client.UserCredentials("admin", "changeit");
|
|
|
|
|
|
|
|
function createRandomEvent() {
|
|
|
|
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
|
|
|
|
}
|
|
|
|
|
2017-07-17 00:11:54 +00:00
|
|
|
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 = {
|
2017-02-25 22:12:13 +00:00
|
|
|
'Test Subscribe to All From (Start)': function(test) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.expect(6);
|
2016-10-15 05:53:23 +00:00
|
|
|
var self = this;
|
|
|
|
var liveProcessing = false;
|
|
|
|
var catchUpEvents = [];
|
|
|
|
var liveEvents = [];
|
2017-01-29 02:04:58 +00:00
|
|
|
var _doneCount = 0;
|
|
|
|
function done(err) {
|
|
|
|
test.ok(!err, err ? err.stack : '');
|
|
|
|
_doneCount++;
|
|
|
|
if (_doneCount < 2) return;
|
2017-07-17 00:11:54 +00:00
|
|
|
|
|
|
|
var catchUpInOrder = true;
|
|
|
|
for(var i = 1; i < catchUpEvents.length; i++)
|
|
|
|
catchUpInOrder = catchUpInOrder && (catchUpEvents[i].originalPosition.compareTo(catchUpEvents[i-1].originalPosition) > 0);
|
|
|
|
test.ok(catchUpInOrder, "Catch-up events are out of order.");
|
|
|
|
|
|
|
|
var liveInOrder = true;
|
|
|
|
for(var j = 1; j < liveEvents.length; j++)
|
|
|
|
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
|
|
|
test.ok(liveInOrder, "Live events are out of order.");
|
|
|
|
|
2017-01-29 02:04:58 +00:00
|
|
|
test.done();
|
|
|
|
}
|
2016-10-15 05:53:23 +00:00
|
|
|
function eventAppeared(s, e) {
|
2017-07-17 00:11:54 +00:00
|
|
|
var isLive = liveProcessing;
|
|
|
|
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
|
|
|
if (isLive) {
|
|
|
|
liveEvents.push(e);
|
|
|
|
} else {
|
|
|
|
catchUpEvents.push(e);
|
|
|
|
}
|
|
|
|
if (isLive && liveEvents.length === 2) s.stop();
|
|
|
|
});
|
2016-10-15 05:53:23 +00:00
|
|
|
}
|
|
|
|
function liveProcessingStarted() {
|
|
|
|
liveProcessing = true;
|
2017-07-17 00:11:54 +00:00
|
|
|
var events = [createRandomEvent(), createRandomEvent()];
|
2017-01-29 02:04:58 +00:00
|
|
|
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
2016-10-15 05:53:23 +00:00
|
|
|
}
|
|
|
|
function subscriptionDropped(connection, reason, error) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
2016-10-15 05:53:23 +00:00
|
|
|
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
2017-01-29 02:04:58 +00:00
|
|
|
done(error);
|
2016-10-15 05:53:23 +00:00
|
|
|
}
|
|
|
|
var subscription = this.conn.subscribeToAllFrom(null, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
|
2017-02-25 22:12:13 +00:00
|
|
|
},
|
|
|
|
'Test Subscribe to All From (Position)': function(test) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.expect(7);
|
2017-02-25 22:12:13 +00:00
|
|
|
var self = this;
|
|
|
|
var liveProcessing = false;
|
|
|
|
var catchUpEvents = [];
|
|
|
|
var liveEvents = [];
|
|
|
|
var _doneCount = 0;
|
|
|
|
function done(err) {
|
|
|
|
test.ok(!err, err ? err.stack : '');
|
|
|
|
_doneCount++;
|
|
|
|
if (_doneCount < 2) return;
|
2017-07-17 00:11:54 +00:00
|
|
|
|
|
|
|
var catchUpInOrder = true;
|
|
|
|
for(var i = 1; i < catchUpEvents.length; i++)
|
|
|
|
catchUpInOrder = catchUpInOrder && (catchUpEvents[i].originalPosition.compareTo(catchUpEvents[i-1].originalPosition) > 0);
|
|
|
|
test.ok(catchUpInOrder, "Catch-up events are out of order.");
|
|
|
|
|
|
|
|
var liveInOrder = true;
|
|
|
|
for(var j = 1; j < liveEvents.length; j++)
|
|
|
|
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
|
|
|
test.ok(liveInOrder, "Live events are out of order.");
|
|
|
|
|
2017-02-25 22:12:13 +00:00
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
function eventAppeared(s, e) {
|
2017-07-17 00:11:54 +00:00
|
|
|
var isLive = liveProcessing;
|
|
|
|
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
|
|
|
if (isLive) {
|
|
|
|
liveEvents.push(e);
|
|
|
|
} else {
|
|
|
|
catchUpEvents.push(e);
|
|
|
|
}
|
|
|
|
if (isLive && liveEvents.length === 2) s.stop();
|
|
|
|
});
|
2017-02-25 22:12:13 +00:00
|
|
|
}
|
|
|
|
function liveProcessingStarted() {
|
|
|
|
liveProcessing = true;
|
2017-07-17 00:11:54 +00:00
|
|
|
var events = [createRandomEvent(), createRandomEvent()];
|
2017-02-25 22:12:13 +00:00
|
|
|
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
}
|
|
|
|
function subscriptionDropped(connection, reason, error) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
2017-02-25 22:12:13 +00:00
|
|
|
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
|
|
|
done(error);
|
|
|
|
}
|
|
|
|
this.conn.readAllEventsForward(client.positions.start, 512, true, allCredentials)
|
|
|
|
.then(function (slice) {
|
|
|
|
var subscription = self.conn.subscribeToAllFrom(slice.nextPosition, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
|
|
|
|
test.ok(subscription, "Subscription is null/undefined.");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Test Subscribe to All From (End)': function(test) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.expect(6);
|
2017-02-25 22:12:13 +00:00
|
|
|
var self = this;
|
|
|
|
var liveProcessing = false;
|
|
|
|
var catchUpEvents = [];
|
|
|
|
var liveEvents = [];
|
|
|
|
var _doneCount = 0;
|
|
|
|
function done(err) {
|
|
|
|
test.ok(!err, err ? err.stack : '');
|
|
|
|
_doneCount++;
|
|
|
|
if (_doneCount < 2) return;
|
2017-07-17 00:11:54 +00:00
|
|
|
|
|
|
|
var liveInOrder = true;
|
|
|
|
for(var j = 1; j < liveEvents.length; j++)
|
|
|
|
liveInOrder = liveInOrder && (liveEvents[j].originalPosition.compareTo(liveEvents[j-1].originalPosition) > 0);
|
|
|
|
test.ok(liveInOrder, "Live events are out of order.");
|
|
|
|
|
2017-02-25 22:12:13 +00:00
|
|
|
test.done();
|
|
|
|
}
|
|
|
|
function eventAppeared(s, e) {
|
2017-07-17 00:11:54 +00:00
|
|
|
var isLive = liveProcessing;
|
|
|
|
delayOnlyFirst(isLive ? liveEvents.length : catchUpEvents.length, function() {
|
|
|
|
if (isLive) {
|
|
|
|
liveEvents.push(e);
|
|
|
|
} else {
|
|
|
|
catchUpEvents.push(e);
|
|
|
|
}
|
|
|
|
if (isLive && liveEvents.length === 2) s.stop();
|
|
|
|
});
|
2017-02-25 22:12:13 +00:00
|
|
|
}
|
|
|
|
function liveProcessingStarted() {
|
|
|
|
liveProcessing = true;
|
2017-07-17 00:11:54 +00:00
|
|
|
var events = [createRandomEvent(), createRandomEvent()];
|
2017-02-25 22:12:13 +00:00
|
|
|
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
|
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
}
|
|
|
|
function subscriptionDropped(connection, reason, error) {
|
2017-07-17 00:11:54 +00:00
|
|
|
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
|
2017-02-25 22:12:13 +00:00
|
|
|
test.ok(catchUpEvents.length === 0, "Expecting 0 catchUp event, got " + catchUpEvents.length);
|
|
|
|
done(error);
|
|
|
|
}
|
|
|
|
this.conn.readAllEventsForward(client.positions.end, 512, true, allCredentials)
|
|
|
|
.then(function (slice) {
|
|
|
|
var subscription = self.conn.subscribeToAllFrom(slice.nextPosition, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
|
|
|
|
test.ok(subscription, "Subscription is null/undefined.");
|
|
|
|
});
|
2016-10-15 05:53:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-01 18:30:06 +00:00
|
|
|
require('./common/base_test').init(module.exports);
|