mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Switch from fibers to threads 💔
This commit is contained in:
parent
0e1ae0fe9f
commit
988b27c071
1 changed files with 64 additions and 20 deletions
|
@ -1,58 +1,102 @@
|
|||
# This is for super-high-level integration testing.
|
||||
|
||||
require 'fiber'
|
||||
require 'thread'
|
||||
|
||||
class ReplTester
|
||||
class Input
|
||||
def initialize(tester_mailbox)
|
||||
@tester_mailbox = tester_mailbox
|
||||
end
|
||||
|
||||
def readline(prompt)
|
||||
Fiber.yield(prompt)
|
||||
awaken_tester
|
||||
mailbox.pop
|
||||
end
|
||||
|
||||
def mailbox
|
||||
Thread.current[:mailbox]
|
||||
end
|
||||
|
||||
def awaken_tester
|
||||
@tester_mailbox.push nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.start(options = {}, &block)
|
||||
redirect_pry_io Input.new, StringIO.new do
|
||||
Thread.current[:mailbox] = Queue.new
|
||||
instance = nil
|
||||
|
||||
redirect_pry_io Input.new(Thread.current[:mailbox]), StringIO.new do
|
||||
instance = new(options)
|
||||
instance.instance_eval(&block)
|
||||
instance.ensure_exit
|
||||
end
|
||||
ensure
|
||||
if instance && instance.thread && instance.thread.alive?
|
||||
instance.thread.kill
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :pry, :repl, :fiber
|
||||
attr_accessor :thread, :mailbox
|
||||
|
||||
def initialize(options = {})
|
||||
@pry = Pry.new(options)
|
||||
@repl = Pry::REPL.new(@pry)
|
||||
@pry = Pry.new(options)
|
||||
@repl = Pry::REPL.new(@pry)
|
||||
@mailbox = Thread.current[:mailbox]
|
||||
|
||||
@fiber = Fiber.new do
|
||||
@repl.start
|
||||
@thread = Thread.new do
|
||||
begin
|
||||
Thread.current[:mailbox] = Queue.new
|
||||
@repl.start
|
||||
ensure
|
||||
Thread.current[:session_ended] = true
|
||||
mailbox.push nil
|
||||
end
|
||||
end
|
||||
|
||||
@fiber.resume
|
||||
mailbox.pop # wait until the instance reaches its first readline
|
||||
end
|
||||
|
||||
# Accept a line of input, as if entered by a user.
|
||||
def input(input)
|
||||
Pry.output.send(:initialize) # reset StringIO
|
||||
@fiber.resume(input)
|
||||
reset_output
|
||||
repl_mailbox.push input
|
||||
mailbox.pop # wait until the instance either calls readline or ends
|
||||
end
|
||||
|
||||
# Assert that the current prompt matches the given string or regex.
|
||||
def prompt(match)
|
||||
match.should === @pry.select_prompt
|
||||
end
|
||||
|
||||
# Assert that the most recent output (since the last time input was called)
|
||||
# matches the given string or regex.
|
||||
def output(match)
|
||||
match.should === Pry.output.string.chomp
|
||||
end
|
||||
|
||||
def ensure_exit
|
||||
if @should_exit_naturally
|
||||
fiber.should.not.be.alive
|
||||
else
|
||||
input "exit-all"
|
||||
raise "REPL didn't die" if fiber.alive?
|
||||
end
|
||||
end
|
||||
|
||||
# Assert that the Pry session ended naturally after the last input.
|
||||
def assert_exited
|
||||
@should_exit_naturally = true
|
||||
end
|
||||
|
||||
# @private
|
||||
def ensure_exit
|
||||
if @should_exit_naturally
|
||||
@thread[:session_ended].should.be.true
|
||||
else
|
||||
input "exit-all"
|
||||
raise "REPL didn't die" unless @thread[:session_ended]
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reset_output
|
||||
@pry.output = Pry.output = StringIO.new
|
||||
end
|
||||
|
||||
def repl_mailbox
|
||||
@thread[:mailbox]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue