feat(cluster): create way to test single/gossip/cluster
* docker-compose files to easily setup cluster or single node eventstore * programmatic tests for single/gossip/cluster
This commit is contained in:
@ -11,38 +11,102 @@ protobufJS.util.Long = undefined;
|
||||
protobufJS.configure();
|
||||
|
||||
var settings = {
|
||||
log: new NoopLogger()
|
||||
log: new NoopLogger(),
|
||||
};
|
||||
if (process.env.TESTS_VERBOSE_LOGGING === '1') {
|
||||
settings.verboseLogging = true;
|
||||
settings.log = new FileLogger('test-verbose.log');
|
||||
}
|
||||
|
||||
var tcpEndPoint = {host: 'localhost', port: 1113};
|
||||
|
||||
function setUp(cb) {
|
||||
function setUpWithGossipSeeds(cb) {
|
||||
var gossipSeeds = [
|
||||
new client.GossipSeed({ host: process.env.EVENTSTORE_HOST_1 || '192.168.33.10', port: 2113 }),
|
||||
new client.GossipSeed({ host: process.env.EVENTSTORE_HOST_2 || '192.168.33.11', port: 2113 }),
|
||||
new client.GossipSeed({ host: process.env.EVENTSTORE_HOST_3 || '192.168.33.12', port: 2113 }),
|
||||
];
|
||||
this.log = settings.log;
|
||||
this.testStreamName = 'test-' + uuid.v4();
|
||||
var connected = false;
|
||||
this.conn = client.EventStoreConnection.create(settings, tcpEndPoint);
|
||||
this.conn.connect()
|
||||
.then(function () {
|
||||
//Doesn't mean anything, connection is just initiated
|
||||
settings.log.debug("Connection to %j initialized.", tcpEndPoint);
|
||||
})
|
||||
.catch(function (err) {
|
||||
settings.log.error(err, "Initializing connection to %j failed.", tcpEndPoint);
|
||||
cb(err);
|
||||
});
|
||||
this.conn = client.createConnection(settings, gossipSeeds);
|
||||
this.conn
|
||||
.connect()
|
||||
.then(function () {
|
||||
//Doesn't mean anything, connection is just initiated
|
||||
settings.log.debug('Connection to %j initialized.', gossipSeeds);
|
||||
})
|
||||
.catch(function (err) {
|
||||
settings.log.error(err, 'Initializing connection to %j failed.', gossipSeeds);
|
||||
cb(err);
|
||||
});
|
||||
this.conn.on('closed', function (reason) {
|
||||
if (connected) return;
|
||||
var error = new Error("Connection failed: " + reason);
|
||||
settings.log.error(error, "Connection to %j failed.", tcpEndPoint);
|
||||
var error = new Error('Connection failed: ' + reason);
|
||||
settings.log.error(error, 'Connection to %j failed.', gossipSeeds);
|
||||
cb(error);
|
||||
});
|
||||
this.conn.on('connected', function (tcpEndPoint) {
|
||||
if (connected) return;
|
||||
settings.log.debug("Connected to %j.", tcpEndPoint);
|
||||
settings.log.debug('Connected to %j.', tcpEndPoint);
|
||||
connected = true;
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
function setUpWithDns(cb) {
|
||||
var clusterDns = 'discover://eventstore.local:2113';
|
||||
this.log = settings.log;
|
||||
this.testStreamName = 'test-' + uuid.v4();
|
||||
var connected = false;
|
||||
this.conn = client.createConnection(settings, clusterDns);
|
||||
this.conn
|
||||
.connect()
|
||||
.then(function () {
|
||||
//Doesn't mean anything, connection is just initiated
|
||||
settings.log.debug('Connection to %j initialized.', clusterDns);
|
||||
})
|
||||
.catch(function (err) {
|
||||
settings.log.error(err, 'Initializing connection to %j failed.', clusterDns);
|
||||
cb(err);
|
||||
});
|
||||
this.conn.on('closed', function (reason) {
|
||||
if (connected) return;
|
||||
var error = new Error('Connection failed: ' + reason);
|
||||
settings.log.error(error, 'Connection to %j failed.', clusterDns);
|
||||
cb(error);
|
||||
});
|
||||
this.conn.on('connected', function (tcpEndPoint) {
|
||||
if (connected) return;
|
||||
settings.log.debug('Connected to %j.', tcpEndPoint);
|
||||
connected = true;
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
function setUpWithTcpEndpoint(cb) {
|
||||
var tcpEndPoint = { host: process.env.EVENTSTORE_HOST || 'localhost', port: 1113 };
|
||||
this.log = settings.log;
|
||||
this.testStreamName = 'test-' + uuid.v4();
|
||||
var connected = false;
|
||||
this.conn = client.EventStoreConnection.create(settings, tcpEndPoint);
|
||||
this.conn
|
||||
.connect()
|
||||
.then(function () {
|
||||
//Doesn't mean anything, connection is just initiated
|
||||
settings.log.debug('Connection to %j initialized.', tcpEndPoint);
|
||||
})
|
||||
.catch(function (err) {
|
||||
settings.log.error(err, 'Initializing connection to %j failed.', tcpEndPoint);
|
||||
cb(err);
|
||||
});
|
||||
this.conn.on('closed', function (reason) {
|
||||
if (connected) return;
|
||||
var error = new Error('Connection failed: ' + reason);
|
||||
settings.log.error(error, 'Connection to %j failed.', tcpEndPoint);
|
||||
cb(error);
|
||||
});
|
||||
this.conn.on('connected', function (tcpEndPoint) {
|
||||
if (connected) return;
|
||||
settings.log.debug('Connected to %j.', tcpEndPoint);
|
||||
connected = true;
|
||||
cb();
|
||||
});
|
||||
@ -50,7 +114,7 @@ function setUp(cb) {
|
||||
|
||||
function tearDown(cb) {
|
||||
this.conn.close();
|
||||
this.conn.on('closed', function() {
|
||||
this.conn.on('closed', function () {
|
||||
cb();
|
||||
});
|
||||
this.conn = null;
|
||||
@ -58,9 +122,8 @@ function tearDown(cb) {
|
||||
|
||||
function areEqual(name, actual, expected) {
|
||||
if (typeof expected !== 'object' || expected === null)
|
||||
this.strictEqual(actual, expected, util.format("Failed %s === %s, got %s.", name, expected, actual));
|
||||
else
|
||||
this.deepEqual(actual, expected, util.format("Failed %s deepEqual %j, got %j.", name, expected, actual));
|
||||
this.strictEqual(actual, expected, util.format('Failed %s === %s, got %s.', name, expected, actual));
|
||||
else this.deepEqual(actual, expected, util.format('Failed %s deepEqual %j, got %j.', name, expected, actual));
|
||||
}
|
||||
|
||||
function fail(reason) {
|
||||
@ -69,55 +132,75 @@ function fail(reason) {
|
||||
|
||||
function eventEqualEventData(name, resolvedEvent, eventData) {
|
||||
var ev = resolvedEvent.originalEvent;
|
||||
this.ok(ev !== null, util.format("Failed %s !== null.", name + ".originalEvent"));
|
||||
this.ok(ev !== null, util.format('Failed %s !== null.', name + '.originalEvent'));
|
||||
if (ev === null) return;
|
||||
this.areEqual(name + ".originalEvent.eventId", ev.eventId, eventData.eventId);
|
||||
this.areEqual(name + ".originalEvent.eventType", ev.eventType, eventData.type);
|
||||
this.ok(Buffer.compare(ev.data, eventData.data) === 0, name + ".originalEvent.data is not equal to original data.");
|
||||
this.ok(Buffer.compare(ev.metadata, eventData.metadata) === 0, name + ".originalEvent.metadata is not equal to original metadata.");
|
||||
this.areEqual(name + '.originalEvent.eventId', ev.eventId, eventData.eventId);
|
||||
this.areEqual(name + '.originalEvent.eventType', ev.eventType, eventData.type);
|
||||
this.ok(Buffer.compare(ev.data, eventData.data) === 0, name + '.originalEvent.data is not equal to original data.');
|
||||
this.ok(
|
||||
Buffer.compare(ev.metadata, eventData.metadata) === 0,
|
||||
name + '.originalEvent.metadata is not equal to original metadata.'
|
||||
);
|
||||
}
|
||||
|
||||
function testRecordedEvent(name, event) {
|
||||
this.ok(Long.isLong(event.eventNumber), name + ".eventNumber is not a Long");
|
||||
this.ok(event.created instanceof Date, name + ".created is not a Date");
|
||||
this.ok(typeof event.createdEpoch === 'number', name + ".createdEpoch is not a number");
|
||||
this.ok(Long.isLong(event.eventNumber), name + '.eventNumber is not a Long');
|
||||
this.ok(event.created instanceof Date, name + '.created is not a Date');
|
||||
this.ok(typeof event.createdEpoch === 'number', name + '.createdEpoch is not a number');
|
||||
}
|
||||
|
||||
function testLiveEvent(name, event, evNumber) {
|
||||
this.ok(event.event, name + ".event not defined (or null)");
|
||||
this.ok(event.originalEvent, name + ".originalEvent not defined (or null)");
|
||||
this.ok(event.isResolved === false, name + ".isResolved should be true");
|
||||
this.ok(event.originalPosition instanceof client.Position, name + ".originalPosition is not an instance of Position");
|
||||
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
|
||||
this.ok(Long.isLong(event.originalEventNumber), name + ".originalEventNumber is not a Long");
|
||||
this.ok(event.event, name + '.event not defined (or null)');
|
||||
this.ok(event.originalEvent, name + '.originalEvent not defined (or null)');
|
||||
this.ok(event.isResolved === false, name + '.isResolved should be true');
|
||||
this.ok(event.originalPosition instanceof client.Position, name + '.originalPosition is not an instance of Position');
|
||||
this.ok(event.originalStreamId, name + '.originalStreamId not defined (or null)');
|
||||
this.ok(Long.isLong(event.originalEventNumber), name + '.originalEventNumber is not a Long');
|
||||
if (typeof evNumber === 'number') {
|
||||
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
||||
this.ok(
|
||||
event.originalEventNumber.toNumber() === evNumber,
|
||||
name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber
|
||||
);
|
||||
}
|
||||
testRecordedEvent.call(this, name + '.event', event.event);
|
||||
}
|
||||
|
||||
function testReadEvent(name, event, evNumber) {
|
||||
this.ok(event.event, name + ".event not defined (or null)");
|
||||
this.ok(event.originalEvent, name + ".originalEvent not defined (or null)");
|
||||
this.ok(event.isResolved === false, name + ".isResolved should be true");
|
||||
this.ok(event.originalPosition === null, name + ".originalPosition is not null");
|
||||
this.ok(event.originalStreamId, name + ".originalStreamId not defined (or null)");
|
||||
this.ok(Long.isLong(event.originalEventNumber), name + ".originalEventNumber is not a Long");
|
||||
this.ok(event.event, name + '.event not defined (or null)');
|
||||
this.ok(event.originalEvent, name + '.originalEvent not defined (or null)');
|
||||
this.ok(event.isResolved === false, name + '.isResolved should be true');
|
||||
this.ok(event.originalPosition === null, name + '.originalPosition is not null');
|
||||
this.ok(event.originalStreamId, name + '.originalStreamId not defined (or null)');
|
||||
this.ok(Long.isLong(event.originalEventNumber), name + '.originalEventNumber is not a Long');
|
||||
if (typeof evNumber === 'number') {
|
||||
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
||||
this.ok(
|
||||
event.originalEventNumber.toNumber() === evNumber,
|
||||
name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber
|
||||
);
|
||||
}
|
||||
testRecordedEvent.call(this, name + '.event', event.event);
|
||||
}
|
||||
|
||||
var _ = {
|
||||
'setUp': setUp,
|
||||
'tearDown': tearDown
|
||||
tearDown: tearDown,
|
||||
};
|
||||
|
||||
switch (process.env.EVENTSTORE_CONNECTION_TYPE) {
|
||||
case 'gossip':
|
||||
_.setUp = setUpWithGossipSeeds;
|
||||
break;
|
||||
case 'dns':
|
||||
_.setUp = setUpWithDns;
|
||||
break;
|
||||
case 'tcp':
|
||||
default:
|
||||
_.setUp = setUpWithTcpEndpoint;
|
||||
}
|
||||
|
||||
function wrap(name, testFunc) {
|
||||
var base = _[name];
|
||||
if (base === undefined) {
|
||||
return function(test) {
|
||||
return function (test) {
|
||||
settings.log.debug('--- %s ---', name);
|
||||
test.areEqual = areEqual.bind(test);
|
||||
test.fail = fail.bind(test);
|
||||
@ -125,36 +208,48 @@ function wrap(name, testFunc) {
|
||||
test.testLiveEvent = testLiveEvent.bind(test);
|
||||
test.testReadEvent = testReadEvent.bind(test);
|
||||
return testFunc.call(this, test);
|
||||
}
|
||||
};
|
||||
}
|
||||
return function(cb) {
|
||||
return function (cb) {
|
||||
var self = this;
|
||||
base.call(this, function(err) {
|
||||
base.call(this, function (err) {
|
||||
if (err) return cb(err);
|
||||
return testFunc.call(self, cb);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.init = function(testSuite, addSetUpTearDownIfNotPresent) {
|
||||
module.exports.init = function (testSuite, addSetUpTearDownIfNotPresent) {
|
||||
var thisObj = {};
|
||||
if (addSetUpTearDownIfNotPresent === undefined) addSetUpTearDownIfNotPresent = true;
|
||||
for(var k in testSuite) {
|
||||
for (var k in testSuite) {
|
||||
if (testSuite.hasOwnProperty(k)) {
|
||||
testSuite[k] = wrap(k, testSuite[k]).bind(thisObj);
|
||||
}
|
||||
}
|
||||
if (!addSetUpTearDownIfNotPresent) return;
|
||||
if (!testSuite.hasOwnProperty('setUp')) testSuite['setUp'] = setUp.bind(thisObj);
|
||||
if (!testSuite.hasOwnProperty('setUp')) {
|
||||
switch (process.env.EVENTSTORE_CONNECTION_TYPE) {
|
||||
case 'gossip':
|
||||
testSuite['setUp'] = setUpWithGossipSeeds.bind(thisObj);
|
||||
break;
|
||||
case 'dns':
|
||||
testSuite['setUp'] = setUpWithDns.bind(thisObj);
|
||||
break;
|
||||
case 'tcp':
|
||||
default:
|
||||
testSuite['setUp'] = setUpWithTcpEndpoint.bind(thisObj);
|
||||
}
|
||||
}
|
||||
if (!testSuite.hasOwnProperty('tearDown')) testSuite['tearDown'] = tearDown.bind(thisObj);
|
||||
};
|
||||
module.exports.settings = function(settingsOverride) {
|
||||
module.exports.settings = function (settingsOverride) {
|
||||
var obj = {};
|
||||
for(var prop in settings) {
|
||||
for (var prop in settings) {
|
||||
obj[prop] = settings[prop];
|
||||
}
|
||||
if (!settingsOverride) return obj;
|
||||
for(var prop in settingsOverride) {
|
||||
for (var prop in settingsOverride) {
|
||||
obj[prop] = settingsOverride[prop];
|
||||
}
|
||||
return obj;
|
||||
|
Reference in New Issue
Block a user