Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit f628eb9

Browse files
committed
Migrate tests to RSpec
1 parent 17ed4a8 commit f628eb9

23 files changed

+429
-629
lines changed

spec/ref/reference_queue_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
end
5454
end
5555

56-
describe 'references are added immediately if the_object has been collected' do
56+
context 'references are added immediately if the_object has been collected' do
5757
specify do
5858
Ref::Mock.use do
5959
ref = Ref::WeakReference.new(obj_1)
@@ -64,4 +64,20 @@
6464
end
6565
end
6666
end
67-
end
67+
68+
context 'references are added when the object has been collected' do
69+
specify do
70+
Ref::Mock.use do
71+
ref = Ref::WeakReference.new(obj_1)
72+
queue.monitor(ref)
73+
result = queue.shift
74+
expect(result).to eq nil
75+
76+
Ref::Mock.gc(obj_1)
77+
78+
object = queue.shift
79+
expect(ref.referenced_object_id).to eq object.referenced_object_id
80+
end
81+
end
82+
end
83+
end

spec/ref/soft_key_map_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'spec_helper'
2+
3+
describe Ref::SoftKeyMap do
4+
it_behaves_like 'a reference key map' do
5+
let(:map_class) { Ref::SoftKeyMap }
6+
let(:reference_class) { Ref::SoftReference }
7+
end
8+
end

spec/ref/soft_reference_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'spec_helper'
2+
3+
describe Ref::SoftReference do
4+
describe '#object' do
5+
context 'when has not garbage collected objects' do
6+
it 'gets the object' do
7+
obj = Object.new
8+
ref = Ref::SoftReference.new(obj)
9+
10+
expect(obj).to eq ref.object
11+
expect(obj.object_id).to eq ref.referenced_object_id
12+
end
13+
end
14+
15+
context 'when has a lot of objects' do
16+
# Since we can't reliably control the garbage collector, this is a brute force test.
17+
# It might not always fail if the garbage collector and memory allocator don't
18+
# cooperate, but it should fail often enough on continuous integration to
19+
# hilite any problems. Set the environment variable QUICK_TEST to "true" if you
20+
# want to make the tests run quickly.
21+
it 'get the correct object' do
22+
id_to_ref = {}
23+
(ENV["QUICK_TEST"] == "true" ? 1000 : 100000).times do |i|
24+
obj = Object.new
25+
if id_to_ref.key?(obj.object_id)
26+
ref = id_to_ref[obj.object_id]
27+
if ref.object
28+
fail "soft reference found with a live reference to an object that was not the one it was created with"
29+
break
30+
end
31+
end
32+
33+
%w(Here are a bunch of objects that are allocated and can then be cleaned up by the garbage collector)
34+
id_to_ref[obj.object_id] = Ref::SoftReference.new(obj)
35+
if i % 1000 == 0
36+
GC.start
37+
sleep(0.01)
38+
end
39+
end
40+
end
41+
end
42+
43+
context 'when references are not collected immediately' do
44+
it "the object can't be nil" do
45+
ref = Ref::SoftReference.new(Object.new)
46+
9.times{ arr = %w(allocate some memory on the heap); arr *= 100; GC.start }
47+
expect(ref.object).to_not be_nil
48+
end
49+
end
50+
end
51+
52+
describe '#inspect' do
53+
context 'when GC is called' do
54+
it 'inspects not be nil' do
55+
ref = Ref::SoftReference.new(Object.new)
56+
expect(ref.inspect).to_not be_nil
57+
GC.start
58+
GC.start
59+
expect(ref.inspect).to_not be_nil
60+
end
61+
end
62+
end
63+
end

spec/ref/soft_value_map_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'spec_helper'
22

33
describe Ref::SoftValueMap do
4-
it_behaves_like 'a reference key map' do
5-
let(:map_class) { Ref::SoftValueMap }
4+
it_behaves_like 'a reference value map' do
5+
let(:map_class) { Ref::SoftValueMap }
66
let(:reference_class) { Ref::SoftReference }
77
end
88
end

spec/ref/weak_key_map_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'spec_helper'
2+
3+
describe Ref::WeakKeyMap do
4+
it_behaves_like 'a reference key map' do
5+
let(:map_class) { Ref::WeakKeyMap }
6+
let(:reference_class) { Ref::WeakReference }
7+
end
8+
end

spec/ref/weak_reference_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'spec_helper'
2+
3+
describe Ref::WeakReference do
4+
describe '#object' do
5+
context 'when has not garbage collected objects' do
6+
it 'gets the object' do
7+
obj = Object.new
8+
ref_1 = Ref::WeakReference.new(obj)
9+
ref_2 = Ref::WeakReference.new(obj)
10+
11+
expect(obj).to eq ref_1.object
12+
expect(obj.object_id).to eq ref_1.referenced_object_id
13+
expect(obj).to eq ref_2.object
14+
expect(obj.object_id).to eq ref_2.referenced_object_id
15+
end
16+
end
17+
18+
context 'when has a lot of objects' do
19+
# Since we can't reliably control the garbage collector, this is a brute force test.
20+
# It might not always fail if the garbage collector and memory allocator don't
21+
# cooperate, but it should fail often enough on continuous integration to
22+
# hilite any problems. Set the environment variable QUICK_TEST to "true" if you
23+
# want to make the tests run quickly.
24+
it 'get the correct object' do
25+
id_to_ref = {}
26+
(ENV["QUICK_TEST"] == "true" ? 1000 : 100000).times do |i|
27+
obj = Object.new
28+
if id_to_ref.key?(obj.object_id)
29+
ref = id_to_ref[obj.object_id]
30+
if ref.object
31+
fail "weak reference found with a live reference to an object that was not the one it was created with"
32+
break
33+
end
34+
end
35+
%w(Here are a bunch of objects that are allocated and can then be cleaned up by the garbage collector)
36+
id_to_ref[obj.object_id] = Ref::WeakReference.new(obj)
37+
if i % 1000 == 0
38+
GC.start
39+
sleep(0.01)
40+
end
41+
end
42+
end
43+
end
44+
end
45+
46+
describe '#inspect' do
47+
context 'when GC is called' do
48+
it 'inspects not be nil' do
49+
ref = Ref::SoftReference.new(Object.new)
50+
expect(ref.inspect).to_not be_nil
51+
GC.start
52+
GC.start
53+
expect(ref.inspect).to_not be_nil
54+
end
55+
end
56+
end
57+
end

spec/ref/weak_value_map_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'spec_helper'
2+
3+
describe Ref::WeakValueMap do
4+
it_behaves_like 'a reference value map' do
5+
let(:map_class) { Ref::WeakValueMap }
6+
let(:reference_class) { Ref::WeakReference }
7+
end
8+
end
File renamed without changes.
Lines changed: 115 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,155 @@
11
require 'spec_helper'
22

33
shared_examples 'a reference key map' do
4-
54
let(:value_1) { 'value 1' }
65
let(:value_2) { 'value 2' }
76
let(:value_3) { 'value 3' }
87
let(:key_1) { Object.new }
98
let(:key_2) { Object.new }
9+
let(:key_3) { Object.new }
1010
let(:hash) { map_class.new }
1111

1212
describe 'keeps entries with strong references' do
1313
specify do
1414
Ref::Mock.use do
15-
hash[key_1] = value_1
16-
hash[key_2] = value_2
17-
expect(hash[key_1] ).to eq(value_1)
18-
expect(hash[key_2] ).to eq(value_2)
15+
hash[key_1] = "value 1"
16+
hash[key_2] = "value 2"
17+
expect(hash[key_1]).to eq "value 1"
18+
expect(hash[key_2]).to eq "value 2"
1919
end
2020
end
2121
end
2222

2323
describe 'removes entries that have been garbage collected' do
2424
specify do
2525
Ref::Mock.use do
26-
hash['key 1'] = value_1
27-
hash['key 2'] = value_2
28-
expect(hash['key 1']).to eq(value_1)
29-
expect(hash['key 2']).to eq(value_2)
30-
Ref::Mock.gc(value_2)
31-
expect(value_1).to eq(value_1)
32-
expect(hash['key 2']).to be_nil
26+
hash[key_1] = value_1
27+
hash[key_2] = value_2
28+
expect(hash[key_1]).to eq(value_1)
29+
expect(hash[key_2]).to eq(value_2)
30+
Ref::Mock.gc(key_2)
31+
expect(hash[key_1]).to eq(value_1)
32+
expect(hash[key_2]).to be_nil
3333
end
3434
end
3535
end
3636

3737
describe 'can clear the map' do
3838
specify do
39-
hash['key 1'] = value_1
40-
hash['key 2'] = value_2
41-
hash.clear
42-
expect(hash['key 1']).to be_nil
43-
expect(hash['key 2']).to be_nil
39+
Ref::Mock.use do
40+
hash[key_1] = value_1
41+
hash[key_2] = value_2
42+
hash.clear
43+
expect(hash[key_1]).to be_nil
44+
expect(hash[key_2]).to be_nil
45+
end
4446
end
4547
end
4648

4749
describe 'can delete entries' do
4850
specify do
49-
hash['key 1'] = value_1
50-
hash['key 2'] = value_2
51-
expect(hash.delete('key 3')).to be_nil
52-
expect(hash.delete('key 1')).to eq(value_1)
53-
expect(hash['key 1']).to be_nil
51+
Ref::Mock.use do
52+
hash[key_1] = value_1
53+
hash[key_2] = value_2
54+
Ref::Mock.gc(key_2)
55+
expect(hash.delete(key_2)).to be_nil
56+
expect(hash.delete(key_1)).to eq(value_1)
57+
expect(hash[key_1]).to be_nil
58+
end
5459
end
5560
end
5661

5762
describe 'can merge in another hash' do
5863
specify do
59-
hash['key 1'] = value_1
60-
hash['key 2'] = value_2
61-
hash.merge!("key 3" => value_3)
62-
expect(hash['key 1']).to be_nil
64+
Ref::Mock.use do
65+
hash[key_1] = value_1
66+
hash[key_2] = value_2
67+
hash.merge!(key_3 => value_3)
68+
69+
expect(hash[key_2]).to eq 'value 2'
70+
expect(hash[key_1]).to eq value_1
71+
72+
Ref::Mock.gc(key_2)
73+
74+
expect(hash[key_2]).to be_nil
75+
expect(hash[key_1]).to eq value_1
76+
expect(hash[key_3]).to eq value_3
77+
end
78+
end
79+
end
6380

81+
describe 'can get all keys' do
82+
specify do
83+
Ref::Mock.use do
84+
hash[key_1] = value_1
85+
hash[key_2] = value_2
86+
hash[key_3] = value_3
87+
expect([key_1, key_2, key_3] - hash.keys).to eq []
88+
Ref::Mock.gc(key_2)
89+
expect([key_1, key_2, key_3] - hash.keys).to eq [key_2]
90+
end
91+
end
92+
end
93+
94+
describe 'can turn into an array' do
95+
specify do
96+
Ref::Mock.use do
97+
hash[key_1] = value_1
98+
hash[key_2] = value_2
99+
hash[key_3] = value_3
100+
order = lambda{|a,b| a.last <=> b.last}
101+
expect([[key_1, "value 1"], [key_2, "value 2"], [key_3, "value 3"]].sort(&order)).to eq hash.to_a.sort(&order)
102+
Ref::Mock.gc(key_2)
103+
expect([[key_1, "value 1"], [key_3, "value 3"]].sort(&order)).to eq hash.to_a.sort(&order)
104+
end
105+
end
106+
end
107+
108+
describe 'can interate over all entries' do
109+
specify do
110+
Ref::Mock.use do
111+
hash[key_1] = value_1
112+
hash[key_2] = value_2
113+
hash[key_3] = value_3
114+
keys = []
115+
values = []
116+
hash.each{|k,v| keys << k; values << v}
117+
expect([key_1, key_2, key_3] - keys).to eq []
118+
expect(["value 1", "value 2", "value 3"]).to eq values.sort
119+
Ref::Mock.gc(key_2)
120+
keys = []
121+
values = []
122+
hash.each{|k,v| keys << k; values << v}
123+
expect([key_1, key_2, key_3] - keys).to eq [key_2]
124+
expect(["value 1", "value 3"]).to eq values.sort
125+
end
126+
end
127+
end
128+
129+
describe 'size' do
130+
specify do
131+
Ref::Mock.use do
132+
hash = map_class.new
133+
expect(hash.empty?).to eq true
134+
expect(hash.size).to eq 0
135+
key_1 = Object.new
136+
key_2 = Object.new
137+
hash[key_1] = "value 1"
138+
hash[key_2] = "value 2"
139+
expect(hash.size).to eq 2
140+
Ref::Mock.gc(key_2)
141+
expect(hash.empty?).to eq false
142+
expect(hash.size).to eq 1
143+
end
144+
end
145+
end
146+
147+
describe 'inspect' do
148+
specify do
149+
Ref::Mock.use do
150+
hash[Object.new] = "value 1"
151+
expect(hash.inspect).to_not be_nil
152+
end
64153
end
65154
end
66155
end

0 commit comments

Comments
 (0)