-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest-connection-cycling-round-robin.js
47 lines (41 loc) · 1.28 KB
/
test-connection-cycling-round-robin.js
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
var after = require('after');
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({port: common.fakeServerPort});
var server = common.createFakeServer();
var numberOfConnections = 4;
var connectionIds = [];
var connectionNumber = 0;
server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);
var getConnectionAndEnsureItsTheCorrectOne = function(cb) {
pool.getConnection(function (err, connection) {
assert.ifError(err);
assert.ok(connection.id === connectionIds[connectionNumber]);
connectionNumber++;
connection.release();
if (cb) cb();
});
};
var done = after(numberOfConnections, function () {
getConnectionAndEnsureItsTheCorrectOne(function() {
getConnectionAndEnsureItsTheCorrectOne(function() {
pool.end(function (err) {
assert.ifError(err);
server.destroy();
});
});
});
});
var counter = 1;
var createIndexedConnection = function () {
pool.getConnection(function (err, connection) {
assert.ifError(err);
connection.id = counter++;
connectionIds.push(connection.id);
connection.release();
done();
});
};
Array.apply(null, Array(numberOfConnections)).map(createIndexedConnection);
});