Sha256: 3c095177968183399ed8cac2d562694fdec6807f4b2c740b4edda05483eda9af

Contents?: true

Size: 1.9 KB

Versions: 96

Compression:

Stored size: 1.9 KB

Contents

require 'test_helper'

class Elasticsearch::Transport::Transport::Connections::SelectorTest < Test::Unit::TestCase
  include Elasticsearch::Transport::Transport::Connections::Selector

  class DummyStrategySelector
    include Elasticsearch::Transport::Transport::Connections::Selector::Base
  end

  class BackupStrategySelector
    include Elasticsearch::Transport::Transport::Connections::Selector::Base

    def select(options={})
      connections.reject do |c|
        c.host[:attributes] && c.host[:attributes][:backup]
      end.send( defined?(RUBY_VERSION) && RUBY_VERSION > '1.9' ? :sample : :choice)
    end
  end

  context "Connection selector" do

    should "be initialized with connections" do
      assert_equal [1, 2], Random.new(:connections => [1, 2]).connections
    end

    should "have the abstract select method" do
      assert_raise(NoMethodError) { DummyStrategySelector.new.select }
    end

    context "in random strategy" do
      setup do
        @selector = Random.new :connections => ['A', 'B', 'C']
      end

      should "pick a connection" do
        assert_not_nil @selector.select
      end
    end

    context "in round-robin strategy" do
      setup do
        @selector = RoundRobin.new :connections => ['A', 'B', 'C']
      end

      should "rotate over connections" do
        assert_equal 'A', @selector.select
        assert_equal 'B', @selector.select
        assert_equal 'C', @selector.select
        assert_equal 'A', @selector.select
      end
    end

    context "with a custom strategy" do

      should "return proper connection" do
        selector = BackupStrategySelector.new :connections => [ stub(:host => { :hostname => 'host1' }),
                                                                stub(:host => { :hostname => 'host2', :attributes => { :backup => true }}) ]
        10.times { assert_equal 'host1', selector.select.host[:hostname] }
      end

    end

  end
end

Version data entries

96 entries across 96 versions & 8 rubygems

Version Path
logstash-output-scalyr-0.1.9 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.8 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.6 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
elasticsearch-transport-6.8.1 test/unit/connection_selector_test.rb
elasticsearch-transport-6.8.0 test/unit/connection_selector_test.rb
elasticsearch-transport-6.3.1 test/unit/connection_selector_test.rb
elasticsearch-transport-6.3.0 test/unit/connection_selector_test.rb
elasticsearch-transport-6.2.0 test/unit/connection_selector_test.rb
logstash-filter-device_detection-1.0.7-java vendor/bundle/jruby/1.9/gems/elasticsearch-transport-5.0.5/test/unit/connection_selector_test.rb
elasticsearch-transport-6.1.0 test/unit/connection_selector_test.rb
elasticsearch-transport-6.0.3 test/unit/connection_selector_test.rb
elasticsearch-transport-5.0.5 test/unit/connection_selector_test.rb