Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tests for tcp and unix domain sockets pass; needs additional testing

  • Loading branch information...
commit 81db55e3318a17753b3d7485fae5850f47b770c4 1 parent d0f2dbf
Steven Jenkins authored
View
29 lib/god/conditions/socket_responding.rb
@@ -11,15 +11,19 @@ def initialize
# default to tcp on the localhost
self.family = 'tcp'
self.addr = '127.0.0.1'
+ # Set these to nil/0 values
+ self.port = 0
+ self.path = nil
end
def socket=(s)
components = s.split(':')
if components.size == 3
@family,@addr,@port = components
+ @port = @port.to_i
elsif components[0] =~ /^tcp$/
@family = components[0]
- @port = components[1]
+ @port = components[1].to_i
elsif components[0] =~ /^unix$/
@family = components[0]
@path = components[1]
@@ -28,7 +32,7 @@ def socket=(s)
def valid?
valid = true
- if self.family == 'tcp' and self.port.nil?
+ if self.family == 'tcp' and @port == 0
valid &= complain("Attribute 'port' must be specified for tcp sockets", self)
end
if self.family == 'unix' and self.path.nil?
@@ -38,13 +42,22 @@ def valid?
end
def test
- socket = Socket.new(AF_INET, SOCK_STREAM, 0)
- sockaddr = Socket.pack_sockaddr_in(self.port, self.addr)
- retval = socket.connect(sockaddr)
- close = socket.close()
- retval == 0 ? true : false
+ if self.family == 'tcp'
+ begin
+ s = TCPSocket.new(self.addr, self.port)
+ rescue SystemCallError
+ end
+ s.nil? ? false : true
+ elsif self.family == 'unix'
+ begin
+ s = UNIXSocket.new(self.path)
+ rescue SystemCallError
+ end
+ s.nil? ? false : true
+ else
+ false
+ end
end
end
-
end
end
View
43 test/test_conditions_socket_responding.rb
@@ -57,7 +57,7 @@ def test_socket_should_set_properties_for_tcp
c.socket = 'tcp:127.0.0.1:443'
assert_equal c.family, 'tcp'
assert_equal c.addr, '127.0.0.1'
- assert_equal c.port, '443'
+ assert_equal c.port, 443
# path should not be set for tcp sockets
assert_equal c.path, nil
end
@@ -68,24 +68,39 @@ def test_socket_should_set_properties_for_unix
assert_equal c.family, 'unix'
assert_equal c.path, '/tmp/process.sock'
# path should not be set for unix domain sockets
- assert_equal c.port, nil
+ assert_equal c.port, 0
end
# test
-# def test_test_should_return_true_if_socket_is_listening
-# c = Conditions::SocketResponding.new
-# c.port = 3000
+ def test_test_tcp_should_return_true_if_socket_is_listening
+ c = Conditions::SocketResponding.new
+
+ TCPSocket.expects(:new).returns(0)
+ assert_equal true, c.test
+ end
-# c.expects(:`).returns(0)
-# assert_equal true, c.test
-# end
+ def test_test_tcp_should_return_false_if_no_socket_is_listening
+ c = Conditions::SocketResponding.new
-# def test_test_should_return_false_if_no_socket_is_listening
-# c = Conditions::SocketResponding.new
-# c.port = 80
+ TCPSocket.expects(:new).returns(nil)
+ assert_equal false, c.test
+ end
+
+ def test_test_unix_should_return_true_if_socket_is_listening
+ c = Conditions::SocketResponding.new
+ c.socket = 'unix:/some/path'
-# c.expects(:`).returns(-1)
-# assert_equal false, c.test
-# end
+ UNIXSocket.expects(:new).returns(0)
+ assert_equal true, c.test
+ end
+
+ def test_test_unix_should_return_false_if_no_socket_is_listening
+
+ c = Conditions::SocketResponding.new
+ c.socket = 'unix:/some/path'
+
+ UNIXSocket.expects(:new).returns(nil)
+ assert_equal false, c.test
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.