Skip to content

Commit

Permalink
Fix bug that test framework gets stuck when debuggee outputs many lines
Browse files Browse the repository at this point in the history
Fixes #318
  • Loading branch information
ono-max committed Oct 31, 2021
1 parent dfe4d62 commit 01170b8
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions test/support/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ def create_message fail_msg, test_info

debuggee_msg =
if test_info.mode != 'LOCAL'
debuggee_backlog = collect_debuggee_backlog(test_info)

<<~DEBUGGEE_MSG.chomp
--------------------
| Debuggee Session |
--------------------
> #{debuggee_backlog.join('> ')}
> #{test_info.remote_info.debuggee_backlog.join('> ')}
DEBUGGEE_MSG
end

Expand All @@ -52,26 +50,10 @@ def create_message fail_msg, test_info
MSG
end

def collect_debuggee_backlog test_info
backlog = []

begin
Timeout.timeout(TIMEOUT_SEC) do
while (line = test_info.remote_info.r.gets)
backlog << line
end
end
rescue Timeout::Error, Errno::EIO
# result of `gets` return Errno::EIO in some platform
# https://github.com/ruby/ruby/blob/master/ext/pty/pty.c#L729-L736
end
backlog
end

TestInfo = Struct.new(:queue, :mode, :prompt_pattern, :remote_info,
:backlog, :last_backlog, :internal_info)

RemoteInfo = Struct.new(:r, :w, :pid, :sock_path, :port)
RemoteInfo = Struct.new(:r, :w, :pid, :sock_path, :port, :reader_thread, :debuggee_backlog)

MULTITHREADED_TEST = !(%w[1 true].include? ENV['RUBY_DEBUG_TEST_DISABLE_THREADS'])

Expand Down Expand Up @@ -316,6 +298,8 @@ def check_error(error, test_info)
def kill_remote_debuggee remote_info
return unless remote_info

remote_info.reader_thread.raise Terminate
remote_info.reader_thread.join
remote_info.r.close
remote_info.w.close
kill_safely remote_info.pid, :remote
Expand All @@ -336,9 +320,20 @@ def manual_debug_code(program)
kill_remote_debuggee remote_info
end

class Terminate < StandardError
end

def setup_remote_debuggee(cmd)
remote_info = RemoteInfo.new(*PTY.spawn(cmd))
remote_info.r.read(1) # wait for the remote server to boot up

remote_info.reader_thread = Thread.new(remote_info.r) do |r|
while data = r.gets
remote_info.debuggee_backlog << data
end
rescue Terminate, Errno::EIO
end
remote_info.debuggee_backlog = []
remote_info
end

Expand Down

0 comments on commit 01170b8

Please sign in to comment.