GH-159-test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const tap = require("tap");
  2. const createPool = require("../").createPool;
  3. const utils = require("./utils");
  4. const ResourceFactory = utils.ResourceFactory;
  5. class ResourceFactoryDelayCreateEachSecond {
  6. constructor() {
  7. this.callCreate = 0;
  8. this.created = 0;
  9. this.destroyed = 0;
  10. this.bin = [];
  11. }
  12. create() {
  13. const that = this;
  14. console.log(`** create call ${that.callCreate}`);
  15. return new Promise(resolve => {
  16. if (that.callCreate % 2 === 0) {
  17. setTimeout(function() {
  18. console.log(`** created ${that.created}`);
  19. resolve({ id: that.created++ });
  20. }, 10);
  21. } else {
  22. console.log(`** created ${that.created}`);
  23. resolve({ id: that.created++ });
  24. }
  25. that.callCreate++;
  26. });
  27. }
  28. validate() {
  29. return Promise.resolve(true);
  30. }
  31. destroy(resource) {
  32. console.log(`** destroying ${resource.id}`);
  33. this.destroyed++;
  34. this.bin.push(resource);
  35. return Promise.resolve();
  36. }
  37. }
  38. tap.test("tests drain clear with autostart and min > 0", function(t) {
  39. const count = 5;
  40. let acquired = 0;
  41. const resourceFactory = new ResourceFactoryDelayCreateEachSecond();
  42. const config = {
  43. max: 10,
  44. min: 1,
  45. evictionRunIntervalMillis: 500,
  46. idleTimeoutMillis: 30000,
  47. testOnBorrow: true,
  48. autostart: true
  49. };
  50. const pool = createPool(resourceFactory, config);
  51. return pool
  52. .drain()
  53. .then(function() {
  54. console.log("** pool drained");
  55. return pool.clear();
  56. })
  57. .then(function() {
  58. console.log("** pool cleared");
  59. t.equal(resourceFactory.created, resourceFactory.destroyed);
  60. })
  61. .then(function() {
  62. t.end();
  63. });
  64. });