Skip to content

Instantly share code, notes, and snippets.

@larkost
Created July 10, 2015 01:06
Show Gist options
  • Select an option

  • Save larkost/99c526c1d171caa4096e to your computer and use it in GitHub Desktop.

Select an option

Save larkost/99c526c1d171caa4096e to your computer and use it in GitHub Desktop.
Isolate test for RethinkDB issue #4526
var assert = require('assert');
var path = require('path');
var net = require('net')
// -- settings
var driverPort = process.env.RDB_DRIVER_PORT || (process.argv[2] ? parseInt(process.argv[2], 10) : 28015);
var serverHost = process.env.RDB_SERVER_HOST || (process.argv[3] ? parseInt(process.argv[3], 10) : 'localhost');
var dbName = 'test';
var tableName = 'test';
// -- load rethinkdb from the proper location
var r = require(path.resolve(__dirname, '..', '..', 'importRethinkDB.js')).r;
/// -- global variables
var tbl = r.db(dbName).table(tableName);
var reqlConn = null;
// -- helper functions
var givesError = function(type, msg, done) {
return function(err){
var error = null;
try {
assert(err instanceof Error, 'Expected error, but got: ' + err);
if(type.__super__ && type.__super__.name == Error.name) {
assert(
err instanceof type,
'Got wrong type of error. Expected `' + type.name + '`, got: ' + err.constructor.name
);
} else {
assert.equal(
err.constructor.name, type,
'Got wrong type of error. Expected `' + type + '`, got: ' + err.constructor.name
);
}
var _msg = err.message.replace(/ in:\n([\r\n]|.)*/m, "");
_msg = _msg.replace(/\nFailed assertion:(.|\n)*/m, "")
assert.equal(_msg, msg);
} catch (e) {
error = e;
} finally {
if (done) {
done(error);
} else if (error) {
throw error;
} else {
return true; // error is correct
}
}
}
}
var withConnection = function(fnct) {
// ensure that the shared connection 'reqlConn' is valid
if (fnct) {
// callback style
return function(done) {
r.expr(1).run(reqlConn, function(err) { // check the connection
if(err) {
// re-establish the connection
reqlConn = null;
r.connect({host:serverHost, port:driverPort}, function(err, conn) {
if(err) { done(err) }
reqlConn = conn; // cache the new connection
fnct(done, reqlConn);
})
} else {
fnct(done, reqlConn);
}
});
};
} else {
// promises style
return r.expr(1).run(reqlConn) // check the connection
.then(function() {
return reqlConn;
})
.catch(r.Error.RqlDriverError, r.Error.RqlRuntimeError, function(err) {
// re-establish the connection
reqlConn = null;
return r.connect({host:serverHost, port:driverPort})
.then(function(conn) {
reqlConn = conn; // cache the new connection
return reqlConn;
});
});
}
}
var noError = function(f){
return function(err){
assertNull(err);
f.apply({}, arguments);
};
}
var assertNull = function(x){
assert.equal(x, null);
}
var assertNotNull = function(x){
assert.notEqual(x, null);
}
// -- test
describe('connection', function(){
this.timeout(5000); // Bump timeout from the default 2000ms because some operations
// ensure reqlConn is valid before each test
beforeEach(function() { return withConnection() });
it("close:noreplyWait works even with outstanding noreplyWait calls", function(done){
var t = Date.now();
r.js('while(true);', {timeout: 0.2}).run(reqlConn, {noreply: true}, function() {});
r.js('while(true);', {timeout: 0.2}).run(reqlConn, {noreply: true}, function() {});
r.js('while(true);', {timeout: 0.2}).run(reqlConn, {noreply: true}, function() {});
reqlConn.close({'noreplyWait': false}, function (err) {
assert.ifError(err);
var duration = Date.now() - t;
assert(duration < 200);
done();
});
});
describe('alpha', function(){
it("close:noreplyWait works even with outstanding noreplyWait calls", function(done){
var t = Date.now();
r.js('while(true);', {timeout: 0.2}).run(reqlConn, {noreply: true}, function() {});
reqlConn.close({'noreplyWait': false}, function (err) {
assert.ifError(err);
var duration = Date.now() - t;
assert(duration < 200);
done();
});
});
});
describe('beta', function(){
it("reconnect:noreplyWait works even with outstanding noreplyWait calls", function(done){
var t = Date.now();
r.js('while(true);', {timeout: 0.2}).run(reqlConn, {noreply: true}, function() {});
reqlConn.reconnect({'noreplyWait': false}, function (err) {
assert.ifError(err);
var duration = Date.now() - t;
assert(duration < 200);
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment