Best Spork_ruby code snippet using Spork.initialize
ruby-debug.rb
Source:ruby-debug.rb
...5class SporkDebugger6 DEFAULT_PORT = 10_1237 HOST = '127.0.0.1'8 extend Forwardable9 def_delegators :state, *[:prepare_debugger, :initialize]10 attr_reader :state11 class << self12 attr_reader :instance13 def run14 @instance = new15 end16 end17 def initialize18 @state = SporkDebugger::PreloadState.new19 Spork.send(:each_run_procs).unshift(lambda do20 @state = @state.transition_to_each_run_state21 end)22 end23 module NetworkHelpers24 def find_port(starting_with)25 port = starting_with26 begin27 server = TCPServer.new(HOST, port)28 server.close29 rescue Errno::EADDRINUSE30 port += 131 retry32 end33 port34 end35 end36 class PreloadState37 include NetworkHelpers38 def initialize39 install_hook40 listen_for_connection_signals41 end42 def finish43 @tcp_service.close; @tcp_service = nil;44 end45 def transition_to_each_run_state46 finish47 SporkDebugger::EachRunState.new(@port)48 end49 protected50 def install_hook51 Kernel.class_eval do52 alias :debugger_without_spork_hook :debugger53 def debugger(steps = 1)54 SporkDebugger.instance.prepare_debugger unless @debugger_invoked55 @debugger_invoked = true56 debugger_without_spork_hook57 end58 end59 end60 def listen_for_connection_signals61 @port = SporkDebugger::DEFAULT_PORT62 begin63 @tcp_service = TCPServer.new(SporkDebugger::HOST, @port)64 rescue Errno::EADDRINUSE65 @port += 166 retry67 end68 Thread.new { main_loop }69 end70 def main_loop71 while @tcp_service do72 socket = @tcp_service.accept73 port = Marshal.load(socket)74 Marshal.dump(true, socket)75 connect_rdebug_client(port)76 socket.close77 end78 rescue => e79 puts "error: #{e.class} - #{e}"80 end81 def connect_rdebug_client(port = Debugger::PORT)82 puts "\n\n - Debug Session Started - \n\n\n"83 begin84 Debugger.start_client(SporkDebugger::HOST, port)85 rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED86 end87 puts "\n\n - Debug Session Terminated - \n\n\n"88 end89 end90 class EachRunState91 include NetworkHelpers92 def initialize(connection_request_port)93 @connection_request_port = connection_request_port94 end95 def prepare_debugger96 port, cport = start_rdebug_server97 signal_spork_server_to_connect_to_rdebug_server(port)98 wait_for_connection99 puts "\n\n - breakpoint - see your spork server for the debug terminal - \n\n"100 end101 def signal_spork_server_to_connect_to_rdebug_server(rdebug_server_port)102 socket = TCPSocket.new(SporkDebugger::HOST, @connection_request_port)103 Marshal.dump(rdebug_server_port, socket)104 response = Marshal.load(socket)105 socket.close106 end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!