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