generic-pool-acquiretimeout-test.js
1.62 KB
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
72
73
74
75
76
77
"use strict";
const tap = require("tap");
const createPool = require("../").createPool;
tap.test("acquireTimeout handles timed out acquire calls", function(t) {
const factory = {
create: function() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve({});
}, 100);
});
},
destroy: function() {
return Promise.resolve();
}
};
const config = {
acquireTimeoutMillis: 20,
idleTimeoutMillis: 150,
log: false
};
const pool = createPool(factory, config);
pool
.acquire()
.then(function(resource) {
t.fail("wooops");
})
.catch(function(err) {
t.match(err, /ResourceRequest timed out/);
return pool.drain();
})
.then(function() {
return pool.clear();
})
.then(function() {})
.then(t.end)
.catch(t.error);
});
tap.test("acquireTimeout handles non timed out acquire calls", function(t) {
const myResource = {};
const factory = {
create: function() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve(myResource);
}, 10);
});
},
destroy: function() {
return Promise.resolve();
}
};
const config = {
acquireTimeoutMillis: 400
};
const pool = createPool(factory, config);
pool
.acquire()
.then(function(resource) {
t.equal(resource, myResource);
pool.release(resource);
return pool.drain();
})
.then(function() {
return pool.clear();
})
.then(t.end)
.catch(t.error);
});