File: connection_pool_spec.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (463 lines) | stat: -rw-r--r-- 11,917 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
require 'spec_helper'

describe Mongo::Server::ConnectionPool do

  let(:options) do
    TEST_OPTIONS.merge(max_pool_size: 2)
  end

  let(:address) do
    Mongo::Address.new('127.0.0.1:27017')
  end

  let(:monitoring) do
    Mongo::Monitoring.new(monitoring: false)
  end

  let(:listeners) do
    Mongo::Event::Listeners.new
  end

  let(:topology) do
    double('topology')
  end

  let(:cluster) do
    double('cluster').tap do |cl|
      allow(cl).to receive(:topology).and_return(topology)
      allow(cl).to receive(:app_metadata).and_return(app_metadata)
    end
  end

  describe '#checkin' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    after do
      expect(cluster).to receive(:pool).with(server).and_return(pool)
      server.disconnect!
    end

    context 'when a connection is checked out on the thread' do

      let!(:connection) do
        pool.checkout
      end

      before do
        pool.checkin(connection)
      end

      let(:queue) do
        pool.send(:queue).queue
      end

      it 'returns the connection to the queue' do
        expect(queue.size).to eq(1)
      end
    end
  end

  describe '#checkout' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    context 'when no connection is checked out on the same thread' do

      let!(:connection) do
        pool.checkout
      end

      it 'returns a new connection' do
        expect(connection.address).to eq(server.address)
      end
    end

    context 'when a connection is checked out on the same thread' do

      before do
        pool.checkout
      end

      it 'returns the threads connection' do
        expect(pool.checkout.address).to eq(server.address)
      end
    end

    context 'when a connection is checked out on a different thread' do

      let!(:connection) do
        Thread.new { pool.checkout }.join
      end

      it 'returns a new connection' do
        expect(pool.checkout.address).to eq(server.address)
      end

      it 'does not return the same connection instance' do
        expect(pool.checkout).to_not eql(connection)
      end
    end

    context 'when connections are checked out and checked back in' do

      it 'pulls the connection from the front of the queue' do
        first = pool.checkout
        second = pool.checkout
        pool.checkin(second)
        pool.checkin(first)
        expect(pool.checkout).to be(first)
      end
    end
  end

  describe '#disconnect!' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    it 'disconnects the queue' do
      expect(cluster).to receive(:pool).with(server).and_return(pool)
      expect(pool.send(:queue)).to receive(:disconnect!).once.and_call_original
      server.disconnect!
    end
  end

  describe '.get' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    after do
      expect(cluster).to receive(:pool).with(server).and_return(pool)
      server.disconnect!
    end

    it 'returns the pool for the server' do
      expect(pool).to_not be_nil
    end
  end

  describe '#inspect' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    after do
      expect(cluster).to receive(:pool).with(server).and_return(pool)
      server.disconnect!
    end

    it 'includes the object id' do
      expect(pool.inspect).to include(pool.object_id.to_s)
    end

    it 'includes the queue inspection' do
      expect(pool.inspect).to include(pool.__send__(:queue).inspect)
    end
  end

  describe '#with_connection' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    context 'when a connection cannot be checked out' do

      before do
        allow(pool).to receive(:checkout).and_return(nil)
        pool.with_connection { |c| c }
      end

      let(:queue) do
        pool.send(:queue).queue
      end

      it 'does not add the connection to the pool' do
        expect(queue.size).to eq(1)
      end
    end
  end

  context 'when the connection does not finish authenticating before the thread is killed' do

    let(:server) do
      Mongo::Server.new(address, cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    let(:options) do
      { user: ROOT_USER.name, password: ROOT_USER.password }.merge(TEST_OPTIONS).merge(max_pool_size: 1)
    end

    before do
     t = Thread.new {
        # Kill the thread when it's authenticating
        allow(Mongo::Auth).to receive(:get) { t.kill and t.stop? }
        pool.with_connection { |c| c.send(:ensure_connected) { |socket| socket } }
      }
      t.join
    end

    it 'disconnects the socket' do
      expect(pool.checkout.send(:socket)).to be_nil
    end
  end

  describe '#close_stale_sockets!' do

    let(:server) do
      Mongo::Server.new(address, authorized_client.cluster, monitoring, listeners, options)
    end

    let!(:pool) do
      described_class.get(server)
    end

    let(:queue) do
      pool.instance_variable_get(:@queue).queue
    end

    context 'when there is a max_idle_time specified' do

      let(:options) do
        TEST_OPTIONS.merge(max_pool_size: 2, max_idle_time: 0.5)
      end

      context 'when the connections have not been checked out' do

        before do
          queue.each do |conn|
            expect(conn).not_to receive(:disconnect!)
          end
          sleep(0.5)
          pool.close_stale_sockets!
        end

        it 'does not close any sockets' do
          expect(queue.none? { |c| c.connected? }).to be(true)
        end
      end

      context 'when the sockets have already been checked out and returned to the pool' do

        context 'when min size is 0' do

          let(:options) do
            TEST_OPTIONS.merge(max_pool_size: 2, min_pool_size: 0, max_idle_time: 0.5)
          end

          before do
            queue.each do |conn|
              expect(conn).to receive(:disconnect!).and_call_original
            end
            pool.checkin(pool.checkout)
            pool.checkin(pool.checkout)
            sleep(0.5)
            pool.close_stale_sockets!
          end

          it 'closes all stale sockets' do
            expect(queue.all? { |c| !c.connected? }).to be(true)
          end
        end

        context 'when min size is > 0' do

          context 'when more than the number of min_size are checked out' do

            let(:options) do
              TEST_OPTIONS.merge(max_pool_size: 5, min_pool_size: 3, max_idle_time: 0.5)
            end

            before do
              first = pool.checkout
              second = pool.checkout
              third = pool.checkout
              fourth = pool.checkout
              fifth = pool.checkout

              pool.checkin(fifth)

              expect(fifth).to receive(:disconnect!).and_call_original
              expect(fifth).not_to receive(:connect!)

              sleep(0.5)
              pool.close_stale_sockets!
            end

            it 'closes all stale sockets and does not connect new ones' do
              expect(queue.size).to be(1)
              expect(queue[0].connected?).to be(false)
            end
          end

          context 'when between 0 and min_size number of connections are checked out' do

            let(:options) do
              TEST_OPTIONS.merge(max_pool_size: 5, min_pool_size: 3, max_idle_time: 0.5)
            end

            before do
              first = pool.checkout
              second = pool.checkout
              third = pool.checkout
              fourth = pool.checkout
              fifth = pool.checkout

              pool.checkin(third)
              pool.checkin(fourth)
              pool.checkin(fifth)


              expect(third).to receive(:disconnect!).and_call_original
              expect(third).not_to receive(:connect!)

              expect(fourth).to receive(:disconnect!).and_call_original
              expect(fourth).not_to receive(:connect!)

              expect(fifth).to receive(:disconnect!).and_call_original
              expect(fifth).to receive(:connect!).and_call_original

              sleep(0.5)
              pool.close_stale_sockets!
            end

            it 'closes all stale sockets and does not connect new ones' do
              expect(queue.size).to be(3)
              expect(queue[0].connected?).to be(true)
              expect(queue[1].connected?).to be(false)
              expect(queue[2].connected?).to be(false)
            end
          end

          context 'when a stale connection is unsuccessfully reconnected' do

            let(:options) do
              TEST_OPTIONS.merge(max_pool_size: 5, min_pool_size: 3, max_idle_time: 0.5)
            end

            before do
              first = pool.checkout
              second = pool.checkout
              third = pool.checkout
              fourth = pool.checkout
              fifth = pool.checkout

              pool.checkin(third)
              pool.checkin(fourth)
              pool.checkin(fifth)


              expect(third).to receive(:disconnect!).and_call_original
              expect(third).not_to receive(:connect!)

              expect(fourth).to receive(:disconnect!).and_call_original
              expect(fourth).not_to receive(:connect!)

              expect(fifth).to receive(:disconnect!).and_call_original
              allow(fifth).to receive(:connect!).and_raise(Mongo::Error::SocketError)

              sleep(0.5)
              pool.close_stale_sockets!
            end

            it 'is kept in the pool' do
              expect(queue.size).to be(3)
              expect(queue[0].connected?).to be(false)
              expect(queue[1].connected?).to be(false)
              expect(queue[2].connected?).to be(false)
            end
          end

          context 'when exactly the min_size number of connections is checked out' do

            let(:options) do
              TEST_OPTIONS.merge(max_pool_size: 5, min_pool_size: 3, max_idle_time: 0.5)
            end

            before do
              first = pool.checkout
              second = pool.checkout
              third = pool.checkout
              fourth = pool.checkout
              fifth = pool.checkout

              pool.checkin(fourth)
              pool.checkin(fifth)

              expect(fourth).to receive(:disconnect!).and_call_original
              expect(fourth).not_to receive(:connect!)

              expect(fifth).to receive(:disconnect!).and_call_original
              expect(fifth).not_to receive(:connect!)

              sleep(0.5)
              pool.close_stale_sockets!
            end

            it 'closes all stale sockets and does not connect new ones' do
              expect(queue.size).to be(2)
              expect(queue[0].connected?).to be(false)
              expect(queue[1].connected?).to be(false)
            end
          end
        end
      end
    end

    context 'when there is no max_idle_time specified' do

      let(:connection) do
        conn = pool.checkout
        conn.connect!
        pool.checkin(conn)
        conn
      end

      before do
        expect(connection).not_to receive(:disconnect!)
        pool.close_stale_sockets!
      end

      it 'does not close any sockets' do
        expect(connection.connected?).to be(true)
      end
    end
  end
end