2016-03-14 01:38:42 +00:00
|
|
|
var util = require('util');
|
|
|
|
var uuid = require('uuid');
|
2019-11-01 18:30:06 +00:00
|
|
|
var client = require('../../lib/dist');
|
2016-03-14 01:38:42 +00:00
|
|
|
var FileLogger = require('../../src/common/log/fileLogger');
|
|
|
|
var NoopLogger = require('../../src/common/log/noopLogger');
|
|
|
|
|
|
|
|
var settings = {
|
|
|
|
log: new NoopLogger()
|
|
|
|
};
|
|
|
|
if (process.env.TESTS_VERBOSE_LOGGING === '1') {
|
|
|
|
settings.verboseLogging = true;
|
|
|
|
settings.log = new FileLogger('test-verbose.log');
|
|
|
|
}
|
|
|
|
|
2017-01-29 02:35:35 +00:00
|
|
|
var tcpEndPoint = {host: 'localhost', port: 1113};
|
2016-03-14 01:38:42 +00:00
|
|
|
|
|
|
|
function setUp(cb) {
|
|
|
|
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);
|
|
|
|
});
|
2016-10-20 11:40:46 +00:00
|
|
|
this.conn.on('connected', function (tcpEndPoint) {
|
|
|
|
if (connected) return;
|
2016-03-14 01:38:42 +00:00
|
|
|
settings.log.debug("Connected to %j.", tcpEndPoint);
|
|
|
|
connected = true;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown(cb) {
|
|
|
|
this.conn.close();
|
|
|
|
this.conn.on('closed', function() {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
this.conn = null;
|
|
|
|
}
|
|
|
|
|
2016-03-15 00:55:35 +00:00
|
|
|
function areEqual(name, actual, expected) {
|
2016-03-14 01:38:42 +00:00
|
|
|
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));
|
2016-03-15 00:55:35 +00:00
|
|
|
}
|
2016-03-14 01:38:42 +00:00
|
|
|
|
2016-03-15 00:55:35 +00:00
|
|
|
function fail(reason) {
|
2016-03-14 01:38:42 +00:00
|
|
|
this.ok(false, reason);
|
2016-03-15 00:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function eventEqualEventData(name, resolvedEvent, eventData) {
|
|
|
|
var ev = resolvedEvent.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.");
|
|
|
|
}
|
2016-03-14 01:38:42 +00:00
|
|
|
|
2017-03-11 22:31:24 +00:00
|
|
|
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)");
|
|
|
|
if (typeof evNumber === 'number') {
|
2017-10-17 22:55:07 +00:00
|
|
|
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
2017-03-11 22:31:24 +00:00
|
|
|
} else {
|
|
|
|
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)");
|
|
|
|
if (typeof evNumber === 'number') {
|
2017-10-17 22:55:07 +00:00
|
|
|
this.ok(event.originalEventNumber.toNumber() === evNumber, name + '.originalEventNumber expected ' + evNumber + ' got ' + event.originalEventNumber);
|
2017-03-11 22:31:24 +00:00
|
|
|
} else {
|
|
|
|
this.ok(typeof event.originalEventNumber === 'number', name + ".originalEventNumber is not a number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 01:38:42 +00:00
|
|
|
var _ = {
|
|
|
|
'setUp': setUp,
|
|
|
|
'tearDown': tearDown
|
|
|
|
};
|
|
|
|
|
|
|
|
function wrap(name, testFunc) {
|
|
|
|
var base = _[name];
|
|
|
|
if (base === undefined) {
|
|
|
|
return function(test) {
|
|
|
|
settings.log.debug('--- %s ---', name);
|
|
|
|
test.areEqual = areEqual.bind(test);
|
|
|
|
test.fail = fail.bind(test);
|
2016-03-15 00:55:35 +00:00
|
|
|
test.eventEqualEventData = eventEqualEventData.bind(test);
|
2017-03-11 22:31:24 +00:00
|
|
|
test.testLiveEvent = testLiveEvent.bind(test);
|
|
|
|
test.testReadEvent = testReadEvent.bind(test);
|
2016-03-14 01:38:42 +00:00
|
|
|
return testFunc.call(this, test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return function(cb) {
|
|
|
|
var self = this;
|
|
|
|
base.call(this, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
return testFunc.call(self, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.init = function(testSuite, addSetUpTearDownIfNotPresent) {
|
|
|
|
var thisObj = {};
|
|
|
|
if (addSetUpTearDownIfNotPresent === undefined) addSetUpTearDownIfNotPresent = true;
|
|
|
|
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('tearDown')) testSuite['tearDown'] = tearDown.bind(thisObj);
|
|
|
|
};
|
|
|
|
module.exports.settings = function(settingsOverride) {
|
|
|
|
var obj = {};
|
|
|
|
for(var prop in settings) {
|
|
|
|
obj[prop] = settings[prop];
|
|
|
|
}
|
|
|
|
if (!settingsOverride) return obj;
|
|
|
|
for(var prop in settingsOverride) {
|
|
|
|
obj[prop] = settingsOverride[prop];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|