Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

shuffle balancer specs

  • Loading branch information...
commit 83551321c901c80d1ac83456fab54ce673649f56 1 parent 86f5850
@igrigorik igrigorik authored
View
2  Rakefile
@@ -9,7 +9,7 @@ begin
gemspec.email = "ilya@igvita.com"
gemspec.homepage = "http://github.com/igrigorik/em-proxy"
gemspec.authors = ["Ilya Grigorik"]
- gemspec.add_dependency('eventmachine', '>= 0.12.9')
+ gemspec.add_dependency("eventmachine", ">= 0.12.9")
gemspec.rubyforge_project = "em-proxy"
end
View
1  autotest/discover.rb
@@ -0,0 +1 @@
+Autotest.add_discovery { 'rspec2' }
View
4 examples/balancing.rb
@@ -1,6 +1,6 @@
require 'lib/em-proxy'
require 'ansi/code'
-require 'rack'
+require 'uri'
module BalancingProxy
extend self
@@ -133,6 +133,8 @@ def run(host='0.0.0.0', port=9999)
if __FILE__ == $0
+ require 'rack'
+
class Proxy
def self.stop
puts "Terminating ProxyServer"
View
103 spec/balancing_spec.rb
@@ -0,0 +1,103 @@
+require 'spec/helper'
+require 'examples/balancing'
+
+describe BalancingProxy do
+
+ before(:each) do
+ class BalancingProxy::Backend
+ @list = nil; @pool = nil
+ end
+ end
+
+ before(:all) do
+ @original_stdout = $stdout
+ # Silence the noisy STDOUT output
+ $stdout = File.new('/dev/null', 'w')
+ end
+
+ after(:all) do
+ $stdout = @original_stdout
+ end
+
+ context "generally" do
+
+ it "should raise error for unknown strategy" do
+ lambda { BalancingProxy::Backend.select(:asdf)}.should raise_error(ArgumentError)
+ end
+
+ end
+
+ context "when using the 'random' strategy" do
+
+ it "should select random backend" do
+ class BalancingProxy::Backend
+ def self.list
+ @list ||= [
+ {"http://127.0.0.1:3000" => 0},
+ {"http://127.0.0.2:3000" => 0},
+ {"http://127.0.0.3:3000" => 0}
+ ]
+ end
+ end
+
+ srand(0)
+ BalancingProxy::Backend.select(:random).host.should == '127.0.0.1'
+ end
+
+ end
+
+ context "when using the 'roundrobin' strategy" do
+ it "should select backends in rotating order" do
+ class BalancingProxy::Backend
+ def self.list
+ @list ||= [
+ {"http://127.0.0.1:3000" => 0},
+ {"http://127.0.0.2:3000" => 0},
+ {"http://127.0.0.3:3000" => 0}
+ ]
+ end
+ end
+
+ BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
+ BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.2'
+ BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.3'
+ BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
+ end
+ end
+
+ context "when using the 'balanced' strategy" do
+
+ it "should select the first backend when all backends have the same load" do
+ class BalancingProxy::Backend
+ def self.list
+ @list ||= [
+ {"http://127.0.0.3:3000" => 0},
+ {"http://127.0.0.2:3000" => 0},
+ {"http://127.0.0.1:3000" => 0}
+ ]
+ end
+ end
+
+ BalancingProxy::Backend.select.host.should == '127.0.0.3'
+ end
+
+ it "should select the least loaded backend" do
+ class BalancingProxy::Backend
+ def self.list
+ @list ||= [
+ {"http://127.0.0.3:3000" => 2},
+ {"http://127.0.0.2:3000" => 1},
+ {"http://127.0.0.1:3000" => 0}
+ ]
+ end
+ end
+
+ BalancingProxy::Backend.select.host.should == '127.0.0.1'
+ BalancingProxy::Backend.select.host.should == '127.0.0.1'
+ BalancingProxy::Backend.select.host.should == '127.0.0.2'
+ BalancingProxy::Backend.select.host.should == '127.0.0.3'
+ end
+
+ end
+
+end
View
4 spec/helper.rb
@@ -1,6 +1,6 @@
require 'rubygems'
-require 'spec'
+require 'rspec'
require 'pp'
require 'em-http'
-require 'lib/em-proxy'
+require 'lib/em-proxy'
View
106 spec/proxy_spec.rb
@@ -164,108 +164,4 @@ def failed
end
}.should_not raise_error
end
-end
-
-require 'examples/balancing'
-require 'rack'
-
-describe BalancingProxy do
-
- before(:each) do
- class BalancingProxy::Backend
- @list = nil; @pool = nil
- end
- end
-
- before(:all) do
- @original_stdout = $stdout
- # Silence the noisy STDOUT output
- $stdout = File.new('/dev/null', 'w')
- end
-
- after(:all) do
- $stdout = @original_stdout
- end
-
- context "generally" do
-
- it "should raise error for unknown strategy" do
- BalancingProxy::Backend.select(:asdf).should raise_error(ArgumentError)
- end
-
- end
-
- context "when using the 'random' strategy" do
-
- it "should select random backend" do
- class BalancingProxy::Backend
- def self.list
- @list ||= [
- {"http://127.0.0.1:3000" => 0},
- {"http://127.0.0.2:3000" => 0},
- {"http://127.0.0.3:3000" => 0}
- ]
- end
- end
-
- srand(0)
- BalancingProxy::Backend.select(:random).host.should == '127.0.0.1'
- end
-
- end
-
- context "when using the 'roundrobin' strategy" do
- it "should select backends in rotating order" do
- class BalancingProxy::Backend
- def self.list
- @list ||= [
- {"http://127.0.0.1:3000" => 0},
- {"http://127.0.0.2:3000" => 0},
- {"http://127.0.0.3:3000" => 0}
- ]
- end
- end
-
- BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
- BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.2'
- BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.3'
- BalancingProxy::Backend.select(:roundrobin).host.should == '127.0.0.1'
- end
- end
-
- context "when using the 'balanced' strategy" do
-
- it "should select the first backend when all backends have the same load" do
- class BalancingProxy::Backend
- def self.list
- @list ||= [
- {"http://127.0.0.3:3000" => 0},
- {"http://127.0.0.2:3000" => 0},
- {"http://127.0.0.1:3000" => 0}
- ]
- end
- end
-
- BalancingProxy::Backend.select.host.should == '127.0.0.3'
- end
-
- it "should select the least loaded backend" do
- class BalancingProxy::Backend
- def self.list
- @list ||= [
- {"http://127.0.0.3:3000" => 2},
- {"http://127.0.0.2:3000" => 1},
- {"http://127.0.0.1:3000" => 0}
- ]
- end
- end
-
- BalancingProxy::Backend.select.host.should == '127.0.0.1'
- BalancingProxy::Backend.select.host.should == '127.0.0.1'
- BalancingProxy::Backend.select.host.should == '127.0.0.2'
- BalancingProxy::Backend.select.host.should == '127.0.0.3'
- end
-
- end
-
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.