<commit_before>var Lab = require('lab');
var Code = require('code');
var Hapi = require('hapi');
var Proxyquire = require('proxyquire');


var stub = {
    pg: {}
};
var Plugin = Proxyquire('../', {
    'pg': stub.pg
});
var lab = exports.lab = Lab.script();
var request, server;


lab.beforeEach(function (done) {

    server = new Hapi.Server();
    server.connection({ port: 0 });
    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, reply) {

            if (request.query.kill) {
                request.pg.kill = true;
            }

            reply('hapi-node-postgres, at your service');
        }
    });

    request = {
        method: 'GET',
        url: '/'
    };

    done();
});


lab.experiment('Postgres Plugin', function () {

    lab.test('it registers the plugin', function (done) {

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();
            done();
        });
    });


    lab.test('it returns an error when the connection fails in the pre handler extension point', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            callback(Error('connect failed'));
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();
        });

        server.inject(request, function (response) {

            Code.expect(response.statusCode).to.equal(500);
            stub.pg.connect = realConnect;

            done();
        });
    });


    lab.test('it successfully returns when the connection succeeds in the pre handler extension point', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function () {};

            callback(null, {}, returnClient);
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();
        });

        server.inject(request, function (response) {

            Code.expect(response.statusCode).to.equal(200);
            stub.pg.connect = realConnect;

            done();
        });
    });


    lab.test('it successfully cleans up during the server tail event', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function (killSwitch) {

                Code.expect(killSwitch).to.equal(true);
                stub.pg.connect = realConnect;

                done();
            };

            callback(null, {}, returnClient);
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();
        });

        request.url = '/?kill=true';

        server.inject(request, function (response) {

            Code.expect(response.statusCode).to.equal(200);
            stub.pg.connect = realConnect;
        });
    });


    lab.test('it successfully uses native bindings without error', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function () {};

            callback(null, {}, returnClient);
        };

        var pluginWithConfig = {
            register: Plugin,
            options: {
                native: true
            }
        };

        server.register(pluginWithConfig, function (err) {

            Code.expect(err).to.not.exist();
        });

        server.inject(request, function (response) {

            Code.expect(response.statusCode).to.equal(200);
            stub.pg.connect = realConnect;

            done();
        });
    });
});
<commit_msg>fixed potential race condition with tests<commit_after>var Lab = require('lab');
var Code = require('code');
var Hapi = require('hapi');
var Proxyquire = require('proxyquire');


var stub = {
    pg: {}
};
var Plugin = Proxyquire('../', {
    'pg': stub.pg
});
var lab = exports.lab = Lab.script();
var request, server;


lab.beforeEach(function (done) {

    server = new Hapi.Server();
    server.connection({ port: 0 });
    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, reply) {

            if (request.query.kill) {
                request.pg.kill = true;
            }

            reply('hapi-node-postgres, at your service');
        }
    });

    request = {
        method: 'GET',
        url: '/'
    };

    done();
});


lab.experiment('Postgres Plugin', function () {

    lab.test('it registers the plugin', function (done) {

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();
            done();
        });
    });


    lab.test('it returns an error when the connection fails in the pre handler extension point', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            callback(Error('connect failed'));
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();

            server.inject(request, function (response) {

                Code.expect(response.statusCode).to.equal(500);
                stub.pg.connect = realConnect;

                done();
            });
        });
    });


    lab.test('it successfully returns when the connection succeeds in the pre handler extension point', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function () {};

            callback(null, {}, returnClient);
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();

            server.inject(request, function (response) {

                Code.expect(response.statusCode).to.equal(200);
                stub.pg.connect = realConnect;

                done();
            });
        });
    });


    lab.test('it successfully cleans up during the server tail event', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function (killSwitch) {

                Code.expect(killSwitch).to.equal(true);
                stub.pg.connect = realConnect;

                done();
            };

            callback(null, {}, returnClient);
        };

        server.register(Plugin, function (err) {

            Code.expect(err).to.not.exist();

            request.url = '/?kill=true';

            server.inject(request, function (response) {

                Code.expect(response.statusCode).to.equal(200);
                stub.pg.connect = realConnect;
            });
        });
    });


    lab.test('it successfully uses native bindings without error', function (done) {

        var realConnect = stub.pg.connect;
        stub.pg.connect = function (connection, callback) {

            var returnClient = function () {};

            callback(null, {}, returnClient);
        };

        var pluginWithConfig = {
            register: Plugin,
            options: {
                native: true
            }
        };

        server.register(pluginWithConfig, function (err) {

            Code.expect(err).to.not.exist();

            server.inject(request, function (response) {

                Code.expect(response.statusCode).to.equal(200);
                stub.pg.connect = realConnect;

                done();
            });
        });
    });
});

{'fraction_non_alphanumeric': 0.13626834381551362, 'fraction_numerical': 0.0034067085953878406, 'mean_word_length': 1.80625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6133169', 'n_tokens_mistral': 2119, 'n_tokens_neox': 1979, 'n_words': 451}