File: monitor_spec.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (312 lines) | stat: -rw-r--r-- 10,761 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
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
# frozen_string_literal: true
# rubocop:todo all

require 'lite_spec_helper'

describe Mongo::Srv::Monitor do
  describe '#scan!' do
    let(:hostname) do
      'test1.test.build.10gen.cc'
    end

    let(:hosts) do
      [
        'localhost.test.build.10gen.cc:27017',
        'localhost.test.build.10gen.cc:27018',
      ]
    end

    let(:result) do
      double('result').tap do |result|
        allow(result).to receive(:hostname).and_return(hostname)
        allow(result).to receive(:address_strs).and_return(hosts)
        allow(result).to receive(:empty?).and_return(false)
        allow(result).to receive(:min_ttl).and_return(nil)
      end
    end

    let(:uri_resolver) do
      double('uri resolver').tap do |resolver|
        expect(resolver).to receive(:get_records).and_return(result)
      end
    end

    context 'when srv_max_hosts is not specified' do
      let(:srv_uri) do
        Mongo::URI.get("mongodb+srv://this.is.not.used")
      end

      let(:cluster) do
        Mongo::Cluster.new(hosts, Mongo::Monitoring.new, monitoring_io: false)
      end

      let(:monitor) do
        described_class.new(cluster, srv_uri: srv_uri)
      end

      before do
        # monitor instantiation triggers cluster instantiation which
        # performs real SRV lookups for the hostname.
        # The next lookup (the one performed when cluster is already set up)
        # is using our doubles.
        RSpec::Mocks.with_temporary_scope do
          allow(uri_resolver).to receive(:get_txt_options_string)
          expect(Mongo::Srv::Resolver).to receive(:new).ordered.and_return(uri_resolver)
          allow(resolver).to receive(:get_txt_options_string)
          expect(Mongo::Srv::Resolver).to receive(:new).ordered.and_return(resolver)
          monitor.send(:scan!)
        end
      end

      context 'when a new DNS record is added' do
        let(:new_hosts) do
          hosts + ['localhost.test.build.10gen.cc:27019']
        end

        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return(new_hosts)
            allow(result).to receive(:empty?).and_return(false)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('monitor resolver').tap do |resolver|
            expect(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'adds the new host to the cluster' do
          expect(cluster.servers_list.map(&:address).map(&:to_s).sort).to eq(new_hosts.sort)
        end
      end

      context 'when a DNS record is removed' do
        let(:new_hosts) do
          hosts - ['test1.test.build.10gen.cc:27018']
        end

        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return(new_hosts)
            allow(result).to receive(:empty?).and_return(false)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('resolver').tap do |resolver|
            allow(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'adds the new host to the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(new_hosts.sort)
        end
      end

      context 'when a single DNS record is replaced' do
        let(:new_hosts) do
          hosts - ['test1.test.build.10gen.cc:27018'] +  ['test1.test.build.10gen.cc:27019']
        end

        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return(new_hosts)
            allow(result).to receive(:empty?).and_return(false)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('resolver').tap do |resolver|
            allow(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'adds the new host to the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(new_hosts.sort)
        end
      end

      context 'when all DNS result are replaced with a single record' do
        let(:new_hosts) do
          ['test1.test.build.10gen.cc:27019']
        end

        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return(new_hosts)
            allow(result).to receive(:empty?).and_return(false)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('resolver').tap do |resolver|
            expect(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'adds the new host to the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(new_hosts.sort)
        end
      end

      context 'when all DNS result are replaced with multiple result' do
        let(:new_hosts) do
          [
            'test1.test.build.10gen.cc:27019',
            'test1.test.build.10gen.cc:27020',
          ]
        end

        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return(new_hosts)
            allow(result).to receive(:empty?).and_return(false)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('resolver').tap do |resolver|
            allow(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'adds the new host to the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(new_hosts.sort)
        end
      end

      context 'when the DNS lookup times out' do
        let(:resolver) do
          double('resolver').tap do |resolver|
            expect(resolver).to receive(:get_records).and_raise(Resolv::ResolvTimeout)
          end
        end

        it 'does not add or remove any hosts from the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(hosts.sort)
        end
      end

      context 'when the DNS lookup is unable to resolve the hostname' do
      let(:resolver) do
          double('resolver').tap do |resolver|
            allow(resolver).to receive(:get_records).and_raise(Resolv::ResolvError)
          end
        end

        it 'does not add or remove any hosts from the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(hosts.sort)
        end
      end

      context 'when no DNS result are returned' do
        let(:new_result) do
          double('result').tap do |result|
            allow(result).to receive(:hostname).and_return(hostname)
            allow(result).to receive(:address_strs).and_return([])
            allow(result).to receive(:empty?).and_return(true)
            allow(result).to receive(:min_ttl).and_return(nil)
          end
        end

        let(:resolver) do
          double('resolver').tap do |resolver|
            allow(resolver).to receive(:get_records).and_return(new_result)
          end
        end

        it 'does not add or remove any hosts from the cluster' do
          expect(cluster.addresses.map(&:to_s).sort).to eq(hosts.sort)
        end
      end
    end

    context 'when srv_max_hosts is specified' do
      let(:cluster) do
        Mongo::Cluster.new(hosts, Mongo::Monitoring.new, monitoring_io: false)
      end

      let(:limited_result) do
        double('result').tap do |result|
          allow(result).to receive(:hostname).and_return(hostname)
          allow(result).to receive(:address_strs).and_return([hosts.first])
          allow(result).to receive(:empty?).and_return(false)
          allow(result).to receive(:min_ttl).and_return(nil)
        end
      end

      let(:resolver) do
        double('resolver').tap do |resolver|
          allow(resolver).to receive(:get_txt_options_string)
          expect(resolver).to receive(:get_records).at_least(:once).with(
            anything,
            anything,
            1  # Verify srv_max_hosts=1 is passed
          ).and_return(limited_result)
        end
      end

      context 'when srv_max_hosts=1 in the URI' do
        let(:srv_uri_with_max_hosts) do
          Mongo::URI.get("mongodb+srv://this.is.not.used/?srvMaxHosts=1")
        end

        let(:monitor_with_max_hosts) do
          described_class.new(cluster, srv_uri: srv_uri_with_max_hosts)
        end
        
        before do
          # monitor instantiation triggers cluster instantiation which
          # performs real SRV lookups for the hostname.
          # The next lookup (the one performed when cluster is already set up)
          # is using our doubles.
          RSpec::Mocks.with_temporary_scope do
            allow(uri_resolver).to receive(:get_txt_options_string)
            expect(Mongo::Srv::Resolver).to receive(:new).ordered.and_return(uri_resolver)
            allow(resolver).to receive(:get_txt_options_string)
            expect(Mongo::Srv::Resolver).to receive(:new).ordered.and_return(resolver)
            monitor_with_max_hosts.send(:scan!)
          end
        end

        it 'limits the number of hosts in the cluster and passes srv_max_hosts to resolver' do
          expect(cluster.servers_list.size).to eq(1)
        end
      end

      context 'when srv_max_hosts is set on client' do
        it 'creates client with srv_max_hosts and passes it to resolver on scan' do
          client = nil
          expect(Mongo::Srv::Resolver).to receive(:new).at_least(:once).and_return(resolver)
          
          client = Mongo::Client.new('mongodb+srv://test.example.com/', srv_max_hosts: 1, monitoring_io: false, connect: :sharded)
          
          # Start SRV monitor but don't run the background thread
          client.cluster.send(:start_stop_srv_monitor)
          
          srv_monitor = client.cluster.instance_variable_get(:@srv_monitor)
          
          # Stop the background thread if it's running
          srv_monitor.stop! if srv_monitor.running?
          
          srv_monitor.instance_variable_set(:@resolver, resolver)
          
          srv_monitor.send(:scan!)
          
          expect(client.cluster.servers_list.size).to eq(1)
        end
      end
    end
  end
end