|
5 | 5 | # SPDX-License-Identifier: Apache-2.0
|
6 | 6 |
|
7 | 7 | require 'test_helper'
|
8 |
| -require 'opentelemetry-exporter-otlp-metrics' unless RUBY_ENGINE == 'jruby' |
| 8 | +require 'json' |
9 | 9 |
|
10 | 10 | describe OpenTelemetry::SDK::Metrics::ForkHooks do
|
11 | 11 | def fork_with_fork_hooks(before_fork_lambda, after_fork_lambda)
|
12 | 12 | with_pipe do |inner_read_io, inner_write_io|
|
13 | 13 | child_pid = fork do # fork twice to avoid prepending fork in the parent process
|
14 | 14 | setup_fork_hooks(before_fork_lambda, after_fork_lambda) do
|
15 | 15 | grandchild_pid = fork {}
|
16 |
| - Process.waitpid(grandchild_pid) |
17 |
| - inner_write_io.puts grandchild_pid |
| 16 | + Timeout.timeout(5) { Process.waitpid(grandchild_pid) } |
| 17 | + message = { 'child_pid' => Process.pid, 'grandchild_pid' => grandchild_pid }.to_json |
| 18 | + inner_write_io.puts message |
| 19 | + rescue StandardError => e |
| 20 | + message = { 'error' => e.message }.to_json |
| 21 | + inner_write_io.puts message |
18 | 22 | end
|
19 | 23 | end
|
20 |
| - Process.waitpid(child_pid) |
21 |
| - grandchild_pid = inner_read_io.gets.chomp.to_i |
| 24 | + Timeout.timeout(10) { Process.waitpid(child_pid) } |
| 25 | + received_from_child = JSON.parse(inner_read_io.gets.chomp) |
| 26 | + refute_includes(received_from_child, 'error') |
| 27 | + grandchild_pid = received_from_child['grandchild_pid'] |
22 | 28 | refute_equal(child_pid, Process.pid)
|
23 | 29 | refute_equal(child_pid, grandchild_pid)
|
24 | 30 | [child_pid, grandchild_pid]
|
@@ -59,11 +65,12 @@ def with_pipe
|
59 | 65 | with_pipe do |after_fork_read_io, after_fork_write_io|
|
60 | 66 | before_fork_lambda = proc {}
|
61 | 67 | after_fork_lambda = proc do
|
62 |
| - after_fork_write_io.puts Process.pid |
| 68 | + message = { 'after_fork_pid' => Process.pid }.to_json |
| 69 | + after_fork_write_io.puts message |
63 | 70 | end
|
64 | 71 |
|
65 |
| - forking_pid, forked_pid = fork_with_fork_hooks(before_fork_lambda, after_fork_lambda) |
66 |
| - pid_from_after_fork = after_fork_read_io.gets.chomp.to_i |
| 72 | + forking_pid, forked_pid = fork_with_fork_hooks(after_fork_lambda) |
| 73 | + pid_from_after_fork = JSON.parse(after_fork_read_io.gets.chomp)['after_fork_pid'].to_i |
67 | 74 |
|
68 | 75 | refute_equal(pid_from_after_fork, Process.pid)
|
69 | 76 | refute_equal(pid_from_after_fork, forking_pid)
|
|
0 commit comments