Adding tests

This commit is contained in:
Nicolas Dextraze 2016-10-14 22:53:23 -07:00
parent b2504749ce
commit 4ea996781f
18 changed files with 295 additions and 6016 deletions

4
.gitignore vendored
View File

@ -33,4 +33,6 @@ node_modules
.node_repl_history
# InteliJ
.idea
.idea
lib/

View File

@ -3,9 +3,4 @@
* see README.md for more details
* see LICENSE for license info
*/
/**
* TODO:
* library is heavy on number of files so it could have negative impact on load time
* we need a compiled (single file) version of the library
*/
module.exports = require('./lib/dist.js');

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,14 @@
"version": "0.0.2",
"description": "A port of the EventStore .Net ClientAPI to Node.js",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"clean": "rm lib/dist.js",
"build": "webpack",
"pretest": "npm run build",
"test": "nodeunit"
},
"repository": {
@ -22,12 +29,16 @@
},
"homepage": "https://github.com/nicdex/eventstore-node#readme",
"dependencies": {
"long": "^3.0.3",
"protobufjs": "^5.0.1",
"uuid": "^2.0.1"
"long": "^3.2",
"protobufjs": "^5.0",
"uuid": "^2.0"
},
"engines": {
"node": ">=0.12"
},
"engineStrict": true
"engineStrict": true,
"devDependencies": {
"nodeunit": "^0.10.2",
"webpack": "^1.13.2"
}
}

View File

@ -189,7 +189,6 @@ SubscriptionOperation.prototype.dropSubscription = function(reason, err, connect
if (reason !== SubscriptionDropReason.UserInitiated && this._subscription === null)
{
if (err === null) throw new Error(util.format("No exception provided for subscription drop reason '%s", reason));
//TODO: this should be last thing to execute
this._cb(err);
return;
}

View File

@ -9,7 +9,7 @@ var BufferSegment = require('../common/bufferSegment');
var InspectionDecision = require('../systemData/inspectionDecision');
var InspectionResult = require('./../systemData/inspectionResult');
var results = require('../results');
var VolatileEventStoreSubscription = require('../volatileEventStoreConnection');
var VolatileEventStoreSubscription = require('../volatileEventStoreSubscription');
function VolatileSubscriptionOperation(
log, cb, streamId, resolveLinkTos, userCredentials, eventAppeared,

View File

@ -43,13 +43,21 @@ function merge(a,b) {
/**
* Create an EventStore connection
* @param {object} settings
* @param {object} tcpEndPoint
* @param {string|object} endPoint
* @param {string} [connectionName]
* @returns {EventStoreNodeConnection}
*/
module.exports.create = function(settings, tcpEndPoint, connectionName) {
//TODO: cluster connection
var mergedSettings = merge(defaultConnectionSettings, settings || {});
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndPoint, settings.useSslConnection);
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName || null);
module.exports.create = function(settings, endPoint, connectionName) {
if (typeof endPoint === 'object') {
var mergedSettings = merge(defaultConnectionSettings, settings || {});
var endpointDiscoverer = new StaticEndpointDiscoverer(endPoint, settings.useSslConnection);
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName || null);
}
if (typeof endPoint === 'string') {
//TODO: tcpEndpoint represented as tcp://hostname:port
//TODO: cluster discovery via dns represented as discover://dns:?port
throw new Error('Not implemented.');
}
//TODO: cluster discovery via gossip seeds in settings
throw new Error('Not implemented.');
};

View File

@ -381,8 +381,10 @@ EventStoreNodeConnection.prototype.readAllEventsBackward = function(
EventStoreNodeConnection.prototype.subscribeToStream = function(
stream, resolveLinkTos, eventAppeared, subscriptionDropped, userCredentials
) {
if (typeof stream !== 'string' || stream === '') throw new TypeError("stream must be a non-empty string.");
if (typeof eventAppeared !== 'function') throw new TypeError("eventAppeared must be a function.");
ensure.notNullOrEmpty(stream, "stream");
ensure.isTypeOf(Function, eventAppeared, "eventAppeared");
if (subscriptionDropped)
ensure.isTypeOf(Function, subscriptionDropped, "subscriptionDropped");
var self = this;
return new Promise(function(resolve,reject) {

View File

@ -10,15 +10,15 @@ var EventStoreSubsription = require('./eventStoreSubscription');
* @constructor
* @augments {EventStoreSubscription}
*/
function VolatileEventStoreConnection(subscriptionOperation, streamId, lastCommitPosition, lastEventNumber) {
function VolatileEventStoreSubscription(subscriptionOperation, streamId, lastCommitPosition, lastEventNumber) {
EventStoreSubsription.call(this, streamId, lastCommitPosition, lastEventNumber);
this._subscriptionOperation = subscriptionOperation;
}
util.inherits(VolatileEventStoreConnection, EventStoreSubsription);
util.inherits(VolatileEventStoreSubscription, EventStoreSubsription);
VolatileEventStoreConnection.prototype.unsubscribe = function() {
VolatileEventStoreSubscription.prototype.unsubscribe = function() {
this._subscriptionOperation.unsubscribe();
};
module.exports = VolatileEventStoreConnection;
module.exports = VolatileEventStoreSubscription;

View File

@ -163,7 +163,7 @@ module.exports = {
.catch(function(err) {
test.done(err);
});
},*/
},
'Test Subscribe to Stream': function(test) {
var done = false;
function eventAppeared() {
@ -188,7 +188,7 @@ module.exports = {
done = true;
test.done(err);
})
},
}
'Test Subscribe to All': function(test) {
var done = false;
function eventAppeared() {
@ -233,11 +233,12 @@ module.exports = {
self.conn.appendToStream(testStreamName, client.expectedVersion.any, events);
}
function subscriptionDropped(connection, reason, error) {
console.log(reason);
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length >= 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
test.done(error);
}
//this.conn.appendToStream()
this.conn.appendToStream(testStreamName, client.expectedVersion.noStream, )
var subscription = this.conn.subscribeToStreamFrom(testStreamName, null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
},
'Test Subscribe to All From': function(test) {
@ -264,55 +265,8 @@ module.exports = {
test.done(error);
}
var subscription = this.conn.subscribeToAllFrom(null, false, eventAppeared, liveProcessingStarted, subscriptionDropped, userCredentialsForAll);
},
'Test Set Stream Metadata Raw': function(test) {
this.conn.setStreamMetadataRaw(testStreamName, client.expectedVersion.emptyStream, {$maxCount: 100})
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
},
'Test Get Stream Metadata Raw': function(test) {
this.conn.getStreamMetadataRaw(testStreamName)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
},
'Test Create Persistent Subscription': function(test) {
var settings = client.PersistentSubscriptionSettings.create();
this.conn.createPersistentSubscription(testStreamName, 'consumer-1', settings, userCredentialsForAll)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
},
//TODO: Update Persistent Subscription
'Test ConnectTo Persistent Subscription': function(test) {
function eventAppeared(s, e) {
s.stop();
}
function subscriptionDropped(connection, reason, error) {
test.done(error);
}
var subscription = this.conn.connectToPersistentSubscription(testStreamName, 'consumer-1', eventAppeared, subscriptionDropped);
this.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent()]);
},
'Test Delete Persistent Subscription': function(test) {
this.conn.deletePersistentSubscription(testStreamName, 'consumer-1', userCredentialsForAll)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
}/*,
},*/
/*,
'Test Delete Stream': function(test) {
this.conn.deleteStream(testStreamName, client.expectedVersion.any)
.then(function(result) {

View File

@ -17,6 +17,7 @@ var tcpEndPoint = {host: 'localhost', port: 1113};
function setUp(cb) {
this.log = settings.log;
this.testStreamName = 'test-' + uuid.v4();
this.log.info('A', this.testStreamName);
var connected = false;
this.conn = client.EventStoreConnection.create(settings, tcpEndPoint);
this.conn.connect()

View File

@ -22,7 +22,7 @@ module.exports = {
test.done();
}
},
'Connect To Endpoint That Don\'t Exist': function(test) {
'Connect To Endpoint That Doesn\'t Exist': function(test) {
var tcpEndpoint = {hostname: 'localhost', port: 1114};
var conn = client.EventStoreConnection.create(testBase.settings({maxReconnections:1}), tcpEndpoint);
conn.connect()

26
test/metadata_test.js Normal file
View File

@ -0,0 +1,26 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
module.exports = {
'Test Set Stream Metadata Raw': function(test) {
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.emptyStream, {$maxCount: 100})
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
},
'Test Get Stream Metadata Raw': function(test) {
this.conn.getStreamMetadataRaw(this.testStreamName)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
}
};
require('./common/base_test').init(module.exports);

View File

@ -0,0 +1,46 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
const adminCredentials = new client.UserCredentials("admin", "changeit");
function createRandomEvent() {
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
}
var testStreamName = 'test' + uuid.v4();
module.exports = {
'Test Create Persistent Subscription': function(test) {
var settings = client.PersistentSubscriptionSettings.create();
this.conn.createPersistentSubscription(testStreamName, 'consumer-1', settings, adminCredentials)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
},
//TODO: Update Persistent Subscription
'Test ConnectTo Persistent Subscription': function(test) {
function eventAppeared(s, e) {
s.stop();
}
function subscriptionDropped(connection, reason, error) {
test.done(error);
}
var subscription = this.conn.connectToPersistentSubscription(testStreamName, 'consumer-1', eventAppeared, subscriptionDropped);
this.log.info('ABC', subscription);
this.conn.appendToStream(testStreamName, client.expectedVersion.any, [createRandomEvent()]);
},
'Test Delete Persistent Subscription': function(test) {
this.conn.deletePersistentSubscription(testStreamName, 'consumer-1', adminCredentials)
.then(function(result) {
test.done();
})
.catch(function(err) {
test.done(err);
});
}
};
require('./common/base_test').init(module.exports);

View File

@ -0,0 +1,38 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
const allCredentials = new client.UserCredentials("admin", "changeit");
function createRandomEvent() {
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
}
module.exports = {
'Test Subscribe to All From': function(test) {
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
var liveEvents = [];
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
test.done(error);
}
var subscription = this.conn.subscribeToAllFrom(null, false, eventAppeared, liveProcessingStarted, subscriptionDropped, allCredentials);
}
};
require('./common/base_test').init(module.exports);

View File

@ -0,0 +1,44 @@
const uuid = require('uuid');
const client = require('../src/client');
const allCredentials = new client.UserCredentials("admin", "changeit");
module.exports = {
'Test Subscribe To All Happy Path': function(test) {
const resolveLinkTos = false;
const numberOfPublishedEvents = 5;
var publishedEvents = [];
for(var i=0;i<numberOfPublishedEvents;i++)
publishedEvents.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'));
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) {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
}
function subscriptionDropped(subscription, reason, error) {
if (error) return test.done(error);
testAllPublishedEventsAppeared();
testEventsAppearedInCorrectOrder();
test.done();
}
var self = this;
this.conn.subscribeToAll(resolveLinkTos, eventAppeared, subscriptionDropped, allCredentials)
.then(function(subscription) {
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, true);
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
})
.catch(test.done)
}
};
require('./common/base_test').init(module.exports);

View File

@ -0,0 +1,47 @@
var util = require('util');
var uuid = require('uuid');
var client = require('../src/client');
function createRandomEvent() {
return client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, {createdAt: Date.now()}, 'testEvent');
}
module.exports = {
'Test Subscribe to Stream From Happy Path': function(test) {
var self = this;
var liveProcessing = false;
var catchUpEvents = [];
var liveEvents = [];
function eventAppeared(s, e) {
if (liveProcessing) {
liveEvents.push(e);
s.stop();
} else {
catchUpEvents.push(e);
}
}
function liveProcessingStarted() {
liveProcessing = true;
var events = [createRandomEvent()];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, events);
}
function subscriptionDropped(connection, reason, error) {
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
test.ok(catchUpEvents.length >= 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
test.done(error);
}
var events = [createRandomEvent()];
this.conn.appendToStream(self.testStreamName, client.expectedVersion.noStream, events)
.then(function() {
var subscription = self.conn.subscribeToStreamFrom(self.testStreamName, null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
test.areEqual("subscription.streamId", subscription.streamId, self.testStreamName);
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, false);
test.areEqual("subscription.readBatchSize", subscription.readBatchSize, 500);
test.areEqual("subscription.maxPushQueueSize", subscription.maxPushQueueSize, 10000);
});
}
};
require('./common/base_test').init(module.exports);

View File

@ -0,0 +1,45 @@
const uuid = require('uuid');
const client = require('../src/client');
module.exports = {
'Test Subscribe To Stream Happy Path': function(test) {
const resolveLinkTos = false;
const numberOfPublishedEvents = 5;
var publishedEvents = [];
for(var i=0;i<numberOfPublishedEvents;i++)
publishedEvents.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'));
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) {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
}
function subscriptionDropped(subscription, reason, error) {
if (error) return test.done(error);
testAllPublishedEventsAppeared();
testEventsAppearedInCorrectOrder();
test.done();
}
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);
})
.catch(test.done)
}
};
require('./common/base_test').init(module.exports);