File: test_conditions_socket_responding.rb

package info (click to toggle)
ruby-god 0.13.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 832 kB
  • sloc: ruby: 6,641; ansic: 237; makefile: 3
file content (176 lines) | stat: -rw-r--r-- 4,694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require File.dirname(__FILE__) + '/helper'

class TestConditionsSocketResponding < Minitest::Test
  # valid?

  def test_valid_should_return_false_if_no_options_set
    c = Conditions::SocketResponding.new
    c.watch = stub(:name => 'foo')
    assert_equal false, c.valid?

  end

  def test_valid_should_return_true_if_required_options_set_for_default
    c = Conditions::SocketResponding.new
    c.port = 443
    assert_equal true, c.valid?
  end

  def test_valid_should_return_true_if_required_options_set_for_tcp
    c = Conditions::SocketResponding.new
    c.family = 'tcp'
    c.port = 443
    assert_equal true, c.valid?
  end

  def test_valid_should_return_true_if_required_options_set_for_unix
    c = Conditions::SocketResponding.new
    c.path = 'some-path'
    c.family = 'unix'
    assert_equal true, c.valid?
  end

  def test_valid_should_return_true_if_family_is_tcp
    c = Conditions::SocketResponding.new
    c.port = 443
    c.family = 'tcp'
    assert_equal true, c.valid?
  end

  def test_valid_should_return_true_if_family_is_unix
    c = Conditions::SocketResponding.new
    c.path = 'some-path'
    c.family = 'unix'
    c.watch = stub(:name => 'foo')
    assert_equal true, c.valid?
  end

  # socket method
  def test_socket_should_return_127_0_0_1_for_default_addr
    c = Conditions::SocketResponding.new
    c.socket = 'tcp:443'
    assert_equal c.addr, '127.0.0.1'
  end

  def test_socket_should_set_properties_for_tcp
    c = Conditions::SocketResponding.new
    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.responding, false
    # path should not be set for tcp sockets
    assert_equal c.path, nil
  end

  def test_socket_should_set_properties_for_unix
    c = Conditions::SocketResponding.new
    c.socket = 'unix:/tmp/process.sock'
    assert_equal c.family, 'unix'
    assert_equal c.path, '/tmp/process.sock'
    assert_equal c.responding, false
    # path should not be set for unix domain sockets
    assert_equal c.port, 0
  end

  # test : responding = false

  def test_test_tcp_should_return_false_if_socket_is_listening
    c = Conditions::SocketResponding.new
    c.prepare

    TCPSocket.expects(:new).returns(0)
    assert_equal false, c.test
  end

  def test_test_tcp_should_return_true_if_no_socket_is_listening
    c = Conditions::SocketResponding.new
    c.prepare

    TCPSocket.expects(:new).returns(nil)
    assert_equal true, c.test
  end

  def test_test_unix_should_return_false_if_socket_is_listening
    c = Conditions::SocketResponding.new
    c.socket = 'unix:/some/path'

    c.prepare
    UNIXSocket.expects(:new).returns(0)
    assert_equal false, c.test
  end

  def test_test_unix_should_return_true_if_no_socket_is_listening

    c = Conditions::SocketResponding.new
    c.socket = 'unix:/some/path'
    c.prepare

    UNIXSocket.expects(:new).returns(nil)
    assert_equal true, c.test
  end

  def test_test_unix_should_return_true_if_socket_is_listening_2_times

    c = Conditions::SocketResponding.new
    c.socket = 'unix:/some/path'
    c.times = [2, 2]
    c.prepare

    UNIXSocket.expects(:new).returns(nil).times(2)
    assert_equal false, c.test
    assert_equal true, c.test
  end

  # test : responding = true

  def test_test_tcp_should_return_true_if_socket_is_listening_with_responding_true
    c = Conditions::SocketResponding.new
    c.responding = true
    c.prepare

    TCPSocket.expects(:new).returns(0)
    assert_equal true, c.test
  end

  def test_test_tcp_should_return_false_if_no_socket_is_listening_with_responding_true
    c = Conditions::SocketResponding.new
    c.responding = true
    c.prepare

    TCPSocket.expects(:new).returns(nil)
    assert_equal false, c.test
  end

  def test_test_unix_should_return_true_if_socket_is_listening_with_responding_true
    c = Conditions::SocketResponding.new
    c.responding = true
    c.socket = 'unix:/some/path'

    c.prepare
    UNIXSocket.expects(:new).returns(0)
    assert_equal true, c.test
  end

  def test_test_unix_should_return_false_if_no_socket_is_listening_with_responding_true
    c = Conditions::SocketResponding.new
    c.socket = 'unix:/some/path'
    c.responding = true
    c.prepare

    UNIXSocket.expects(:new).returns(nil)
    assert_equal false, c.test
  end

  def test_test_unix_should_return_false_if_socket_is_listening_2_times_with_responding_true
    c = Conditions::SocketResponding.new
    c.socket = 'unix:/some/path'
    c.responding = true
    c.times = [2, 2]
    c.prepare

    UNIXSocket.expects(:new).returns(nil).times(2)
    assert_equal false, c.test
    assert_equal false, c.test
  end
end