File: cursor_reaper_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (230 lines) | stat: -rw-r--r-- 5,799 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Cluster::CursorReaper do

  let(:cluster) { double('cluster') }

  before do
    authorized_collection.drop
  end

  let(:reaper) do
    described_class.new(cluster)
  end

  let(:active_cursor_ids) do
    reaper.instance_variable_get(:@active_cursor_ids)
  end

  describe '#intialize' do

    it 'initializes a hash for servers and their kill cursors ops' do
      expect(reaper.instance_variable_get(:@to_kill)).to be_a(Hash)
    end

    it 'initializes a set for the list of active cursors' do
      expect(reaper.instance_variable_get(:@active_cursor_ids)).to be_a(Set)
    end
  end

  describe '#schedule_kill_cursor' do

    let(:address) { Mongo::Address.new('localhost') }
    let(:server) do
      double('server').tap do |server|
        allow(server).to receive(:address).and_return(address)
      end
    end
    let(:session) do
      double(Mongo::Session)
    end
    let(:cursor_id) { 1 }
    let(:cursor_kill_spec_1) do
      Mongo::Cursor::KillSpec.new(
        cursor_id: cursor_id,
        coll_name: 'c',
        db_name: 'd',
        server_address: address,
        connection_global_id: 1,
        session: session,
      )
    end
    let(:cursor_kill_spec_2) do
      Mongo::Cursor::KillSpec.new(
        cursor_id: cursor_id,
        coll_name: 'c',
        db_name: 'q',
        server_address: address,
        connection_global_id: 1,
        session: session,
      )
    end
    let(:to_kill) { reaper.instance_variable_get(:@to_kill)}

    context 'when the cursor is on the list of active cursors' do

      before do
        reaper.register_cursor(cursor_id)
      end

      context 'when there is not a list already for the server' do

        before do
          reaper.schedule_kill_cursor(cursor_kill_spec_1)
          reaper.read_scheduled_kill_specs
        end

        it 'initializes the list of op specs to a set' do
          expect(to_kill.keys).to eq([ address ])
          expect(to_kill[address]).to contain_exactly(cursor_kill_spec_1)
        end
      end

      context 'when there is a list of ops already for the server' do

        before do
          reaper.schedule_kill_cursor(cursor_kill_spec_1)
          reaper.read_scheduled_kill_specs
          reaper.schedule_kill_cursor(cursor_kill_spec_2)
          reaper.read_scheduled_kill_specs
        end

        it 'adds the op to the server list' do
          expect(to_kill.keys).to eq([ address ])
          expect(to_kill[address]).to contain_exactly(cursor_kill_spec_1, cursor_kill_spec_2)
        end

        context 'when the same op is added more than once' do

          before do
            reaper.schedule_kill_cursor(cursor_kill_spec_2)
            reaper.read_scheduled_kill_specs
          end

          it 'does not allow duplicates ops for a server' do
            expect(to_kill.keys).to eq([ address ])
            expect(to_kill[address]).to contain_exactly(cursor_kill_spec_1, cursor_kill_spec_2)
          end
        end
      end
    end

    context 'when the cursor is not on the list of active cursors' do

      before do
        reaper.schedule_kill_cursor(cursor_kill_spec_1)
      end

      it 'does not add the kill cursors op spec to the list' do
        expect(to_kill).to eq({})
      end
    end
  end

  describe '#register_cursor' do

    context 'when the cursor id is nil' do

      let(:cursor_id) do
        nil
      end

      it 'raises exception' do
        expect do
          reaper.register_cursor(cursor_id)
        end.to raise_error(ArgumentError, /register_cursor called with nil cursor_id/)
      end
    end

    context 'when the cursor id is 0' do

      let(:cursor_id) do
        0
      end

      it 'raises exception' do
        expect do
          reaper.register_cursor(cursor_id)
        end.to raise_error(ArgumentError, /register_cursor called with cursor_id=0/)
      end
    end

    context 'when the cursor id is a valid id' do

      let(:cursor_id) do
        2
      end

      before do
        reaper.register_cursor(cursor_id)
      end

      it 'registers the cursor id as active' do
        expect(active_cursor_ids).to eq(Set.new([2]))
      end
    end
  end

  describe '#unregister_cursor' do

    context 'when the cursor id is in the active cursors list' do

      before do
        reaper.register_cursor(2)
        reaper.unregister_cursor(2)
      end

      it 'removes the cursor id' do
        expect(active_cursor_ids.size).to eq(0)
      end
    end
  end

  context 'when a non-exhausted cursor goes out of scope' do

    let(:docs) do
      103.times.collect { |i| { a: i } }
    end

    let(:periodic_executor) do
      cluster.instance_variable_get(:@periodic_executor)
    end

    let(:cluster) do
      authorized_client.cluster
    end

    let(:cursor) do
      view = authorized_collection.find
      view.to_enum.next
      cursor = view.instance_variable_get(:@cursor)
    end

    around do |example|
      authorized_collection.insert_many(docs)
      periodic_executor.stop!
      cluster.schedule_kill_cursor(
        cursor.kill_spec(
          cursor.instance_variable_get(:@server)
        )
      )
      periodic_executor.flush
      example.run
      periodic_executor.run!
    end

    it 'schedules the kill cursor op' do
      expect {
        cursor.to_a
        # Mongo::Error::SessionEnded is raised here because the periodic executor
        # called in around block kills the cursor and closes the session.
        # This code is normally scheduled in cursor finalizer, so the cursor object
        # is garbage collected when the code is executed. So, a user won't get
        # this exception.
      }.to raise_exception(Mongo::Error::SessionEnded)
    end
  end
end