connection_breaker.js 1.7 KB

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