Skip to content

✅ Backport various test improvements to v0.4 #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions test/lib/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ def wait_for_response_count(imap, type:, count:,
sleep interval
end
end

# Copied from minitest
def assert_pattern
flunk "assert_pattern requires a block to capture errors." unless block_given?
assert_block do
yield
true
rescue NoMatchingPatternError => e
flunk e.message
end
end

end
6 changes: 5 additions & 1 deletion test/net/imap/fake_server/command_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "net/imap"

class Net::IMAP::FakeServer
CommandParseError = RuntimeError

class CommandReader
attr_reader :last_command
Expand All @@ -21,7 +22,10 @@ def get_command
$2 or socket.print "+ Continue\r\n"
buf << socket.read(Integer($1))
end
throw :eof if buf.empty?
@last_command = parse(buf)
rescue CommandParseError => err
raise IOError, err.message if socket.eof? && !buf.end_with?("\r\n")
end

private
Expand All @@ -31,7 +35,7 @@ def get_command
# TODO: convert bad command exception to tagged BAD response, when possible
def parse(buf)
/\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or
raise "bad request: %p" [buf]
raise CommandParseError, "bad request: %p" [buf]
case $2.upcase
when "LOGIN", "SELECT", "EXAMINE", "ENABLE", "AUTHENTICATE"
Command.new $1, $2, scan_astrings($3), buf
Expand Down
4 changes: 3 additions & 1 deletion test/net/imap/fake_server/command_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module Routable
def on(*command_names, &handler)
scope = self.is_a?(Module) ? self : singleton_class
command_names.each do |command_name|
scope.define_method("handle_#{command_name.downcase}", &handler)
method_name = :"handle_#{command_name.downcase}"
scope.undef_method(method_name) if scope.method_defined?(method_name)
scope.define_method(method_name, &handler)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/net/imap/fake_server/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def unsolicited(...) writer.untagged(...) end

def run
writer.greeting
router << reader.get_command until state.logout?
catch(:eof) do
router << reader.get_command until state.logout?
end
ensure
close
end
Expand Down
1 change: 1 addition & 0 deletions test/net/imap/fake_server/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(tcp_socket, config:)
def tls?; !!@tls_socket end
def closed?; @closed end

def eof?; socket.eof? end
def gets(...) socket.gets(...) end
def read(...) socket.read(...) end
def print(...) socket.print(...) end
Expand Down
23 changes: 20 additions & 3 deletions test/net/imap/fake_server/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@

module Net::IMAP::FakeServer::TestHelper

def run_fake_server_in_thread(ignore_io_error: false, timeout: 5, **opts)
IO_ERRORS = [
IOError,
EOFError,
Errno::ECONNABORTED,
Errno::ECONNRESET,
Errno::EPIPE,
Errno::ETIMEDOUT,
].freeze

def run_fake_server_in_thread(ignore_io_error: false, timeout: 5,
report_on_exception: true,
**opts)
Timeout.timeout(timeout) do
server = Net::IMAP::FakeServer.new(timeout: timeout, **opts)
@threads << Thread.new do
Thread.current.abort_on_exception = false
Thread.current.report_on_exception = report_on_exception
server.run
rescue IOError
rescue *IO_ERRORS
raise unless ignore_io_error
end
yield server
ensure
server&.shutdown
begin
server&.shutdown
rescue *IO_ERRORS
raise unless ignore_io_error
end
end
end

Expand Down
Loading