Permalink
Browse files

adds a tcp dependency

  • Loading branch information...
1 parent 10f1972 commit a50c4bd4f115e6652b5e4652d8872e55de5a22c9 @braintreeps braintreeps committed May 30, 2012
Showing with 60 additions and 0 deletions.
  1. +1 −0 lib/ipvs_litmus.rb
  2. +24 −0 lib/ipvs_litmus/dependency/tcp.rb
  3. +35 −0 spec/ipvs_litmus/dependency/tcp_spec.rb
View
@@ -10,6 +10,7 @@
require 'ipvs_litmus/app'
require 'ipvs_litmus/configuration'
require 'ipvs_litmus/dependency/http'
+require 'ipvs_litmus/dependency/tcp'
require 'ipvs_litmus/health'
require 'ipvs_litmus/forced_health'
require 'ipvs_litmus/metric/available_memory'
@@ -0,0 +1,24 @@
+module IPVSLitmus
+ module Dependency
+ class TCP
+ def initialize(ip, port)
+ @ip, @port = ip, port
+ end
+
+ def available?
+ Timeout.timeout(5) do
+ socket = TCPSocket.new(@ip, @port)
+ socket.close
+ end
+ true
+ rescue Exception
+ false
+ end
+
+ def to_s
+ "Dependency::TCP(tcp://#{@ip}:#{@port})"
+ end
+ end
+ end
+end
+
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe IPVSLitmus::Dependency::TCP do
+ before(:all) do
+ @server = TCPServer.new 3333
+ end
+
+ after(:all) do
+ @server.close
+ end
+
+ describe "#available?" do
+ it "is true when it's able to reach the ip and port" do
+ check = IPVSLitmus::Dependency::TCP.new("127.0.0.1", 3333)
+ check.should be_available
+ end
+
+ it "is false when the ip is not available" do
+ check = IPVSLitmus::Dependency::TCP.new("127.0.0.254", 3333)
+ check.should_not be_available
+ end
+
+ it "is false when the port is not available" do
+ check = IPVSLitmus::Dependency::TCP.new("127.0.0.1", 3334)
+ check.should_not be_available
+ end
+ end
+
+ describe "to_s" do
+ it "is the name of the class and the ip and port" do
+ check = IPVSLitmus::Dependency::TCP.new("127.0.0.1", 3333)
+ check.to_s.should == "Dependency::TCP(tcp://127.0.0.1:3333)"
+ end
+ end
+end

0 comments on commit a50c4bd

Please sign in to comment.