Skip to content

Commit

Permalink
attempt to run tests with hapi-pg ... hangs. dwyl/hapi-login-example-…
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Mar 12, 2016
1 parent e72e006 commit b8dee4c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 52 deletions.
81 changes: 42 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
var pg = require('pg');
var assert = require('assert');
var internals = {};
var pkg = require('./package.json');

var CLIENT, DONE, CON = 0; // Don't worry these "Globals" are scoped by module

function connect (callback) {
console.log('>>>> CON: ', ++CON);
if (CLIENT || DONE) {
console.log(CLIENT, DONE);
return callback();
}
pg.connect(process.env.POSTGRES_URL, function(err, client, done) {
if(err) {
return callback(err);
}
CLIENT = client; // make available for all subs
DONE = done;
return callback(err);
});
}
exports.connect = connect;
var pg = require('pg.js');
var Hoek = require('hoek');

var defaults = {
connectionString: undefined,
attach: 'onPreHandler',
detach: 'tail'
};

exports.register = function(server, options, next) {
// if POSTGRES_URL Environment Variable is unset halt the server.start
assert(process.env.POSTGRES_URL, 'Please set POSTGRES_URL Env Variable');

server.ext('onPreHandler', function (request, reply) {
connect(function (err) {
server.log(['info', 'hapi-postgres-connection'], 'DB Connection Active');
request.pg = {
client: CLIENT,
done: DONE
var config = Hoek.applyToDefaults(defaults, options);

server.ext(config.attach, function(request, reply) {
var connectionString = generateConnection(options.connectionString, request);

// if a connection string is not resolved, we stop the process
if(!connectionString) {
return reply.continue();
}

pg.connect(connectionString, function(err, client, done) {
if ( err ) throw err;

request.postgres = {
client: client,
done: done
}
reply.continue();
});

});

server.on('tail', function(request, err) {
CLIENT.end();
DONE();
server.log(['info', 'hapi-postgres-connection'], 'DB Connection Closed');
server.on('stop', function(request, err) {
console.log('STOP!!')
if ( request.postgres ) {
request.postgres.done();
request.postgres.client.end();
}
});

next();
};
}

exports.register.attributes = {
pkg: pkg
};
pkg: require('./package.json')
}

function generateConnection(connectionString, request) {
if(typeof connectionString === 'function') {
return connectionString(request);
} else {
return connectionString;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"decache": "^3.0.5",
"hapi": "^13.1.0",
"hapi-pg": "^1.0.1",
"istanbul": "^0.4.2",
"jshint": "^2.9.1",
"pg-escape": "^0.2.0",
Expand Down
31 changes: 21 additions & 10 deletions test/server_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ var assert = require('assert');

var server = new Hapi.Server({ debug: { request: ['error'] } });

var hapiPgOpts = {
connectionString: process.env.POSTGRES_URL
};

server.connection();
server.register({ register: require('../index.js') }, function(err) {
assert(!err, 'No error connecting to postgres');

server.register({
register: require('hapi-pg'),
options: hapiPgOpts
}, function (err) {
if (err) {
console.error(err);
throw err;
}
});

server.route({
Expand All @@ -15,9 +26,9 @@ server.route({
handler: function(request, reply) {
var email = '[email protected]';
var select = escape('SELECT * FROM people WHERE (email = %L)', email);
request.pg.client.query(select, function(err, result) {
request.postgres.client.query(select, function(err, result) {
// console.log(err, result);
request.pg.done();
request.postgres.done();
return reply(result.rows[0]);
})
}
Expand All @@ -30,12 +41,12 @@ server.route({
var insert = escape('INSERT INTO logs (message) VALUES (%L)',
request.payload.message);
var select = 'SELECT * FROM logs WHERE (log_id = 2)';
request.pg.client.query(insert, function(err, result) {
console.log(err, result);
// request.pg.done();
request.pg.client.query(select, function(err, result) {
console.log(err, result);
// request.pg.done();
request.postgres.client.query(insert, function(err, result) {
// console.log(err, result);
// request.postgres.done();
request.postgres.client.query(select, function(err, result) {
// console.log(err, result);
// request.postgres.done();
return reply(result.rows[0]);
})
})
Expand Down
5 changes: 2 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test("Test connecting to an invalid POSTGRES_URL", function (t) {

var server = require('./server_example.js');

test("Connect to Valid POSTGRES_URL", function (t) {
test.only("Connect to Valid POSTGRES_URL", function (t) {
server.inject('/', function(response) {
// t.equal(response.statusCode, 200, "Find Person in Database");
t.equal(response.result.id, 1, "Person found in Postgres DB")
Expand All @@ -48,8 +48,7 @@ test("Connect to Valid POSTGRES_URL", function (t) {
}
server.inject(options, function(response) {
// t.equal(response.statusCode, 200, "Find Person in Database");
t.equal(response.result.log_id, 1, "Log found in Postgres DB")
t.equal(response.result.log_id, 2, "Log found in Postgres DB")
server.stop(function(){ t.end() });
});

});

0 comments on commit b8dee4c

Please sign in to comment.