blob: b4e80bbebc718f657bacc0e568f372c48c8eca53 (
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
60
61
62
63
64
65
66
67
68
69
|
#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
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
expect { NoRelease.new(ManagedStructTestLib.ptr_from_address(0x12345678)) }.to raise_error(NoMethodError)
end
it "should be the right class" do
class WhatClassAmI < FFI::ManagedStruct
layout :i, :int
def self.release(_ptr)
end
end
expect(WhatClassAmI.new(ManagedStructTestLib.ptr_from_address(0x12345678)).class).to eq(WhatClassAmI)
end
it "should build with self reference" do
class ClassWithSelfRef < FFI::ManagedStruct
layout :data, self.ptr
def self.release(_ptr)
end
end
expect(ClassWithSelfRef.new(ManagedStructTestLib.ptr_from_address(0x12345678)).class).to eq(ClassWithSelfRef)
end
# see #427
it "should release memory properly" do
class PleaseReleaseMe < FFI::ManagedStruct
layout :i, :int
@@count = 0
def self.release(_ptr)
@@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
@@count
end
end
loop_count = 30
wiggle_room = 5
loop_count.times do
PleaseReleaseMe.new(ManagedStructTestLib.ptr_from_address(0x12345678))
end
calls = PleaseReleaseMe.wait_gc(loop_count)
expect(calls).to be >= loop_count-wiggle_room
end
end
|