Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

passing swap test

  • Loading branch information...
commit 048eb838c25d325e1cdbae2b864aba232066430c 1 parent 8205661
James Halliday substack authored

Showing 1 changed file with 89 additions and 0 deletions. Show diff stats Hide diff stats

  1. +89 0 test/swap.js
89 test/swap.js
... ... @@ -0,0 +1,89 @@
  1 +var test = require('tap').test;
  2 +var stagecoach = require('../');
  3 +var exec = require('child_process').exec;
  4 +var http = require('http');
  5 +
  6 +test('forward', function (t) {
  7 + var p = Math.floor(Math.random() * (Math.pow(2,16) - 1e4)) + 1e4;
  8 + var p0 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4)) + 1e4;
  9 + var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4)) + 1e4;
  10 +
  11 + var s0 = http.createServer(function (req, res) {
  12 + res.setHeader('content-type', 'text/plain');
  13 + res.end('servers[0]\n');
  14 + });
  15 + s0.listen(p0, ready);
  16 +
  17 + var s1 = http.createServer(function (req, res) {
  18 + res.setHeader('content-type', 'text/plain');
  19 + res.end('servers[1]\n');
  20 + });
  21 + s1.listen(p1, ready);
  22 +
  23 + var hostname = null;
  24 + exec('hostname', function (err, out) {
  25 + hostname = out.trim();
  26 + coach.add(hostname, p1);
  27 + ready();
  28 + });
  29 +
  30 + var coach = stagecoach().listen(p, ready);
  31 + coach.add('localhost', p0);
  32 +
  33 + var pending = {};
  34 +
  35 + pending.ready = 4;
  36 + var names = [ 'servers[0]', 'servers[1]' ];
  37 + function ready () {
  38 + if (--pending.ready > 0) return;
  39 + var opts0 = {
  40 + host : 'localhost',
  41 + port : p,
  42 + path : '/'
  43 + };
  44 + http.get(opts0, function (res) {
  45 + var s = '';
  46 + res.on('data', function (buf) {
  47 + s += buf.toString();
  48 + });
  49 +
  50 + res.on('end', function () {
  51 + t.equal(s.trim(), names[0]);
  52 + done();
  53 + });
  54 + });
  55 +
  56 + var opts1 = {
  57 + host : hostname,
  58 + port : p,
  59 + path : '/'
  60 + };
  61 + http.get(opts1, function (res) {
  62 + var s = '';
  63 + res.on('data', function (buf) {
  64 + s += buf.toString();
  65 + });
  66 +
  67 + res.on('end', function () {
  68 + t.equal(s.trim(), names[1]);
  69 + done();
  70 + });
  71 + });
  72 + }
  73 +
  74 + pending.done = 2;
  75 + function done () {
  76 + if (--pending.done > 0) return;
  77 + else if (pending.done === 0) {
  78 + coach.swap('localhost', hostname);
  79 + names = [ 'servers[1]', 'servers[0]' ];
  80 + ready();
  81 + }
  82 + else if (pending.done === -2) {
  83 + s0.close();
  84 + s1.close();
  85 + coach.close();
  86 + t.end();
  87 + }
  88 + }
  89 +});

0 comments on commit 048eb83

Please sign in to comment.
Something went wrong with that request. Please try again.