File: reference_queue_spec.rb

package info (click to toggle)
ruby-ref 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: ruby: 1,262; java: 92; makefile: 5
file content (83 lines) | stat: -rw-r--r-- 1,913 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'spec_helper'

describe Ref::ReferenceQueue do

  let(:queue) { Ref::ReferenceQueue.new }
  let(:obj_1) { Object.new }
  let(:obj_2) { Object.new }
  let(:ref_1) { Ref::WeakReference.new(obj_1) }
  let(:ref_2) { Ref::WeakReference.new(obj_2) }

  describe '#push' do
    context 'when the queue is empty' do
      it { expect(queue.size).to be(0) }
      it { expect(queue).to be_empty }
    end

    context 'when the queue is not empty' do
      before do
        queue.push(ref_1)
        queue.push(ref_2)
      end

      it { expect(queue.size).to be(2) }
      it { expect(queue).to_not be_empty }
    end
  end

  describe '#shift' do
    context 'when the queue is not empty' do
      before do
        queue.push(ref_1)
        queue.push(ref_2)
      end
      it { expect(queue.shift).to be(ref_1) }
    end

    context 'when the queue is empty' do
      it { expect(queue.shift).to be_nil }
    end
  end

  describe '#pop' do
    context 'when the queue is not empty' do
      before do
        queue.push(ref_1)
        queue.push(ref_2)
      end

      it { expect(queue.pop).to be(ref_2) }
    end
    context 'when the queue is empty' do
      it { expect(queue.pop).to be_nil }
    end
  end

  context 'references are added immediately if the_object has been collected' do
    specify do
      Ref::Mock.use do
        ref = Ref::WeakReference.new(obj_1)
        Ref::Mock.gc(obj_1)
        queue.monitor(ref)

        expect(ref).to equal(queue.shift)
      end
    end
  end

  context 'references are added when the object has been collected' do
    specify do
      Ref::Mock.use do
        ref = Ref::WeakReference.new(obj_1)
        queue.monitor(ref)
        result = queue.shift
        expect(result).to eq nil

        Ref::Mock.gc(obj_1)

        object = queue.shift
        expect(ref.referenced_object_id).to eq object.referenced_object_id
      end
    end
  end
 end