Skip to content

Commit 7a37e97

Browse files
committed
⏱️ Add Timeout to several existing SSL tests
Several tests are still using `imaps_test`, which didn't have a timeout to protect against failing tests. They may or may not be converted to `with_fake_server` later, but adding a timeout is sufficient for now.
1 parent 92dabbb commit 7a37e97

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

test/net/imap/test_imap.rb

+36-34
Original file line numberDiff line numberDiff line change
@@ -1054,46 +1054,48 @@ def with_fake_server(select: nil, timeout: 5, **opts)
10541054
end
10551055
end
10561056

1057-
def imaps_test
1058-
server = create_tcp_server
1059-
port = server.addr[1]
1060-
ctx = OpenSSL::SSL::SSLContext.new
1061-
ctx.ca_file = CA_FILE
1062-
ctx.key = File.open(SERVER_KEY) { |f|
1063-
OpenSSL::PKey::RSA.new(f)
1064-
}
1065-
ctx.cert = File.open(SERVER_CERT) { |f|
1066-
OpenSSL::X509::Certificate.new(f)
1067-
}
1068-
ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
1069-
started = false
1070-
ths = Thread.start do
1071-
Thread.current.report_on_exception = false # always join-ed
1072-
begin
1073-
started = true
1074-
sock = ssl_server.accept
1057+
def imaps_test(timeout: 10)
1058+
Timeout.timeout(timeout) do
1059+
server = create_tcp_server
1060+
port = server.addr[1]
1061+
ctx = OpenSSL::SSL::SSLContext.new
1062+
ctx.ca_file = CA_FILE
1063+
ctx.key = File.open(SERVER_KEY) { |f|
1064+
OpenSSL::PKey::RSA.new(f)
1065+
}
1066+
ctx.cert = File.open(SERVER_CERT) { |f|
1067+
OpenSSL::X509::Certificate.new(f)
1068+
}
1069+
ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
1070+
started = false
1071+
ths = Thread.start do
1072+
Thread.current.report_on_exception = false # always join-ed
10751073
begin
1076-
sock.print("* OK test server\r\n")
1077-
sock.gets
1078-
sock.print("* BYE terminating connection\r\n")
1079-
sock.print("RUBY0001 OK LOGOUT completed\r\n")
1080-
ensure
1081-
sock.close
1074+
started = true
1075+
sock = ssl_server.accept
1076+
begin
1077+
sock.print("* OK test server\r\n")
1078+
sock.gets
1079+
sock.print("* BYE terminating connection\r\n")
1080+
sock.print("RUBY0001 OK LOGOUT completed\r\n")
1081+
ensure
1082+
sock.close
1083+
end
1084+
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED
10821085
end
1083-
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED
10841086
end
1085-
end
1086-
sleep 0.1 until started
1087-
begin
1087+
sleep 0.1 until started
10881088
begin
1089-
imap = yield(port)
1090-
imap.logout
1089+
begin
1090+
imap = yield(port)
1091+
imap.logout
1092+
ensure
1093+
imap.disconnect if imap
1094+
end
10911095
ensure
1092-
imap.disconnect if imap
1096+
ssl_server.close
1097+
ths.join
10931098
end
1094-
ensure
1095-
ssl_server.close
1096-
ths.join
10971099
end
10981100
end
10991101

0 commit comments

Comments
 (0)