Skip to content
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

Do a better job of cleaning up keypress listener thread #10

Merged
merged 1 commit into from
Feb 22, 2024
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
19 changes: 9 additions & 10 deletions lib/mighty_test/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ def run(iterations: :indefinitely) # rubocop:disable Metrics/MethodLength
end
ensure
puts "\nExiting."
listener&.stop
file_system_listener&.stop
keypress_listener&.kill
end

private

attr_reader :console, :extra_args, :file_system, :listener, :system_proc
attr_reader :console, :extra_args, :file_system, :file_system_listener, :keypress_listener, :system_proc

def run_all_tests
console.clear
Expand Down Expand Up @@ -81,26 +82,24 @@ def mt(*test_paths)
end

def start_file_system_listener
listener.stop if listener && !listener.stopped?
file_system_listener.stop if file_system_listener && !file_system_listener.stopped?

@listener = file_system.listen do |modified, added, _removed|
@file_system_listener = file_system.listen do |modified, added, _removed|
# Pause listener so that subsequent changes are queued up while we are running the tests
listener.pause unless listener.stopped?
file_system_listener.pause unless file_system_listener.stopped?
post_event(:file_system_changed, [*modified, *added].uniq)
end
end
alias restart_file_system_listener start_file_system_listener

def start_keypress_listener
Thread.new do
@keypress_listener = Thread.new do
loop do
key = console.wait_for_keypress
post_event(:keypress, key)
post_event(:keypress, key) if key
rescue Interrupt
retry
end
rescue StandardError
# ignore
end
end

Expand All @@ -109,7 +108,7 @@ def loop_for(iterations, &)
end

def await_next_event
listener.start if listener.paused?
file_system_listener.start if file_system_listener.paused?
@event.take
end

Expand Down
2 changes: 1 addition & 1 deletion test/mighty_test/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def paused?

def run_watcher(iterations: :indefinitely, in: ".", extra_args: [], stdin: nil, file_system: FileSystem.new)
listen_thread = @listen_thread
console = Console.new(stdin: stdin.nil? ? File::NULL : StringIO.new(stdin))
console = Console.new(stdin: stdin.nil? ? StringIO.new("") : StringIO.new(stdin))
console.define_singleton_method(:clear) { puts "[CLEAR]" }
console.define_singleton_method(:play_sound) { |sound| puts "[SOUND] #{sound.inspect}" }
file_system.define_singleton_method(:listen) { |&callback| Listener.new(listen_thread, callback) }
Expand Down