Skip to content

Commit

Permalink
⏱️ Add Timeout to several existing SSL tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nevans committed Aug 28, 2023
1 parent 92dabbb commit 7a37e97
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7a37e97

Please sign in to comment.