summaryrefslogtreecommitdiff
path: root/spec/ffi/spec_helper.rb
blob: a0adbed4962f1b55029f94c97f26aba2770cd10f (plain)
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
#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

require_relative 'fixtures/compile'
require 'timeout'
require 'objspace'

RSpec.configure do |c|
  c.filter_run_excluding gc_dependent: true unless ENV['FFI_TEST_GC'] == 'true'
end

module TestLibrary
  def self.force_gc
    if RUBY_ENGINE == 'jruby'
      java.lang.System.gc
    elsif RUBY_ENGINE == 'rbx'
      GC.run(true)
    else
      GC.start
    end
  end
end

module LibTest
  extend FFI::Library
  ffi_lib TestLibrary::PATH
end

def external_run(cmd, rb_file, options: [], timeout: 10)
  path = File.join(File.dirname(__FILE__), rb_file)
  log = "#{path}.log"
  pid = spawn(cmd, "-Ilib", path, { [:out, :err] => log })
  begin
    Timeout.timeout(timeout){ Process.wait(pid) }
  rescue Timeout::Error
    Process.kill(9, pid)
    raise
  else
    if $?.exitstatus != 0
      raise "external process failed:\n#{ File.read(log) }"
    end
  end
  File.read(log)
end

module OrderHelper
  case FFI::Platform::BYTE_ORDER
  when FFI::Platform::LITTLE_ENDIAN
    ORDER = :little
    OTHER_ORDER = :big
  when FFI::Platform::BIG_ENDIAN
    ORDER = :big
    OTHER_ORDER = :little
  else
    raise
  end
end