2011-02-24 09:50:43 +08:00
|
|
|
//make assert a global...
|
|
|
|
assert = require('assert');
|
2010-10-24 11:07:00 +08:00
|
|
|
|
2016-06-21 22:53:09 +08:00
|
|
|
//support for node@0.10.x
|
|
|
|
if (typeof Promise == 'undefined') {
|
|
|
|
global.Promise = require('promise-polyfill')
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:50:43 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var sys = require('util');
|
2011-02-24 09:50:43 +08:00
|
|
|
var BufferList = require(__dirname+'/buffer-list')
|
2010-11-04 05:47:26 +08:00
|
|
|
|
2011-08-29 15:35:08 +08:00
|
|
|
var Connection = require(__dirname + '/../lib/connection');
|
2010-10-24 11:07:00 +08:00
|
|
|
|
2011-03-04 02:21:43 +08:00
|
|
|
Client = require(__dirname + '/../lib').Client;
|
2011-02-23 12:48:51 +08:00
|
|
|
|
2010-12-19 17:41:11 +08:00
|
|
|
process.on('uncaughtException', function(d) {
|
|
|
|
if ('stack' in d && 'message' in d) {
|
|
|
|
console.log("Message: " + d.message);
|
|
|
|
console.log(d.stack);
|
|
|
|
} else {
|
|
|
|
console.log(d);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-10-24 11:07:00 +08:00
|
|
|
assert.same = function(actual, expected) {
|
|
|
|
for(var key in expected) {
|
|
|
|
assert.equal(actual[key], expected[key]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-20 09:04:48 +08:00
|
|
|
assert.emits = function(item, eventName, callback, message) {
|
2010-10-24 11:07:00 +08:00
|
|
|
var called = false;
|
|
|
|
var id = setTimeout(function() {
|
2012-12-11 12:44:58 +08:00
|
|
|
test("Should have called '" + eventName + "' event", function() {
|
2011-05-20 09:04:48 +08:00
|
|
|
assert.ok(called, message || "Expected '" + eventName + "' to be called.")
|
2010-10-25 12:18:23 +08:00
|
|
|
});
|
2012-08-07 21:36:30 +08:00
|
|
|
},5000);
|
2010-10-24 11:07:00 +08:00
|
|
|
|
|
|
|
item.once(eventName, function() {
|
2011-09-22 19:36:12 +08:00
|
|
|
if (eventName === 'error') {
|
|
|
|
// belt and braces test to ensure all error events return an error
|
|
|
|
assert.ok(arguments[0] instanceof Error,
|
|
|
|
"Expected error events to throw instances of Error but found: " + sys.inspect(arguments[0]));
|
|
|
|
}
|
2010-10-24 11:07:00 +08:00
|
|
|
called = true;
|
|
|
|
clearTimeout(id);
|
|
|
|
assert.ok(true);
|
|
|
|
if(callback) {
|
|
|
|
callback.apply(item, arguments);
|
|
|
|
}
|
|
|
|
});
|
2010-11-01 13:25:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.UTCDate = function(actual, year, month, day, hours, min, sec, milisecond) {
|
|
|
|
var actualYear = actual.getUTCFullYear();
|
|
|
|
assert.equal(actualYear, year, "expected year " + year + " but got " + actualYear);
|
|
|
|
|
2011-08-13 00:17:43 +08:00
|
|
|
var actualMonth = actual.getUTCMonth();
|
|
|
|
assert.equal(actualMonth, month, "expected month " + month + " but got " + actualMonth);
|
|
|
|
|
2010-11-01 13:25:03 +08:00
|
|
|
var actualDate = actual.getUTCDate();
|
|
|
|
assert.equal(actualDate, day, "expected day " + day + " but got " + actualDate);
|
|
|
|
|
|
|
|
var actualHours = actual.getUTCHours();
|
|
|
|
assert.equal(actualHours, hours, "expected hours " + hours + " but got " + actualHours);
|
|
|
|
|
|
|
|
var actualMin = actual.getUTCMinutes();
|
|
|
|
assert.equal(actualMin, min, "expected min " + min + " but got " + actualMin);
|
|
|
|
|
|
|
|
var actualSec = actual.getUTCSeconds();
|
|
|
|
assert.equal(actualSec, sec, "expected sec " + sec + " but got " + actualSec);
|
2010-10-24 11:07:00 +08:00
|
|
|
|
2010-11-01 13:25:03 +08:00
|
|
|
var actualMili = actual.getUTCMilliseconds();
|
|
|
|
assert.equal(actualMili, milisecond, "expected milisecond " + milisecond + " but got " + actualMili);
|
2010-10-24 11:07:00 +08:00
|
|
|
};
|
|
|
|
|
2011-01-15 05:18:56 +08:00
|
|
|
var spit = function(actual, expected) {
|
|
|
|
console.log("");
|
|
|
|
console.log("actual " + sys.inspect(actual));
|
|
|
|
console.log("expect " + sys.inspect(expected));
|
|
|
|
console.log("");
|
|
|
|
}
|
|
|
|
|
2010-10-24 11:07:00 +08:00
|
|
|
assert.equalBuffers = function(actual, expected) {
|
|
|
|
if(actual.length != expected.length) {
|
2011-01-15 05:18:56 +08:00
|
|
|
spit(actual, expected)
|
2010-10-24 11:07:00 +08:00
|
|
|
assert.equal(actual.length, expected.length);
|
|
|
|
}
|
|
|
|
for(var i = 0; i < actual.length; i++) {
|
|
|
|
if(actual[i] != expected[i]) {
|
2011-01-15 05:18:56 +08:00
|
|
|
spit(actual, expected)
|
2010-10-24 11:07:00 +08:00
|
|
|
}
|
|
|
|
assert.equal(actual[i],expected[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.empty = function(actual) {
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(actual, 0);
|
2010-10-24 11:07:00 +08:00
|
|
|
};
|
|
|
|
|
2011-01-22 05:13:58 +08:00
|
|
|
assert.success = function(callback) {
|
2013-04-05 00:54:31 +08:00
|
|
|
if(callback.length === 1 || callback.length === 0) {
|
2013-03-08 05:57:00 +08:00
|
|
|
return assert.calls(function(err, arg) {
|
|
|
|
if(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2014-09-14 13:23:02 +08:00
|
|
|
assert(!err);
|
2013-03-08 05:57:00 +08:00
|
|
|
callback(arg);
|
|
|
|
});
|
|
|
|
} else if (callback.length === 2) {
|
|
|
|
return assert.calls(function(err, arg1, arg2) {
|
|
|
|
if(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2014-09-14 13:23:02 +08:00
|
|
|
assert(!err);
|
2013-03-08 05:57:00 +08:00
|
|
|
callback(arg1, arg2);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error('need to preserve arrity of wrapped function');
|
|
|
|
}
|
2011-01-22 05:13:58 +08:00
|
|
|
}
|
|
|
|
|
2011-02-25 11:50:17 +08:00
|
|
|
assert.throws = function(offender) {
|
|
|
|
try {
|
|
|
|
offender();
|
|
|
|
} catch (e) {
|
2011-09-22 19:36:12 +08:00
|
|
|
assert.ok(e instanceof Error, "Expected " + offender + " to throw instances of Error");
|
2011-02-25 11:50:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert.ok(false, "Expected " + offender + " to throw exception");
|
|
|
|
}
|
2010-10-24 11:07:00 +08:00
|
|
|
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs = function(actual, expectedLength) {
|
2010-10-24 11:07:00 +08:00
|
|
|
assert.equal(actual.length, expectedLength);
|
|
|
|
};
|
|
|
|
|
2010-12-10 10:50:19 +08:00
|
|
|
var expect = function(callback, timeout) {
|
2010-12-03 04:43:17 +08:00
|
|
|
var executed = false;
|
2014-05-02 12:32:15 +08:00
|
|
|
timeout = timeout || parseInt(process.env.TEST_TIMEOUT) || 5000;
|
2010-12-03 07:48:39 +08:00
|
|
|
var id = setTimeout(function() {
|
2014-05-02 12:32:15 +08:00
|
|
|
assert.ok(executed,
|
|
|
|
"Expected execution of function to be fired within " + timeout
|
|
|
|
+ " milliseconds " +
|
|
|
|
+ " (hint: export TEST_TIMEOUT=<timeout in milliseconds>"
|
2014-10-02 07:03:24 +08:00
|
|
|
+ " to change timeout globally)"
|
|
|
|
+ callback.toString());
|
2014-04-25 13:45:34 +08:00
|
|
|
}, timeout)
|
2010-12-03 04:43:17 +08:00
|
|
|
|
2013-03-08 05:57:00 +08:00
|
|
|
if(callback.length < 3) {
|
|
|
|
return function(err, queryResult) {
|
|
|
|
clearTimeout(id);
|
|
|
|
if (err) {
|
|
|
|
assert.ok(err instanceof Error, "Expected errors to be instances of Error: " + sys.inspect(err));
|
|
|
|
}
|
|
|
|
callback.apply(this, arguments)
|
|
|
|
}
|
|
|
|
} else if(callback.length == 3) {
|
|
|
|
return function(err, arg1, arg2) {
|
|
|
|
clearTimeout(id);
|
|
|
|
if (err) {
|
|
|
|
assert.ok(err instanceof Error, "Expected errors to be instances of Error: " + sys.inspect(err));
|
|
|
|
}
|
|
|
|
callback.apply(this, arguments)
|
2011-09-22 19:36:12 +08:00
|
|
|
}
|
2013-03-08 05:57:00 +08:00
|
|
|
} else {
|
|
|
|
throw new Error("Unsupported arrity " + callback.length);
|
2010-12-03 04:43:17 +08:00
|
|
|
}
|
2013-03-08 05:57:00 +08:00
|
|
|
|
2010-12-03 04:43:17 +08:00
|
|
|
}
|
|
|
|
assert.calls = expect;
|
|
|
|
|
2010-12-10 10:53:59 +08:00
|
|
|
assert.isNull = function(item, message) {
|
|
|
|
message = message || "expected " + item + " to be null";
|
|
|
|
assert.ok(item === null, message);
|
|
|
|
};
|
|
|
|
|
2010-10-24 11:07:00 +08:00
|
|
|
test = function(name, action) {
|
2010-12-10 10:50:19 +08:00
|
|
|
test.testCount ++;
|
2012-12-11 12:26:23 +08:00
|
|
|
test[name] = action;
|
|
|
|
var result = test[name]();
|
2010-12-10 10:50:19 +08:00
|
|
|
if(result === false) {
|
2012-05-31 11:12:14 +08:00
|
|
|
process.stdout.write('?');
|
2010-12-10 10:50:19 +08:00
|
|
|
}else{
|
2012-05-31 11:12:14 +08:00
|
|
|
process.stdout.write('.');
|
2010-10-24 11:07:00 +08:00
|
|
|
}
|
|
|
|
};
|
2010-10-25 14:30:14 +08:00
|
|
|
|
2010-12-15 08:50:18 +08:00
|
|
|
//print out the filename
|
|
|
|
process.stdout.write(require('path').basename(process.argv[1]));
|
2012-05-31 11:12:14 +08:00
|
|
|
var args = require(__dirname + '/cli');
|
|
|
|
if(args.binary) process.stdout.write(' (binary)');
|
|
|
|
if(args.native) process.stdout.write(' (native)');
|
|
|
|
|
2012-07-12 11:46:19 +08:00
|
|
|
process.on('exit', function() {
|
|
|
|
console.log('')
|
|
|
|
})
|
2010-12-15 08:50:18 +08:00
|
|
|
|
|
|
|
process.on('uncaughtException', function(err) {
|
|
|
|
console.error("\n %s", err.stack || err.toString())
|
|
|
|
//causes xargs to abort right away
|
|
|
|
process.exit(255);
|
|
|
|
});
|
|
|
|
|
2010-12-14 07:20:51 +08:00
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
var Sink = function(expected, timeout, callback) {
|
2012-08-07 21:36:30 +08:00
|
|
|
var defaultTimeout = 5000;
|
2010-12-14 07:20:51 +08:00
|
|
|
if(typeof timeout == 'function') {
|
|
|
|
callback = timeout;
|
|
|
|
timeout = defaultTimeout;
|
|
|
|
}
|
|
|
|
timeout = timeout || defaultTimeout;
|
|
|
|
var internalCount = 0;
|
|
|
|
var kill = function() {
|
|
|
|
assert.ok(false, "Did not reach expected " + expected + " with an idle timeout of " + timeout);
|
|
|
|
}
|
|
|
|
var killTimeout = setTimeout(kill, timeout);
|
|
|
|
return {
|
|
|
|
add: function(count) {
|
|
|
|
count = count || 1;
|
|
|
|
internalCount += count;
|
|
|
|
clearTimeout(killTimeout)
|
|
|
|
if(internalCount < expected) {
|
|
|
|
killTimeout = setTimeout(kill, timeout)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert.equal(internalCount, expected);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-25 14:30:14 +08:00
|
|
|
|
2014-03-31 04:16:27 +08:00
|
|
|
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
|
|
|
|
|
|
|
|
var setTimezoneOffset = function(minutesOffset) {
|
|
|
|
Date.prototype.getTimezoneOffset = function () { return minutesOffset; };
|
|
|
|
}
|
|
|
|
|
|
|
|
var resetTimezoneOffset = function() {
|
|
|
|
Date.prototype.getTimezoneOffset = getTimezoneOffset;
|
|
|
|
}
|
2012-05-31 11:12:14 +08:00
|
|
|
|
2010-10-25 14:30:14 +08:00
|
|
|
module.exports = {
|
2010-12-14 07:20:51 +08:00
|
|
|
Sink: Sink,
|
2011-08-29 15:35:08 +08:00
|
|
|
pg: require(__dirname + '/../lib/'),
|
2012-05-31 11:12:14 +08:00
|
|
|
args: args,
|
2011-11-22 11:42:43 +08:00
|
|
|
config: args,
|
2011-02-24 09:50:43 +08:00
|
|
|
sys: sys,
|
2014-03-31 04:16:27 +08:00
|
|
|
Client: Client,
|
|
|
|
setTimezoneOffset: setTimezoneOffset,
|
|
|
|
resetTimezoneOffset: resetTimezoneOffset
|
2010-10-25 14:30:14 +08:00
|
|
|
};
|
2010-12-14 07:20:51 +08:00
|
|
|
|
|
|
|
|