From a8e354e0cafd455d291e071d7bb822e2863dd079 Mon Sep 17 00:00:00 2001 From: nick evans Date: Mon, 21 Aug 2023 14:35:52 -0400 Subject: [PATCH] =?UTF-8?q?=E2=8F=B1=EF=B8=8F=20Add=20Timeout=20to=20sever?= =?UTF-8?q?al=20existing=20SSL=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several tests are still using `imaps_test`. They might be converted to `with_fake_server` at some point, but a timeout is sufficient for now. --- test/net/imap/test_imap.rb | 70 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb index f794fde2..a3911d52 100644 --- a/test/net/imap/test_imap.rb +++ b/test/net/imap/test_imap.rb @@ -1054,46 +1054,48 @@ def with_fake_server(select: nil, timeout: 5, **opts) end end - def imaps_test - server = create_tcp_server - port = server.addr[1] - ctx = OpenSSL::SSL::SSLContext.new - ctx.ca_file = CA_FILE - ctx.key = File.open(SERVER_KEY) { |f| - OpenSSL::PKey::RSA.new(f) - } - ctx.cert = File.open(SERVER_CERT) { |f| - OpenSSL::X509::Certificate.new(f) - } - ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx) - started = false - ths = Thread.start do - Thread.current.report_on_exception = false # always join-ed - begin - started = true - sock = ssl_server.accept + def imaps_test(timeout: 10) + Timeout.timeout(timeout) do + server = create_tcp_server + port = server.addr[1] + ctx = OpenSSL::SSL::SSLContext.new + ctx.ca_file = CA_FILE + ctx.key = File.open(SERVER_KEY) { |f| + OpenSSL::PKey::RSA.new(f) + } + ctx.cert = File.open(SERVER_CERT) { |f| + OpenSSL::X509::Certificate.new(f) + } + ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx) + started = false + ths = Thread.start do + Thread.current.report_on_exception = false # always join-ed begin - sock.print("* OK test server\r\n") - sock.gets - sock.print("* BYE terminating connection\r\n") - sock.print("RUBY0001 OK LOGOUT completed\r\n") - ensure - sock.close + started = true + sock = ssl_server.accept + begin + sock.print("* OK test server\r\n") + sock.gets + sock.print("* BYE terminating connection\r\n") + sock.print("RUBY0001 OK LOGOUT completed\r\n") + ensure + sock.close + end + rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED end - rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED end - end - sleep 0.1 until started - begin + sleep 0.1 until started begin - imap = yield(port) - imap.logout + begin + imap = yield(port) + imap.logout + ensure + imap.disconnect if imap + end ensure - imap.disconnect if imap + ssl_server.close + ths.join end - ensure - ssl_server.close - ths.join end end