Skip to content

Commit 3203830

Browse files
committed
Add connection_breaker test tool for #622
1 parent b12817c commit 3203830

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

connection_breaker.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var net = require('net');
2+
3+
var proxyPort = 6379;
4+
var counter = 0;
5+
6+
function breaker(conn) {
7+
conn.end();
8+
conn.destroy();
9+
}
10+
11+
var server = net.createServer(function(conn) {
12+
counter++;
13+
var proxyConn = net.createConnection({
14+
port: proxyPort
15+
});
16+
conn.pipe(proxyConn);
17+
proxyConn.pipe(conn);
18+
proxyConn.on('end', function() {
19+
conn.end();
20+
});
21+
conn.on('end', function() {
22+
proxyConn.end();
23+
});
24+
conn.on('close', function() {
25+
proxyConn.end();
26+
});
27+
proxyConn.on('close', function() {
28+
conn.end();
29+
});
30+
proxyConn.on('error', function() {
31+
conn.end();
32+
});
33+
conn.on('error', function() {
34+
proxyConn.end();
35+
});
36+
37+
setTimeout(breaker.bind(null, conn), Math.floor(Math.random() * 2000));
38+
});
39+
server.listen(6479);
40+
41+
var redis = require('./');
42+
43+
var port = 6479;
44+
45+
var client = redis.createClient(6479, 'localhost');
46+
47+
function iter() {
48+
var k = "k" + Math.floor(Math.random() * 10);
49+
var coinflip = Math.random() > 0.5;
50+
if (coinflip) {
51+
client.set(k, k, function(err, resp) {
52+
if (!err && resp !== "OK") {
53+
console.log("Unexpected set response " + resp);
54+
}
55+
});
56+
} else {
57+
client.get(k, function(err, resp) {
58+
if (!err) {
59+
if (k !== resp) {
60+
console.log("Key response mismatch: " + k + " " + resp);
61+
}
62+
}
63+
});
64+
}
65+
}
66+
67+
function iters() {
68+
for (var i = 0; i < 100; ++i) {
69+
iter();
70+
}
71+
setTimeout(iters, 10);
72+
}
73+
74+
client.on("connect", function () {
75+
iters();
76+
});
77+
78+
client.on("error", function (err) {
79+
console.log("Client error " + err);
80+
});

0 commit comments

Comments
 (0)