File: server_selection_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 (95 lines) | stat: -rw-r--r-- 3,572 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
require 'spec_helper'

describe 'Server Selection' do

  include Mongo::ServerSelection::Read

  SERVER_SELECTION_TESTS.each do |file|

    spec = Mongo::ServerSelection::Read::Spec.new(file)

    context(spec.description) do

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

      let(:topology) do
        spec.type.new({}, monitoring)
      end

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

      let(:cluster) do
        double('cluster').tap do |c|
          allow(c).to receive(:topology).and_return(topology)
          allow(c).to receive(:single?).and_return(topology.single?)
          allow(c).to receive(:sharded?).and_return(topology.sharded?)
          allow(c).to receive(:replica_set?).and_return(topology.replica_set?)
          allow(c).to receive(:unknown?).and_return(topology.unknown?)
          allow(c).to receive(:app_metadata).and_return(app_metadata)
        end
      end

      let(:candidate_servers) do
        spec.candidate_servers.collect do |server|
          address = Mongo::Address.new(server['address'])
          Mongo::Server.new(address, cluster, monitoring, listeners, TEST_OPTIONS).tap do |s|
            allow(s).to receive(:average_round_trip_time).and_return(server['avg_rtt_ms'] / 1000.0)
            allow(s).to receive(:tags).and_return(server['tags'])
            allow(s).to receive(:secondary?).and_return(server['type'] == 'RSSecondary')
            allow(s).to receive(:primary?).and_return(server['type'] == 'RSPrimary')
            allow(s).to receive(:connectable?).and_return(true)
            allow(s).to receive(:check_driver_support!).and_return(true)
          end
        end
      end

      let(:in_latency_window) do
        spec.in_latency_window.collect do |server|
          address = Mongo::Address.new(server['address'])
          Mongo::Server.new(address, cluster, monitoring, listeners, TEST_OPTIONS).tap do |s|
            allow(s).to receive(:average_round_trip_time).and_return(server['avg_rtt_ms'] / 1000.0)
            allow(s).to receive(:tags).and_return(server['tags'])
            allow(s).to receive(:connectable?).and_return(true)
            allow(s).to receive(:check_driver_support!).and_return(true)
          end
        end
      end

      let(:server_selector) do
        Mongo::ServerSelector.get(:mode => spec.read_preference['mode'],
                                  :tag_sets => spec.read_preference['tag_sets'])
      end

      before do
        allow(cluster).to receive(:servers).and_return(candidate_servers)
        allow(cluster).to receive(:options).and_return(server_selection_timeout: 0.2)
        allow(cluster).to receive(:scan!).and_return(true)
        allow(cluster).to receive(:app_metadata).and_return(app_metadata)
      end

      context 'Valid read preference and matching server available', if: spec.server_available? do

        it 'Finds all suitable servers in the latency window', if: spec.replica_set? do
          expect(server_selector.send(:select, cluster.servers)).to match_array(in_latency_window)
        end

        it 'Finds the most suitable server in the latency window' do
          expect(in_latency_window).to include(server_selector.select_server(cluster))
        end
      end

      context 'No matching server available', if: !spec.server_available? do

        it 'Raises exception' do
          expect do
            server_selector.select_server(cluster)
          end.to raise_exception(Mongo::Error::NoServerAvailable)
        end
      end
    end
  end
end