Sha256: 32ebc75ea0ab75d7464ad2bea90c6f9d44fdda7740befbbf9d2ba84882cff2f8

Contents?: true

Size: 1.57 KB

Versions: 39

Compression:

Stored size: 1.57 KB

Contents

#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

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

describe "Managed Struct" do
  include FFI
  module ManagedStructTestLib
    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(ManagedStructTestLib.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(ManagedStructTestLib.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
          TestLibrary.force_gc
          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
      PleaseReleaseMe.new(ManagedStructTestLib.ptr_from_address(0x12345678))
    end
    PleaseReleaseMe.wait_gc loop_count
  end
end

Version data entries

39 entries across 37 versions & 8 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/ffi-1.9.3/spec/ffi/managed_struct_spec.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/ffi-1.9.3/spec/ffi/managed_struct_spec.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/ffi-1.9.3/spec/ffi/managed_struct_spec.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/ffi-1.9.3/spec/ffi/managed_struct_spec.rb
ffi-1.9.3-x64-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.9.3-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.9.3 spec/ffi/managed_struct_spec.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/ffi-1.9.0/spec/ffi/managed_struct_spec.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/ffi-1.9.0/spec/ffi/managed_struct_spec.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
ffi-1.9.0-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.9.0 spec/ffi/managed_struct_spec.rb
vagrant-shell-0.2.6 vendor/bundle/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
vagrant-shell-0.2.5 vendor/bundle/gems/ffi-1.8.1/spec/ffi/managed_struct_spec.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/ffi-1.3.1/spec/ffi/managed_struct_spec.rb
ffi-1.8.1-x86-mingw32 spec/ffi/managed_struct_spec.rb
ffi-1.8.1 spec/ffi/managed_struct_spec.rb