Skip to content

Commit 12df724

Browse files
committed
Make code clearer and more explicit in SocketSpecs
* Remove the rescue-all SocketSpecs.wait_until_success and simplify SocketSpecs.loop_with_timeout.
1 parent c9b78e7 commit 12df724

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

spec/ruby/library/socket/fixtures/classes.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,11 @@ def self.each_ip_protocol
7272
end
7373

7474
def self.loop_with_timeout(timeout = TIME_TOLERANCE)
75-
require 'timeout'
76-
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
75+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
7776

78-
loop do
79-
if Process.clock_gettime(Process::CLOCK_MONOTONIC) - time >= timeout
80-
raise TimeoutError, "Did not succeed within #{timeout} seconds"
81-
end
82-
83-
sleep 0.01 # necessary on OSX; don't know why
84-
yield
85-
end
86-
end
87-
88-
def self.wait_until_success(timeout = TIME_TOLERANCE)
89-
loop_with_timeout(timeout) do
90-
begin
91-
return yield
92-
rescue
77+
while yield == :retry
78+
if Process.clock_gettime(Process::CLOCK_MONOTONIC) - start >= timeout
79+
raise RuntimeError, "Did not succeed within #{timeout} seconds"
9380
end
9481
end
9582
end

spec/ruby/library/socket/socket/tcp_server_loop_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
end
3232
end
3333

34-
SocketSpecs.wait_until_success do
35-
@client.connect(Socket.sockaddr_in(@port, '127.0.0.1'))
34+
SocketSpecs.loop_with_timeout do
35+
begin
36+
@client.connect(Socket.sockaddr_in(@port, '127.0.0.1'))
37+
rescue SystemCallError
38+
sleep 0.01
39+
:retry
40+
end
3641
end
3742

3843
# At this point the connection has been set up but the thread may not yet

spec/ruby/library/socket/socket/udp_server_loop_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@
3535
@client.connect(Socket.sockaddr_in(@port, '127.0.0.1'))
3636

3737
SocketSpecs.loop_with_timeout do
38-
SocketSpecs.wait_until_success { @client.write('hello') }
39-
40-
break if msg
38+
begin
39+
@client.write('hello')
40+
rescue SystemCallError
41+
sleep 0.01
42+
:retry
43+
else
44+
unless msg
45+
sleep 0.001
46+
:retry
47+
end
48+
end
4149
end
4250

4351
msg.should == 'hello'

spec/ruby/library/socket/socket/unix_server_loop_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@
3939
end
4040
end
4141

42-
@client = SocketSpecs.wait_until_success { Socket.unix(@path) }
42+
SocketSpecs.loop_with_timeout do
43+
begin
44+
@client = Socket.unix(@path)
45+
rescue SystemCallError
46+
sleep 0.01
47+
:retry
48+
end
49+
end
4350

4451
thread.join(2)
4552

0 commit comments

Comments
 (0)