handle eventAppeared callback returning Promise

This commit is contained in:
2017-07-16 17:11:54 -07:00
parent 0b968c40e8
commit 7c94e26055
12 changed files with 269 additions and 102 deletions

View File

@ -9,6 +9,20 @@ function createRandomEvent() {
var testStreamName = 'test-' + uuid.v4();
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();
})
}
module.exports = {
'Test Create Persistent Subscription': function(test) {
var settings = client.PersistentSubscriptionSettings.create();
@ -22,7 +36,8 @@ module.exports = {
},
//TODO: Update Persistent Subscription
'Test ConnectTo Persistent Subscription': function(test) {
test.expect(3);
test.expect(4);
var receivedEvents = [];
var _doneCount = 0;
function done(err) {
test.ok(!err, err ? err.stack : '');
@ -31,16 +46,21 @@ module.exports = {
test.done();
}
function eventAppeared(s, e) {
s.stop();
return delayOnlyFirst(receivedEvents.length, function () {
receivedEvents.push(e);
if (receivedEvents.length === 2) s.stop();
});
}
function subscriptionDropped(connection, reason, error) {
done(error);
if (error) return done(error);
test.ok(receivedEvents[1].originalEventNumber > receivedEvents[0].originalEventNumber, "Received events are out of order.");
done();
}
var self = this;
this.conn.connectToPersistentSubscription(testStreamName, 'consumer-1', eventAppeared, subscriptionDropped)
.then(function(subscription) {
test.ok(subscription, "Subscription is null.");
return self.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent()]);
return self.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent(), createRandomEvent()]);
})
.then(function () {
done();

View File

@ -7,9 +7,23 @@ function createRandomEvent() {
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
}
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();
})
}
module.exports = {
'Test Subscribe to All From (Start)': function(test) {
test.expect(4);
test.expect(6);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -19,19 +33,33 @@ module.exports = {
test.ok(!err, err ? err.stack : '');
_doneCount++;
if (_doneCount < 2) return;
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.");
test.done();
}
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
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();
});
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
.then(function () {
done();
@ -39,14 +67,14 @@ module.exports = {
.catch(done);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
done(error);
}
var subscription = this.conn.subscribeToAllFrom(null, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
},
'Test Subscribe to All From (Position)': function(test) {
test.expect(5);
test.expect(7);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -56,19 +84,33 @@ module.exports = {
test.ok(!err, err ? err.stack : '');
_doneCount++;
if (_doneCount < 2) return;
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.");
test.done();
}
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
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();
});
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
.then(function () {
done();
@ -76,7 +118,7 @@ module.exports = {
.catch(done);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
done(error);
}
@ -87,7 +129,7 @@ module.exports = {
});
},
'Test Subscribe to All From (End)': function(test) {
test.expect(5);
test.expect(6);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -97,19 +139,28 @@ module.exports = {
test.ok(!err, err ? err.stack : '');
_doneCount++;
if (_doneCount < 2) return;
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.");
test.done();
}
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
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();
});
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
.then(function () {
done();
@ -117,7 +168,7 @@ module.exports = {
.catch(done);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length === 0, "Expecting 0 catchUp event, got " + catchUpEvents.length);
done(error);
}

View File

@ -2,6 +2,20 @@ const uuid = require('uuid');
const client = require('../src/client');
const allCredentials = new client.UserCredentials("admin", "changeit");
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();
})
}
module.exports = {
'Test Subscribe To All Happy Path': function(test) {
const resolveLinkTos = false;
@ -30,8 +44,10 @@ module.exports = {
var receivedEvents = [];
function eventAppeared(subscription, event) {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
delayOnlyFirst(receivedEvents.length, function() {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
});
}
function subscriptionDropped(subscription, reason, error) {
if (error) return done(error);

View File

@ -6,9 +6,23 @@ function createRandomEvent() {
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
}
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();
})
}
module.exports = {
'Test Subscribe to Stream From Beginning (null)': function(test) {
test.expect(22);
test.expect(36);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -22,16 +36,19 @@ module.exports = {
}
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
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();
});
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
.then(function () {
done();
@ -39,16 +56,20 @@ module.exports = {
.catch(done);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.testLiveEvent('liveEvents[0]', liveEvents[0]);
test.ok(liveEvents[0].originalEventNumber, 1);
test.ok(catchUpEvents.length === 1, "Expecting 1 catchUp event, got " + catchUpEvents.length);
test.testLiveEvent('liveEvents[1]', liveEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 2);
test.ok(liveEvents[1].originalEventNumber, 3);
test.ok(catchUpEvents.length === 2, "Expecting 2 catchUp event, got " + catchUpEvents.length);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0]);
test.testReadEvent('catchUpEvents[1]', catchUpEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 0);
test.ok(liveEvents[1].originalEventNumber, 1);
done(error);
}
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
this.conn.appendToStream(self.testStreamName, client.expectedVersion.noStream, events)
.then(function() {
var subscription = self.conn.subscribeToStreamFrom(self.testStreamName, null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
@ -61,7 +82,7 @@ module.exports = {
.catch(test.done);
},
'Test Subscribe to Stream From 0': function(test) {
test.expect(22);
test.expect(29);
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
@ -75,16 +96,19 @@ module.exports = {
}
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
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();
});
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
var events = [createRandomEvent(), createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events)
.then(function () {
done();
@ -92,9 +116,11 @@ module.exports = {
.catch(done);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(liveEvents.length === 2, "Expecting 2 live event, got " + liveEvents.length);
test.testLiveEvent('liveEvents[0]', liveEvents[0]);
test.testLiveEvent('liveEvents[1]', liveEvents[1]);
test.ok(liveEvents[0].originalEventNumber, 2);
test.ok(liveEvents[1].originalEventNumber, 3);
test.ok(catchUpEvents.length === 1, "Expecting 1 catchUp event, got " + catchUpEvents.length);
test.testReadEvent('catchUpEvents[0]', catchUpEvents[0]);
test.ok(liveEvents[0].originalEventNumber, 1);

View File

@ -1,6 +1,20 @@
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();
})
}
module.exports = {
'Test Subscribe To Stream Happy Path': function(test) {
const resolveLinkTos = false;
@ -28,8 +42,10 @@ module.exports = {
var receivedEvents = [];
function eventAppeared(subscription, event) {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
delayOnlyFirst(receivedEvents.length, function () {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
});
}
function subscriptionDropped(subscription, reason, error) {
if (error) return done(error);