index.js
3.56 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
global.TEST = true
global.Bottleneck = require('../lib/Bottleneck.js')
global.Cluster = require('../lib/Cluster.js')
global.makeTest = function (arg1, arg2, arg3, arg4) {
var start = Date.now()
var calls = []
var limiter = new Bottleneck(arg1, arg2, arg3, arg4)
var getResults = function () {
return {
elapsed: Date.now() - start,
callsDuration: calls[calls.length - 1].time,
calls: calls
}
}
var context = {
limiter:limiter,
size: function() {
return limiter._waitingClients.size()
},
job: function(num) {
return function(cb) {
calls.push({num:num,time:Date.now()-start})
cb()
}
},
last: function(num, check) {
return function(cb) {
calls.push({num:num,time:Date.now()-start})
cb()
console.assert(limiter._waitingClients.size() === 0)
if(check.checkResults)
context.checkResultsOrder(check.checkResults)
if(check.checkDuration)
context.checkDuration(check.checkDuration)
check.done()
}
},
checkResultsOrder: function (order) {
for (var i = 0; i < Math.max(calls.length, order.length); i++) {
console.assert(order[i] === calls[i].num)
}
},
checkDuration: function (shouldBe) {
var results = getResults()
var min = shouldBe - 10
var max = shouldBe + 50
console.assert(results.callsDuration > min)
console.assert(results.callsDuration < max)
}
}
return context
}
global.clusterTest = function(arg1, arg2, arg3, arg4, arg5) {
var start = Date.now()
var calls = []
var cluster = new Cluster(arg1, arg2, arg3, arg4, arg5)
var getResults = function () {
return {
elapsed: Date.now() - start,
callsDuration: calls[calls.length - 1].time,
calls: calls
}
}
var context = {
cluster:cluster,
size: function() {
return cluster.waitingClients
},
fastJob: function(inLimiter, value) {
return function(cb, outLimiter) {
setTimeout(function() {
calls.push({in:inLimiter,out:outLimiter,value:value,time:Date.now()-start})
cb()
}, 100)
}
},
slowJob: function(inLimiter, value) {
return function(cb, outLimiter) {
setTimeout(function() {
calls.push({in:inLimiter,out:outLimiter,value:value,time:Date.now()-start})
cb()
}, 500)
}
},
last: function(inLimiter, value, check) {
return function(cb, outLimiter) {
setTimeout(function() {
calls.push({in:inLimiter,out:outLimiter,value:value,time:Date.now()-start})
cb()
console.assert(cluster._waitingClients() === 0)
if(check.checkResults)
context.checkResultsOrder(check.checkResults)
if(check.checkDuration)
context.checkDuration(check.checkDuration)
check.done()
}, 500)
}
},
checkResultsOrder: function (order) {
for(var i = 0; i < order.length; i++) {
console.assert(order[i].in == calls[i].in)
console.assert(order[i].out == calls[i].out)
console.assert(order[i].value == calls[i].value)
}
},
checkDuration: function (shouldBe) {
var results = getResults();
var min = shouldBe - 10
var max = shouldBe + 50
console.assert(results.callsDuration > min)
console.assert(results.callsDuration < max)
}
}
return context
}
var fs = require('fs')
var files = fs.readdirSync('./test')
for (var f in files) {
var stat = fs.statSync('./test/' + files[f])
if (!stat.isDirectory()) {
try {
require('./' + files[f])
} catch (e) {
console.error(e.toString())
}
}
}