aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/redis/connection_breaker.js
blob: 489f5d56450f9b55fb793cf063964f24344d065f (plain)
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
var net = require('net');

var proxyPort = 6379;
var counter = 0;

function breaker(conn) {
    conn.end();
    conn.destroy();
}

var server = net.createServer(function(conn) {
    counter++;
    var proxyConn = net.createConnection({
        port: proxyPort
    });
    conn.pipe(proxyConn);
    proxyConn.pipe(conn);
    proxyConn.on('end', function() {
        conn.end();
    });
    conn.on('end', function() {
        proxyConn.end();
    });
    conn.on('close', function() {
        proxyConn.end();
    });
    proxyConn.on('close', function() {
        conn.end();
    });
    proxyConn.on('error', function() {
        conn.end();
    });
    conn.on('error', function() {
        proxyConn.end();
    });

    setTimeout(breaker.bind(null, conn), Math.floor(Math.random() * 2000));
});
server.listen(6479);

var redis = require('./');

var port = 6479;

var client = redis.createClient(6479, 'localhost');

function iter() {
    var k = "k" + Math.floor(Math.random() * 10);
    var coinflip = Math.random() > 0.5;
    if (coinflip) {
        client.set(k, k, function(err, resp) {
            if (!err && resp !== "OK") {
                console.log("Unexpected set response " + resp);
            }
        });
    } else {
        client.get(k, function(err, resp) {
            if (!err) {
                if (k !== resp) {
                    console.log("Key response mismatch: " + k + " " + resp);
                }
            }
        });
    }
}

function iters() {
    for (var i = 0; i < 100; ++i) {
        iter();
    }
    setTimeout(iters, 10);
}

client.on("connect", function () {
    iters();
});

client.on("error", function (err) {
    console.log("Client error " + err);
});