Skip to content

Commit

Permalink
Add unique token to separated runner
Browse files Browse the repository at this point in the history
Same as Test::Unit::CoreAssertions#assert_no_memory_leak.
  • Loading branch information
nobu committed Sep 13, 2021
1 parent dbdceb8 commit aaa9805
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions tool/lib/core_assertions.rb
Expand Up @@ -105,9 +105,7 @@ def assert_no_memory_leak(args, prepare, code, message=nil, limit: 2.0, rss: fal
require_relative 'memory_status'
raise Test::Unit::PendedError, "unsupported platform" unless defined?(Memory::Status)

token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
token_dump = token.dump
token_re = Regexp.quote(token)
token_dump, token_re = new_test_token
envs = args.shift if Array === args and Hash === args.first
args = [
"--disable=gems",
Expand Down Expand Up @@ -244,11 +242,11 @@ def assert_ruby_status(args, test_stdin="", message=nil, **opt)

ABORT_SIGNALS = Signal.list.values_at(*%w"ILL ABRT BUS SEGV TERM")

def separated_runner(out = nil)
def separated_runner(token, out = nil)
include(*Test::Unit::TestCase.ancestors.select {|c| !c.is_a?(Class) })
out = out ? IO.new(out, 'w') : STDOUT
at_exit {
out.puts [Marshal.dump($!)].pack('m'), "assertions=#{self._assertions}"
out.puts "#{token}<error>", [Marshal.dump($!)].pack('m'), "#{token}</error>", "#{token}assertions=#{self._assertions}"
}
Test::Unit::Runner.class_variable_set(:@@stop_auto_run, true) if defined?(Test::Unit::Runner)
end
Expand All @@ -266,11 +264,12 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
res_p, res_c = IO.pipe
opt[:ios] = [res_c]
end
token_dump, token_re = new_test_token
src = <<eom
# -*- coding: #{line += __LINE__; src.encoding}; -*-
BEGIN {
require "test/unit";include Test::Unit::Assertions;include Test::Unit::CoreAssertions;require #{__FILE__.dump}
separated_runner #{res_c&.fileno}
separated_runner #{token_dump}, #{res_c&.fileno || 'nil'}
}
#{line -= __LINE__; src}
eom
Expand All @@ -288,9 +287,9 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
raise if $!
abort = status.coredump? || (status.signaled? && ABORT_SIGNALS.include?(status.termsig))
assert(!abort, FailDesc[status, nil, stderr])
self._assertions += res[/^assertions=(\d+)/, 1].to_i
self._assertions += res[/^#{token_re}assertions=(\d+)/, 1].to_i
begin
res = Marshal.load(res.unpack1("m"))
res = Marshal.load(res[/^#{token_re}<error>\n\K.*\n(?=#{token_re}<\/error>$)/m].unpack1("m"))
rescue => marshal_error
ignore_stderr = nil
res = nil
Expand Down Expand Up @@ -763,6 +762,11 @@ def diff(exp, act)
end
q.output
end

def new_test_token
token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
return token.dump, Regexp.quote(token)
end
end
end
end

0 comments on commit aaa9805

Please sign in to comment.