File: populator_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 (120 lines) | stat: -rw-r--r-- 2,971 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
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Server::ConnectionPool::Populator do
  require_no_linting

  let(:options) { {} }

  let(:client) do
    authorized_client.with(options)
  end

  let(:server) do
    client.cluster.next_primary
  end

  let(:pool) do
    server.pool
  end

  let(:populator) do
    register_background_thread_object(
      described_class.new(pool, pool.options)
    )
  end

  before do
    # We create our own populator to test; disable pool's background populator
    # and clear the pool, so ours can run
    pool.disconnect!
    pool.stop_populator
  end

  describe '#log_warn' do
    it 'works' do
      expect do
        populator.log_warn('test warning')
      end.not_to raise_error
    end
  end


  describe '#run!' do
    context 'when the min_pool_size is zero' do
      let(:options) { {min_pool_size: 0} }

      it 'calls populate on pool once' do
        expect(pool).to receive(:populate).once.and_call_original
        populator.run!
        sleep 1
        expect(populator.running?).to be true
      end
    end

    context 'when the min_pool_size is greater than zero' do
      let(:options) { {min_pool_size: 2, max_pool_size: 3} }

      it 'calls populate on the pool multiple times' do
        expect(pool).to receive(:populate).at_least(:once).and_call_original
        populator.run!
        sleep 1
        expect(populator.running?).to be true
      end

      it 'populates the pool up to min_size' do
        pool.instance_variable_set(:@ready, true)
        populator.run!
        ::Utils.wait_for_condition(3) do
          pool.size >= 2
        end
        expect(pool.size).to eq 2
        expect(populator.running?).to be true
      end
    end

    context 'when populate raises a non socket related error' do
      it 'does not terminate the thread' do
        expect(pool).to receive(:populate).once.and_raise(Mongo::Auth::InvalidMechanism.new(""))
        populator.run!
        sleep 0.5
        expect(populator.running?).to be true
      end
    end

    context 'when populate raises a socket related error' do
      it 'does not terminate the thread' do
        expect(pool).to receive(:populate).once.and_raise(Mongo::Error::SocketError)
        populator.run!
        sleep 0.5
        expect(populator.running?).to be true
      end
    end

    context "when clearing the pool" do
      it "the populator is run one extra time" do
        expect(pool).to receive(:populate).twice
        populator.run!
        sleep 0.5
        pool.disconnect!
        sleep 0.5
        expect(populator.running?).to be true
      end
    end
  end

  describe '#stop' do
    it 'stops calling populate on pool and terminates the thread' do
      populator.run!

      # let populator do work and wait on semaphore
      sleep 0.5

      expect(pool).not_to receive(:populate)
      populator.stop!
      expect(populator.running?).to be false
    end
  end
end