|
1 | 1 | module ForkableExample
|
2 | 2 | def finish(reporter)
|
3 | 3 | # TODO: better name than execute_in_fork?
|
4 |
| - if @metadata[:execute_in_fork] |
| 4 | + if @metadata[:execute_in_fork] && Process.ppid != 1 |
5 | 5 | super ? exit(0) : exit(1)
|
6 | 6 | else
|
7 | 7 | super
|
8 | 8 | end
|
9 | 9 | end
|
10 | 10 |
|
| 11 | + EXAMPLE_SENTINEL = Module.new |
| 12 | + MODULE_VALUE = Struct.new(:name) |
| 13 | + |
11 | 14 | def run(example_group_instance, reporter)
|
12 | 15 | if @metadata[:execute_in_fork]
|
| 16 | + reader, writer = IO.pipe |
| 17 | + |
| 18 | + reader.binmode |
| 19 | + writer.binmode |
| 20 | + |
13 | 21 | pid = fork do
|
| 22 | + reporter.singleton_class.prepend(ForkableExample::Reporter) |
| 23 | + execution_result.singleton_class.prepend(ForkableExample::ExecutionResult) |
| 24 | + execution_result.instance_variable_set(:@fork_writer_pipe, writer) |
| 25 | + reporter.instance_variable_set(:@fork_writer_pipe, writer) |
| 26 | + |
| 27 | + reporter.instance_variable_set(:@fork_example_sentinel, self) |
| 28 | + |
| 29 | + reader.close |
| 30 | + |
14 | 31 | super
|
| 32 | + |
| 33 | + # writer.puts "Hello from child process" |
| 34 | + writer.close |
15 | 35 | end
|
16 | 36 |
|
| 37 | + writer.close |
| 38 | + |
17 | 39 | _, status = Process.wait2(pid)
|
| 40 | + |
| 41 | + while (read_size = reader.gets) |
| 42 | + call = Marshal.load(reader.read(Integer(read_size))) |
| 43 | + |
| 44 | + args = call[:args] |
| 45 | + args.map! do |arg| |
| 46 | + if arg == EXAMPLE_SENTINEL |
| 47 | + self |
| 48 | + elsif arg.is_a?(MODULE_VALUE) |
| 49 | + ::Object.const_get(arg.name) |
| 50 | + elsif arg.is_a?(RSpec::Core::Notifications::ExampleNotification) && arg.example == EXAMPLE_SENTINEL |
| 51 | + ::RSpec::Core::Notifications::ExampleNotification.for(self) |
| 52 | + else |
| 53 | + arg |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # puts("call: #{call}") |
| 58 | + |
| 59 | + if call[:receiver] == :reporter |
| 60 | + reporter.send(call[:method], *args) |
| 61 | + elsif call[:receiver] == :execution_result |
| 62 | + execution_result.send(call[:method], *args) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + reader.close |
| 67 | + |
18 | 68 | status.success?
|
19 | 69 | else
|
20 | 70 | super
|
21 | 71 | end
|
22 | 72 | end
|
| 73 | + |
| 74 | + module Reporter |
| 75 | + |
| 76 | + |
| 77 | + # PUBLIC_INTERNAL_METHODS = [:start, :stop, :publish, :finish, :close_after, :notify, :fail_fast_limit_met?] |
| 78 | + RSpec::Core::Reporter.instance_methods(false).each do |method| |
| 79 | + define_method(method) do |*args| |
| 80 | + original_args = args |
| 81 | + |
| 82 | + |
| 83 | + unless @fork_instrumenting # Nested calls are skipped, since they will invoked by the first call site |
| 84 | + begin |
| 85 | + @fork_instrumenting = true |
| 86 | + |
| 87 | + args = args.map do |arg| |
| 88 | + if arg == @fork_example_sentinel |
| 89 | + EXAMPLE_SENTINEL |
| 90 | + elsif arg.is_a?(Module) |
| 91 | + MODULE_VALUE.new(arg.name) |
| 92 | + elsif arg.is_a?(::RSpec::Core::Notifications::ExampleNotification) && arg.example == @fork_example_sentinel |
| 93 | + ::RSpec::Core::Notifications::ExampleNotification.send(:new, EXAMPLE_SENTINEL) |
| 94 | + else |
| 95 | + arg |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + # puts({ method: method, args: args }) |
| 100 | + |
| 101 | + dump = Marshal.dump({ receiver: :reporter, method: method, args: args }) |
| 102 | + # Write a header hex int with the size of the next object dump |
| 103 | + @fork_writer_pipe.write(sprintf("0x%x\n", dump.bytesize)) |
| 104 | + @fork_writer_pipe.write(dump) |
| 105 | + |
| 106 | + # puts({ method: method, args: args }) |
| 107 | + rescue => e |
| 108 | + puts "ERRORERRORERROR 1111" |
| 109 | + puts args |
| 110 | + puts e |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + super(*original_args) |
| 115 | + ensure |
| 116 | + @fork_instrumenting = false |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + module ExecutionResult |
| 122 | + ::RSpec::Core::Example::ExecutionResult.instance_methods(false).reject{|m|m.end_with?('?')}.each do |method| |
| 123 | + define_method(method) do |*args| |
| 124 | + original_args = args |
| 125 | + |
| 126 | + unless @fork_instrumenting # Nested calls are skipped, since they will invoked by the first call site |
| 127 | + begin |
| 128 | + @fork_instrumenting = true |
| 129 | + |
| 130 | + # args = args.map do |arg| |
| 131 | + # if arg == @fork_example_sentinel |
| 132 | + # EXAMPLE_SENTINEL |
| 133 | + # elsif arg.is_a?(Module) |
| 134 | + # MODULE_VALUE.new(arg.name) |
| 135 | + # else |
| 136 | + # arg |
| 137 | + # end |
| 138 | + # end |
| 139 | + |
| 140 | + # puts({ method: method, args: args }) |
| 141 | + |
| 142 | + dump = Marshal.dump({ receiver: :execution_result, method: method, args: args }) |
| 143 | + # Write a header hex int with the size of the next object dump |
| 144 | + @fork_writer_pipe.write(sprintf("0x%x\n", dump.bytesize)) |
| 145 | + @fork_writer_pipe.write(dump) |
| 146 | + |
| 147 | + # puts({ method: method, args: args }) |
| 148 | + rescue => e |
| 149 | + puts "ERRORERRORERROR 2222" |
| 150 | + puts args |
| 151 | + puts e |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + super(*original_args) |
| 156 | + ensure |
| 157 | + @fork_instrumenting = false |
| 158 | + end |
| 159 | + end |
| 160 | + end |
23 | 161 | end
|
24 | 162 |
|
25 | 163 | RSpec::Core::Example.prepend(ForkableExample)
|
0 commit comments