File: server_selection_rtt_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 (85 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download | duplicates (4)
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
require 'spec_helper'

describe 'Server Selection moving average round trip time calculation' do

  include Mongo::ServerSelection::RTT

  SERVER_SELECTION_RTT_TESTS.each do |file|

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

    context(spec.description) do

      before(:all) do

        module Mongo
          class Server

            # We monkey-patch the monitor here, so the last average rtt can be controlled.
            # We keep the API of Monitor#initialize but add in an extra option and set the last rtt.
            #
            # @since 2.0.0
            class Monitor

              alias :original_initialize :initialize
              def initialize(address, listeners, options = {})
                @description = Mongo::Server::Description.new(address, {})
                @inspector = Mongo::Server::Description::Inspector.new(listeners)
                @options = options.freeze
                @connection = Connection.new(address, options)
                @last_round_trip_time = options[:avg_rtt_ms]
                @mutex = Mutex.new
              end

              # We monkey patch this method to use an instance variable instead of calculating time elapsed.
              #
              # @since 2.0.0
              alias :original_average_round_trip_time :average_round_trip_time
              def average_round_trip_time(start)
                new_rtt = @new_rtt_ms
                RTT_WEIGHT_FACTOR * new_rtt + (1 - RTT_WEIGHT_FACTOR) * (@last_round_trip_time || new_rtt)
              end
            end
          end
        end
      end

      after(:all) do

        module Mongo
          class Server

            # Return the monitor implementation to its original for the other
            # tests in the suite.
            class Monitor

              alias :initialize :original_initialize
              remove_method(:original_initialize)

              alias :average_round_trip_time :original_average_round_trip_time
              remove_method(:original_average_round_trip_time)
            end
          end
        end
      end

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

      let(:monitor) do
        Mongo::Server::Monitor.new(address, Mongo::Event::Listeners.new,
                                   TEST_OPTIONS.merge(avg_rtt_ms: spec.avg_rtt_ms))
      end

      before do
        monitor.instance_variable_set(:@new_rtt_ms, spec.new_rtt_ms)
        monitor.scan!
      end

      it 'correctly calculates the moving average round trip time' do
        expect(monitor.description.average_round_trip_time).to eq(spec.new_avg_rtt)
      end
    end
  end
end