File: remote_debugging_tests.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,300 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require "test_helper"
require "open3"

module Byebug
  #
  # Tests remote debugging functionality.
  #
  module RemoteDebuggingTests
    def test_connecting_to_remote_debugger
      write_program(program)

      remote_debug_and_connect("quit!")

      check_output_includes \
        "Connecting to byebug server at 127.0.0.1:8989...",
        "Connected."
    end

    def test_interacting_with_remote_debugger
      write_program(program)

      remote_debug_and_connect("cont 9", "cont")

      check_output_includes \
        "7:   class ByebugExampleClass",
        "8:     def a",
        "=>  9:       3",
        "10:     end"
    end

    def test_interrupting_client_doesnt_abort_server
      write_program(program)

      status = remote_debug_connect_and_interrupt("cont")

      assert_equal true, status.success?
    end

    def test_ignoring_main_server_and_control_threads
      write_program(program)

      remote_debug_and_connect("thread list", "cont")

      check_output_includes \
        %r{!.*/byebug/remote/server.rb},
        %r{!.*/byebug/remote/server.rb}
    end

    private

    def write_program(code)
      example_file.write(code)
      example_file.close
    end

    def remote_debug_and_connect(*commands)
      remote_debug(*commands) do
        launch_client

        wait_for_client_startup
      end
    end

    def remote_debug_connect_and_interrupt(*commands)
      remote_debug(*commands) do
        th = Thread.new { launch_client }

        wait_for_client_startup

        th.kill
      end
    end

    def remote_debug(*commands)
      enter(*commands)

      Open3.popen2e(shell_out_env, "ruby #{example_path}") do |_i, oe, wait_thr|
        outerr_thr = Thread.new { oe.read }

        yield

        exit_status = wait_thr.value

        print outerr_thr.value unless exit_status.success?

        exit_status
      end
    end

    def wait_for_client_startup
      sleep 0.1 until mutex.synchronize { client.started? }
    end

    def launch_client
      mutex.synchronize { client.start("127.0.0.1") }
    rescue Errno::ECONNREFUSED
      sleep 0.1
      retry
    end

    def mutex
      @mutex ||= Mutex.new
    end

    def client
      @client ||= Remote::Client.new(Context.interface)
    end
  end
end