validate-async-stress-test.js
2.06 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
var tap = require('tap')
var poolModule = require('..')
tap.test('validateAsync multiple calls', function (t) {
var create_count = 0
var pool = poolModule.Pool({
name: 'test',
create: function (callback) {
setTimeout(function () {
create_count += 1
callback(null, { id: create_count })
}, 50)
},
validateAsync: function (resource, callback) {
// console.log( "setTimeout Validate object count:", resource.count )
setTimeout(function () {
// console.log( "Validating object count:", resource.count )
callback(true)
}, 100)
},
destroy: function (client) {},
max: 3,
idleTimeoutMillis: 100,
log: false
})
var borrowedObjects = []
var acquire_release = function (num, in_use_count, available_count, release_timeout) {
release_timeout = release_timeout || 100
in_use_count = in_use_count === undefined ? 0 : in_use_count
available_count = available_count === undefined ? 0 : available_count
// console.log("Request " + num + " - available " + pool.availableObjectsCount())
pool.acquire(function (err, obj) {
// check we haven't already borrowed this before:
t.equal(borrowedObjects.indexOf(obj), -1, 'acquire returned an object is currently acquired')
borrowedObjects.push(obj)
// console.log( "Acquire " + num + " - object id:", obj.id )
t.error(err)
t.ok(create_count <= 3)
setTimeout(function () {
var pos = borrowedObjects.indexOf(obj)
borrowedObjects.splice(pos, 1)
// console.log( "Release " + num + " - object id:", obj.id )
pool.release(obj)
}, release_timeout)
})
}
acquire_release(1, 1)
acquire_release(2, 2)
acquire_release(3)
acquire_release(4)
setTimeout(function () {
acquire_release(5)
acquire_release(6)
acquire_release(7)
acquire_release(8, 3, 0, 50)
acquire_release(9, 3, 0, 50)
acquire_release(10, 3, 0, 50)
acquire_release(11)
acquire_release(12)
pool.drain(function () {
t.end()
})
}, 110)
})