-
Notifications
You must be signed in to change notification settings - Fork 26
/
weighted-connection-pool.test.ts
277 lines (233 loc) · 8.41 KB
/
weighted-connection-pool.test.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { test } from 'tap'
import {
WeightedConnectionPool,
HttpConnection,
BaseConnection,
Connection
} from '../..'
const opts = {
now: Date.now() + 1000 * 60 * 3,
requestId: 1,
name: 'elasticsearch-js',
context: null
}
test('API', t => {
t.test('markDead', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection('http://localhost:9200/')
pool.addConnection('http://localhost:9201/')
pool.markDead(pool.connections[0])
t.equal(pool.connections[0].weight, 491)
t.equal(pool.connections[0].status, BaseConnection.statuses.DEAD)
t.equal(pool.maxWeight, 500)
t.equal(pool.greatestCommonDivisor, 1)
t.end()
})
t.test('markAlive', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection('http://localhost:9200/')
pool.addConnection('http://localhost:9201/')
pool.markDead(pool.connections[0])
pool.markAlive(pool.connections[0])
t.equal(pool.connections[0].weight, 500)
t.equal(pool.maxWeight, 500)
t.equal(pool.greatestCommonDivisor, 500)
t.equal(pool.connections[0].status, BaseConnection.statuses.ALIVE)
t.end()
})
t.test('getConnection', t => {
t.test('Should return a connection', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection('http://localhost:9200/')
t.ok(pool.getConnection(opts) instanceof HttpConnection)
t.end()
})
// TODO: test the nodes distribution with different weights
t.test('Connection distribution', t => {
t.test('3 Connections, same weight', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection([
'http://localhost:9200/',
'http://localhost:9201/',
'http://localhost:9202/'
])
try {
for (let i = 0; i < 1000; i++) {
for (let j = 0; j < 3; j++) {
if (pool.getConnection(opts)?.id !== `http://localhost:920${j}/`) {
throw new Error('Wrong distribution')
}
}
}
t.pass('Distribution is ok')
} catch (err: any) {
t.error(err)
}
t.end()
})
// t.test('3 Connections, 1 dead 1 time', t => {
// const pool = new WeightedConnectionPool({ Connection: HttpConnection })
// pool.addConnection([
// 'http://localhost:9200/',
// 'http://localhost:9201/',
// 'http://localhost:9202/'
// ])
// pool.markDead(pool.connections[1])
// // with thid distribution we expect
// // to see the dead connection every 7 gets
// try {
// var foundAt = 0
// for (var i = 0; i < 1000; i++) {
// const connection = pool.getConnection()
// if (connection.id === 'http://localhost:9201/' && foundAt === 0) {
// foundAt = i
// }
// if (connection.id === 'http://localhost:9201/') {
// if (foundAt !== i) throw new Error('Wrong distribution')
// foundAt += 7
// }
// }
// t.pass('Distribution is ok')
// } catch (err: any) {
// t.error(err)
// }
// t.end()
// })
// t.test('3 Connections, 1 dead 2 time', t => {
// const pool = new WeightedConnectionPool({ Connection: HttpConnection })
// pool.addConnection([
// 'http://localhost:9200/',
// 'http://localhost:9201/',
// 'http://localhost:9202/'
// ])
// pool.markDead(pool.connections[1])
// pool.markDead(pool.connections[1])
// // with thid distribution we expect
// // to see the dead connection every 4 times in 10 gets
// try {
// for (var i = 0; i < 100; i++) {
// const connection = pool.getConnection()
// if (connection.id === 'http://localhost:9201/') {
// if (i !== 59 && i !== 62 && i !== 65 && i !== 68) {
// throw new Error('Wrong distribution')
// }
// }
// }
// t.pass('Distribution is ok')
// } catch (err: any) {
// t.error(err)
// }
// t.end()
// })
t.test('3 Connections, 3 weights', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection([
'http://localhost:9200/',
'http://localhost:9201/',
'http://localhost:9202/'
])
pool.connections[0].weight = 4
pool.connections[0].id = 'A'
pool.connections[1].weight = 3
pool.connections[1].id = 'B'
pool.connections[2].weight = 2
pool.connections[2].id = 'C'
pool.maxWeight = 4
pool.greatestCommonDivisor = 1
const arr = []
for (let i = 0; i < 9; i++) arr.push(pool.getConnection(opts)?.id)
t.same(arr, ['A', 'A', 'B', 'A', 'B', 'C', 'A', 'B', 'C'])
t.end()
})
t.end()
})
t.test('It should not enter in an infinite loop', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection([
'http://localhost:9200/',
'http://localhost:9201/',
'http://localhost:9202/'
])
const filter = (node: Connection): boolean => false
t.equal(pool.getConnection({ ...opts, filter }), null)
t.end()
})
t.test('filter option', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
const href1 = 'http://localhost:9200/'
const href2 = 'http://localhost:9200/other'
pool.addConnection([href1, href2])
const filter = (node: Connection): boolean => node.id === href2
t.equal(pool.getConnection({ ...opts, filter })?.id, href2)
t.end()
})
t.test('filter should get Connection objects', t => {
t.plan(1)
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
const href1 = 'http://localhost:9200/'
const href2 = 'http://localhost:9200/other'
pool.addConnection([href1, href2])
const filter = (node: Connection): boolean => {
t.ok(node instanceof HttpConnection)
return true
}
pool.getConnection({ ...opts, filter })
})
t.test('filter should get alive connection first', t => {
t.plan(2)
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
const href1 = 'http://localhost:9200/'
const href2 = 'http://localhost:9200/other'
pool.addConnection([href1, href2])
pool.markDead(pool.connections[0])
const filter = (node: Connection): boolean => {
t.equal(node.id, href2)
t.equal(node.weight, 500)
return true
}
pool.getConnection({ ...opts, filter })
})
t.end()
})
t.test('empty', async t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection('http://localhost:9200/')
pool.addConnection('http://localhost:9201/')
await pool.empty()
t.same(pool.connections, [])
t.equal(pool.size, 0)
t.equal(pool.maxWeight, 0)
t.equal(pool.greatestCommonDivisor, 0)
t.equal(pool.index, -1)
t.equal(pool.currentWeight, 0)
t.end()
})
t.end()
})
test('Single node behavior', t => {
const pool = new WeightedConnectionPool({ Connection: HttpConnection })
pool.addConnection('http://localhost:9200/')
pool.markDead(pool.connections[0])
t.equal(pool.connections[0].weight, 1000)
pool.markAlive(pool.connections[0])
t.equal(pool.connections[0].weight, 1000)
t.end()
})