release-connection.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var assert = require('assert');
  2. var Pack = require('../../');
  3. describe('Connectable ::', function() {
  4. describe('Release Connection', function() {
  5. var manager;
  6. var connection;
  7. // Create a manager and connection
  8. before(function(done) {
  9. // Needed to dynamically get the host using the docker container
  10. var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
  11. Pack.createManager({
  12. connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
  13. })
  14. .exec(function(err, report) {
  15. if (err) {
  16. return done(err);
  17. }
  18. manager = report.manager;
  19. Pack.getConnection({
  20. manager: manager
  21. })
  22. .exec(function(err, report) {
  23. if (err) {
  24. return done(err);
  25. }
  26. connection = report.connection;
  27. return done();
  28. });
  29. });
  30. });
  31. it('should successfully release a connection', function(done) {
  32. // Grab the number of free connections before releasing the current one
  33. var freeConnectionsPreRelease = manager.pool._freeConnections.length;
  34. // Release the connection
  35. Pack.releaseConnection({
  36. connection: connection
  37. })
  38. .exec(function(err) {
  39. if (err) {
  40. return done(err);
  41. }
  42. // If the connection was successfully released the _allConnections and the
  43. // _freeConnections should be equal.
  44. // https://github.com/mysqljs/mysql/blob/master/lib/Pool.js
  45. var poolSize = manager.pool._allConnections.length;
  46. var freeConnectionsPostRelease = manager.pool._freeConnections.length;
  47. // Ensure we end up with different counts after releasing the connection
  48. assert.notEqual(freeConnectionsPostRelease, freeConnectionsPreRelease);
  49. // Ensure that all the available connections are free
  50. assert.equal(poolSize, freeConnectionsPostRelease);
  51. return done();
  52. });
  53. });
  54. });
  55. });