Sha256: 8e30526868f562beb99e7be8d44aaac209d7066795c1b061034e3e37d31085f1

Contents?: true

Size: 1.54 KB

Versions: 25

Compression:

Stored size: 1.54 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
require 'java' if RUBY_PLATFORM =~ /java/

describe "Managed Struct" do
  include FFI
  module LibTest
    extend FFI::Library
    ffi_lib TestLibrary::PATH
    attach_function :ptr_from_address, [ FFI::Platform::ADDRESS_SIZE == 32 ? :uint : :ulong_long ], :pointer
  end
  it "should raise an error if release() is not defined" do
    class NoRelease < FFI::ManagedStruct ; layout :i, :int; end
    lambda { NoRelease.new(LibTest.ptr_from_address(0x12345678)) }.should raise_error(NoMethodError)
  end

  it "should be the right class" do
    class WhatClassAmI < FFI::ManagedStruct
      layout :i, :int
      def self.release
      end
    end    

    WhatClassAmI.new(LibTest.ptr_from_address(0x12345678)).class.should == WhatClassAmI
  end

  it "should release memory properly" do
    class PleaseReleaseMe < FFI::ManagedStruct
      layout :i, :int
      @@count = 0
      def self.release
        @@count += 1
      end
      def self.wait_gc(count)
        loop = 5
        while loop > 0 && @@count < count
          loop -= 1
          if RUBY_PLATFORM =~ /java/
            java.lang.System.gc
          else
            GC.start
          end
          sleep 0.05 if @@count < count
        end
      end
    end

    loop_count = 30
    wiggle_room = 5

    PleaseReleaseMe.should_receive(:release).at_least(loop_count-wiggle_room).times
    loop_count.times do
      s = PleaseReleaseMe.new(LibTest.ptr_from_address(0x12345678))
    end
    PleaseReleaseMe.wait_gc loop_count
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
ffi-0.6.4 spec/ffi/managed_struct_spec.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/spec/ffi/managed_struct_spec.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/spec/ffi/managed_struct_spec.rb
ffi-1.0.4-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.0.4 spec/ffi/managed_struct_spec.rb
ffi-1.0.3-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.0.3 spec/ffi/managed_struct_spec.rb
ffi-1.0.2-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.0.2 spec/ffi/managed_struct_spec.rb
ffi-1.0.1-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.0.1 spec/ffi/managed_struct_spec.rb
ffi-1.0.0 spec/ffi/managed_struct_spec.rb
ffi-aitrus-0.9.0.beta spec/ffi/managed_struct_spec.rb
ffi-0.6.3-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-0.6.3-x86-mswin32 spec/ffi/managed_struct_spec.rb
ffi-0.6.3 spec/ffi/managed_struct_spec.rb
ffi-0.6.2 spec/ffi/managed_struct_spec.rb
ffi-0.6.1 spec/ffi/managed_struct_spec.rb
ffi-0.6.0 spec/ffi/managed_struct_spec.rb
ffi-0.5.4-x86-mswin32 spec/ffi/managed_struct_spec.rb