generic-pool-acquiretimeout-test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. const tap = require("tap");
  3. const createPool = require("../").createPool;
  4. tap.test("acquireTimeout handles timed out acquire calls", function(t) {
  5. const factory = {
  6. create: function() {
  7. return new Promise(function(resolve) {
  8. setTimeout(function() {
  9. resolve({});
  10. }, 100);
  11. });
  12. },
  13. destroy: function() {
  14. return Promise.resolve();
  15. }
  16. };
  17. const config = {
  18. acquireTimeoutMillis: 20,
  19. idleTimeoutMillis: 150,
  20. log: false
  21. };
  22. const pool = createPool(factory, config);
  23. pool
  24. .acquire()
  25. .then(function(resource) {
  26. t.fail("wooops");
  27. })
  28. .catch(function(err) {
  29. t.match(err, /ResourceRequest timed out/);
  30. return pool.drain();
  31. })
  32. .then(function() {
  33. return pool.clear();
  34. })
  35. .then(function() {})
  36. .then(t.end)
  37. .catch(t.error);
  38. });
  39. tap.test("acquireTimeout handles non timed out acquire calls", function(t) {
  40. const myResource = {};
  41. const factory = {
  42. create: function() {
  43. return new Promise(function(resolve) {
  44. setTimeout(function() {
  45. resolve(myResource);
  46. }, 10);
  47. });
  48. },
  49. destroy: function() {
  50. return Promise.resolve();
  51. }
  52. };
  53. const config = {
  54. acquireTimeoutMillis: 400
  55. };
  56. const pool = createPool(factory, config);
  57. pool
  58. .acquire()
  59. .then(function(resource) {
  60. t.equal(resource, myResource);
  61. pool.release(resource);
  62. return pool.drain();
  63. })
  64. .then(function() {
  65. return pool.clear();
  66. })
  67. .then(t.end)
  68. .catch(t.error);
  69. });